project.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.PersistentFlags().StringVar(
  56. &host,
  57. "host",
  58. getHost(),
  59. "host url of Porter instance",
  60. )
  61. projectCmd.AddCommand(createProjectCmd)
  62. projectCmd.AddCommand(deleteProjectCmd)
  63. projectCmd.AddCommand(listProjectCmd)
  64. }
  65. func createProject(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  66. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  67. Name: args[0],
  68. })
  69. if err != nil {
  70. return err
  71. }
  72. color.New(color.FgGreen).Printf("Created project with name %s and id %d\n", args[0], resp.ID)
  73. return setProject(resp.ID)
  74. }
  75. func listProjects(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  76. projects, err := client.ListUserProjects(context.Background(), user.ID)
  77. if err != nil {
  78. return err
  79. }
  80. w := new(tabwriter.Writer)
  81. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  82. fmt.Fprintf(w, "%s\t%s\n", "ID", "NAME")
  83. currProjectID := getProjectID()
  84. for _, project := range projects {
  85. if currProjectID == project.ID {
  86. color.New(color.FgGreen).Fprintf(w, "%d\t%s (current project)\n", project.ID, project.Name)
  87. } else {
  88. fmt.Fprintf(w, "%d\t%s\n", project.ID, project.Name)
  89. }
  90. }
  91. w.Flush()
  92. return nil
  93. }
  94. func deleteProject(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  95. userResp, err := utils.PromptPlaintext(
  96. fmt.Sprintf(
  97. `Are you sure you'd like to delete the project with id %s? %s `,
  98. args[0],
  99. color.New(color.FgCyan).Sprintf("[y/n]"),
  100. ),
  101. )
  102. if err != nil {
  103. return err
  104. }
  105. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  106. id, err := strconv.ParseUint(args[0], 10, 64)
  107. if err != nil {
  108. return err
  109. }
  110. resp, err := client.DeleteProject(context.Background(), uint(id))
  111. if err != nil {
  112. return err
  113. }
  114. color.New(color.FgGreen).Printf("Deleted project with name %s and id %d\n", resp.Name, resp.ID)
  115. }
  116. return nil
  117. }