project.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Short: "Commands that control Porter project settings",
  19. }
  20. var createProjectCmd = &cobra.Command{
  21. Use: "create [name]",
  22. Args: cobra.ExactArgs(1),
  23. Short: "Creates a project with the authorized user as admin",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. err := checkLoginAndRun(args, createProject)
  26. if err != nil {
  27. os.Exit(1)
  28. }
  29. },
  30. }
  31. var deleteProjectCmd = &cobra.Command{
  32. Use: "delete [id]",
  33. Args: cobra.ExactArgs(1),
  34. Short: "Deletes the project with the given id",
  35. Run: func(cmd *cobra.Command, args []string) {
  36. err := checkLoginAndRun(args, deleteProject)
  37. if err != nil {
  38. os.Exit(1)
  39. }
  40. },
  41. }
  42. var listProjectCmd = &cobra.Command{
  43. Use: "list",
  44. Short: "Lists the projects for the logged in user",
  45. Run: func(cmd *cobra.Command, args []string) {
  46. err := checkLoginAndRun(args, listProjects)
  47. if err != nil {
  48. os.Exit(1)
  49. }
  50. },
  51. }
  52. var listProjectClustersCmd = &cobra.Command{
  53. Use: "clusters list",
  54. Short: "Lists the linked clusters for a project",
  55. Run: func(cmd *cobra.Command, args []string) {
  56. err := checkLoginAndRun(args, listProjectClusters)
  57. if err != nil {
  58. os.Exit(1)
  59. }
  60. },
  61. }
  62. func init() {
  63. rootCmd.AddCommand(projectCmd)
  64. projectCmd.PersistentFlags().StringVar(
  65. &host,
  66. "host",
  67. getHost(),
  68. "host url of Porter instance",
  69. )
  70. projectCmd.AddCommand(createProjectCmd)
  71. projectCmd.AddCommand(deleteProjectCmd)
  72. projectCmd.AddCommand(listProjectCmd)
  73. projectCmd.AddCommand(listProjectClustersCmd)
  74. }
  75. func createProject(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  76. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  77. Name: args[0],
  78. })
  79. if err != nil {
  80. return err
  81. }
  82. color.New(color.FgGreen).Printf("Created project with name %s and id %d\n", args[0], resp.ID)
  83. return setProject(resp.ID)
  84. }
  85. func listProjects(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  86. projects, err := client.ListUserProjects(context.Background(), user.ID)
  87. if err != nil {
  88. return err
  89. }
  90. w := new(tabwriter.Writer)
  91. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  92. fmt.Fprintf(w, "%s\t%s\n", "ID", "NAME")
  93. currProjectID := getProjectID()
  94. for _, project := range projects {
  95. if currProjectID == project.ID {
  96. color.New(color.FgGreen).Fprintf(w, "%d\t%s (current project)\n", project.ID, project.Name)
  97. } else {
  98. fmt.Fprintf(w, "%d\t%s\n", project.ID, project.Name)
  99. }
  100. }
  101. w.Flush()
  102. return nil
  103. }
  104. func deleteProject(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  105. userResp, err := utils.PromptPlaintext(
  106. fmt.Sprintf(
  107. `Are you sure you'd like to delete the project with id %s? %s `,
  108. args[0],
  109. color.New(color.FgCyan).Sprintf("[y/n]"),
  110. ),
  111. )
  112. if err != nil {
  113. return err
  114. }
  115. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  116. id, err := strconv.ParseUint(args[0], 10, 64)
  117. if err != nil {
  118. return err
  119. }
  120. resp, err := client.DeleteProject(context.Background(), uint(id))
  121. if err != nil {
  122. return err
  123. }
  124. color.New(color.FgGreen).Printf("Deleted project with name %s and id %d\n", resp.Name, resp.ID)
  125. }
  126. return nil
  127. }
  128. func listProjectClusters(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  129. clusters, err := client.ListProjectClusters(context.Background(), getProjectID())
  130. if err != nil {
  131. return err
  132. }
  133. w := new(tabwriter.Writer)
  134. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  135. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "SERVER")
  136. for _, cluster := range clusters {
  137. fmt.Fprintf(w, "%d\t%s\t%s\n", cluster.ID, cluster.Name, cluster.Server)
  138. }
  139. w.Flush()
  140. return nil
  141. }