delete.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/fatih/color"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/spf13/cobra"
  10. )
  11. // deleteCmd represents the "porter delete" base command
  12. var deleteCmd = &cobra.Command{
  13. Use: "delete",
  14. Short: "Deletes a deployment",
  15. Long: fmt.Sprintf(`
  16. %s
  17. Destroys a deployment, which is read based on env variables.
  18. %s
  19. The following are the environment variables that can be used to set certain values while
  20. deleting a configuration:
  21. PORTER_CLUSTER Cluster ID that contains the project
  22. PORTER_PROJECT Project ID that contains the application
  23. `,
  24. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter delete\":"),
  25. color.New(color.FgGreen, color.Bold).Sprintf("porter delete"),
  26. ),
  27. Run: func(cmd *cobra.Command, args []string) {
  28. err := checkLoginAndRun(args, delete)
  29. if err != nil {
  30. os.Exit(1)
  31. }
  32. },
  33. }
  34. func init() {
  35. rootCmd.AddCommand(deleteCmd)
  36. }
  37. func delete(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  38. projectID := cliConf.Project
  39. if projectID == 0 {
  40. return fmt.Errorf("project id must be set")
  41. }
  42. clusterID := cliConf.Cluster
  43. if clusterID == 0 {
  44. return fmt.Errorf("cluster id must be set")
  45. }
  46. var environmentID string
  47. var gitRepoName string
  48. var gitRepoOwner string
  49. var gitPRNumber string
  50. if envID := os.Getenv("PORTER_ENVIRONMENT_ID"); envID != "" {
  51. environmentID = envID
  52. } else {
  53. return fmt.Errorf("Environment ID must be defined, set by PORTER_ENVIRONMENT_ID")
  54. }
  55. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
  56. gitRepoName = repoName
  57. } else {
  58. return fmt.Errorf("Repo name must be defined, set by PORTER_REPO_NAME")
  59. }
  60. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
  61. gitRepoOwner = repoOwner
  62. } else {
  63. return fmt.Errorf("Repo owner must be defined, set by PORTER_REPO_OWNER")
  64. }
  65. if prNumber := os.Getenv("PORTER_PR_NUMBER"); prNumber != "" {
  66. gitPRNumber = prNumber
  67. } else {
  68. return fmt.Errorf("Pull request number must be defined, set by PORTER_PR_NUMBER")
  69. }
  70. return client.DeleteDeployment(
  71. context.Background(), projectID, clusterID, environmentID,
  72. gitRepoOwner, gitRepoName, gitPRNumber,
  73. )
  74. }