delete.go 2.5 KB

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