project.go 3.3 KB

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