delete.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // deleteAppsCmd represents the "porter delete apps" subcommand
  35. var deleteAppsCmd = &cobra.Command{
  36. Use: "apps",
  37. Aliases: []string{"app"},
  38. Short: "Deletes an existing app",
  39. Run: func(cmd *cobra.Command, args []string) {
  40. err := checkLoginAndRun(args, deleteApp)
  41. if err != nil {
  42. os.Exit(1)
  43. }
  44. },
  45. }
  46. func init() {
  47. deleteCmd.AddCommand(deleteAppsCmd)
  48. rootCmd.AddCommand(deleteCmd)
  49. }
  50. func delete(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  51. projectID := cliConf.Project
  52. if projectID == 0 {
  53. return fmt.Errorf("project id must be set")
  54. }
  55. clusterID := cliConf.Cluster
  56. if clusterID == 0 {
  57. return fmt.Errorf("cluster id must be set")
  58. }
  59. var environmentID string
  60. var gitRepoName string
  61. var gitRepoOwner string
  62. var gitPRNumber string
  63. if envID := os.Getenv("PORTER_ENVIRONMENT_ID"); envID != "" {
  64. environmentID = envID
  65. } else {
  66. return fmt.Errorf("Environment ID must be defined, set by PORTER_ENVIRONMENT_ID")
  67. }
  68. if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
  69. gitRepoName = repoName
  70. } else {
  71. return fmt.Errorf("Repo name must be defined, set by PORTER_REPO_NAME")
  72. }
  73. if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
  74. gitRepoOwner = repoOwner
  75. } else {
  76. return fmt.Errorf("Repo owner must be defined, set by PORTER_REPO_OWNER")
  77. }
  78. if prNumber := os.Getenv("PORTER_PR_NUMBER"); prNumber != "" {
  79. gitPRNumber = prNumber
  80. } else {
  81. return fmt.Errorf("Pull request number must be defined, set by PORTER_PR_NUMBER")
  82. }
  83. return client.DeleteDeployment(
  84. context.Background(), projectID, clusterID, environmentID,
  85. gitRepoOwner, gitRepoName, gitPRNumber,
  86. )
  87. }
  88. func deleteApp(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  89. return nil
  90. }