project.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "text/tabwriter"
  9. "github.com/fatih/color"
  10. api "github.com/porter-dev/porter/api/client"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/cli/cmd/config"
  13. "github.com/porter-dev/porter/cli/cmd/utils"
  14. "github.com/spf13/cobra"
  15. )
  16. func registerCommand_Project(cliConf config.CLIConfig) *cobra.Command {
  17. projectCmd := &cobra.Command{
  18. Use: "project",
  19. Aliases: []string{"projects"},
  20. Short: "Commands that control Porter project settings",
  21. }
  22. createProjectCmd := &cobra.Command{
  23. Use: "create [name]",
  24. Args: cobra.ExactArgs(1),
  25. Short: "Creates a project with the authorized user as admin",
  26. Run: func(cmd *cobra.Command, args []string) {
  27. err := checkLoginAndRunWithConfig(cmd.Context(), cliConf, args, createProject)
  28. if err != nil {
  29. os.Exit(1)
  30. }
  31. },
  32. }
  33. projectCmd.AddCommand(createProjectCmd)
  34. deleteProjectCmd := &cobra.Command{
  35. Use: "delete [id]",
  36. Args: cobra.ExactArgs(1),
  37. Short: "Deletes the project with the given id",
  38. Run: func(cmd *cobra.Command, args []string) {
  39. err := checkLoginAndRunWithConfig(cmd.Context(), cliConf, args, deleteProject)
  40. if err != nil {
  41. os.Exit(1)
  42. }
  43. },
  44. }
  45. projectCmd.AddCommand(deleteProjectCmd)
  46. listProjectCmd := &cobra.Command{
  47. Use: "list",
  48. Short: "Lists the projects for the logged in user",
  49. Run: func(cmd *cobra.Command, args []string) {
  50. err := checkLoginAndRunWithConfig(cmd.Context(), cliConf, args, listProjects)
  51. if err != nil {
  52. os.Exit(1)
  53. }
  54. },
  55. }
  56. projectCmd.AddCommand(listProjectCmd)
  57. return projectCmd
  58. }
  59. func createProject(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  60. resp, err := client.CreateProject(ctx, &types.CreateProjectRequest{
  61. Name: args[0],
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. color.New(color.FgGreen).Printf("Created project with name %s and id %d\n", args[0], resp.ID)
  67. return cliConf.SetProject(ctx, client, resp.ID)
  68. }
  69. func listProjects(ctx context.Context, user *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  70. resp, err := client.ListUserProjects(ctx)
  71. if err != nil {
  72. return err
  73. }
  74. projects := *resp
  75. w := new(tabwriter.Writer)
  76. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  77. fmt.Fprintf(w, "%s\t%s\n", "ID", "NAME")
  78. currProjectID := cliConf.Project
  79. for _, project := range projects {
  80. if currProjectID == project.ID {
  81. color.New(color.FgGreen).Fprintf(w, "%d\t%s (current project)\n", project.ID, project.Name)
  82. } else {
  83. fmt.Fprintf(w, "%d\t%s\n", project.ID, project.Name)
  84. }
  85. }
  86. w.Flush()
  87. return nil
  88. }
  89. func deleteProject(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, args []string) error {
  90. userResp, err := utils.PromptPlaintext(
  91. fmt.Sprintf(
  92. `Are you sure you'd like to delete the project with id %s? %s `,
  93. args[0],
  94. color.New(color.FgCyan).Sprintf("[y/n]"),
  95. ),
  96. )
  97. if err != nil {
  98. return err
  99. }
  100. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  101. id, err := strconv.ParseUint(args[0], 10, 64)
  102. if err != nil {
  103. return err
  104. }
  105. err = client.DeleteProject(ctx, uint(id))
  106. if err != nil {
  107. return err
  108. }
  109. color.New(color.FgGreen).Printf("Deleted project with id %d\n", id)
  110. }
  111. return nil
  112. }
  113. func setProjectCluster(ctx context.Context, client api.Client, cliConf config.CLIConfig, projectID uint) error {
  114. resp, err := client.ListProjectClusters(ctx, projectID)
  115. if err != nil {
  116. return err
  117. }
  118. clusters := *resp
  119. if len(clusters) > 0 {
  120. cliConf.SetCluster(clusters[0].ID)
  121. }
  122. return nil
  123. }