project.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/spf13/cobra"
  8. )
  9. // projectCmd represents the "porter project" base command when called
  10. // without any subcommands
  11. var projectCmd = &cobra.Command{
  12. Use: "project",
  13. Short: "Commands that control Porter project settings",
  14. }
  15. var createProjectCmd = &cobra.Command{
  16. Use: "create [name]",
  17. Args: cobra.ExactArgs(1),
  18. Short: "Creates a project with the authorized user as admin",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. err := createProject(getHost(), args[0])
  21. if err != nil {
  22. fmt.Printf("An error occurred: %v\n", err)
  23. os.Exit(1)
  24. }
  25. },
  26. }
  27. func init() {
  28. rootCmd.AddCommand(projectCmd)
  29. projectCmd.AddCommand(createProjectCmd)
  30. projectCmd.PersistentFlags().StringVar(
  31. &host,
  32. "host",
  33. getHost(),
  34. "host url of Porter instance",
  35. )
  36. projectCmd.AddCommand(setProjectCmd)
  37. }
  38. func createProject(host string, name string) error {
  39. client := api.NewClient(host+"/api", "cookie.json")
  40. resp, err := client.CreateProject(context.Background(), &api.CreateProjectRequest{
  41. Name: name,
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. fmt.Printf("Created project with name %s and id %d\n", name, resp.ID)
  47. return setProject(resp.ID)
  48. }