project.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. "github.com/fatih/color"
  8. "github.com/porter-dev/porter/cli/cmd/api"
  9. "github.com/spf13/cobra"
  10. )
  11. // projectCmd represents the "porter project" base command when called
  12. // without any subcommands
  13. var projectCmd = &cobra.Command{
  14. Use: "project",
  15. Short: "Commands that control Porter project settings",
  16. }
  17. var createProjectCmd = &cobra.Command{
  18. Use: "create [name]",
  19. Args: cobra.ExactArgs(1),
  20. Short: "Creates a project with the authorized user as admin",
  21. Run: func(cmd *cobra.Command, args []string) {
  22. err := checkLoginAndRun(args, createProject)
  23. if err != nil {
  24. os.Exit(1)
  25. }
  26. },
  27. }
  28. var listProjectCmd = &cobra.Command{
  29. Use: "list",
  30. Short: "Lists the projects for the logged in user",
  31. Run: func(cmd *cobra.Command, args []string) {
  32. err := checkLoginAndRun(args, listProjects)
  33. if err != nil {
  34. os.Exit(1)
  35. }
  36. },
  37. }
  38. var listProjectClustersCmd = &cobra.Command{
  39. Use: "clusters list",
  40. Short: "Lists the linked clusters for a project",
  41. Run: func(cmd *cobra.Command, args []string) {
  42. err := checkLoginAndRun(args, listProjectClusters)
  43. if err != nil {
  44. os.Exit(1)
  45. }
  46. },
  47. }
  48. func init() {
  49. rootCmd.AddCommand(projectCmd)
  50. projectCmd.AddCommand(createProjectCmd)
  51. projectCmd.PersistentFlags().StringVar(
  52. &host,
  53. "host",
  54. getHost(),
  55. "host url of Porter instance",
  56. )
  57. projectCmd.AddCommand(listProjectCmd)
  58. projectCmd.AddCommand(listProjectClustersCmd)
  59. }
  60. func createProject(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  61. resp, err := client.CreateProject(context.Background(), &api.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 setProject(resp.ID)
  69. }
  70. func listProjects(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  71. projects, err := client.ListUserProjects(context.Background(), user.ID)
  72. if err != nil {
  73. return err
  74. }
  75. w := new(tabwriter.Writer)
  76. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  77. fmt.Fprintf(w, "%s\t%s\n", "ID", "NAME")
  78. currProjectID := getProjectID()
  79. for _, project := range projects {
  80. if currProjectID == project.ID {
  81. color.New(color.FgGreen).Fprintf(w, "%d\t%s (current project)\n", project.ID, project.Name)
  82. } else {
  83. fmt.Fprintf(w, "%d\t%s\n", project.ID, project.Name)
  84. }
  85. }
  86. w.Flush()
  87. return nil
  88. }
  89. func listProjectClusters(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  90. clusters, err := client.ListProjectClusters(context.Background(), getProjectID())
  91. if err != nil {
  92. return err
  93. }
  94. w := new(tabwriter.Writer)
  95. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  96. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "SERVER")
  97. for _, cluster := range clusters {
  98. fmt.Fprintf(w, "%d\t%s\t%s\n", cluster.ID, cluster.Name, cluster.Server)
  99. }
  100. w.Flush()
  101. return nil
  102. }