project.go 3.6 KB

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