project.go 3.4 KB

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