|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + opConfig "github.com/onepanelio/cli/config" |
| 6 | + "github.com/onepanelio/cli/files" |
| 7 | + "github.com/onepanelio/cli/util" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "path/filepath" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + // skipConfirmDelete if true, will skip the confirmation prompt of the delete command |
| 14 | + skipConfirmDelete bool |
| 15 | +) |
| 16 | + |
| 17 | +var deleteCmd = &cobra.Command{ |
| 18 | + Use: "delete", |
| 19 | + Short: "Deletes onepanel cluster resources", |
| 20 | + Long: "Delete all onepanel kubernetes cluster resources. Does not delete database unless it is in-cluster.", |
| 21 | + Example: "delete", |
| 22 | + Run: func(cmd *cobra.Command, args []string) { |
| 23 | + if skipConfirmDelete == false { |
| 24 | + fmt.Print("Are you sure you want to delete onepanel? ('y' or 'yes' to confirm. Anything else to cancel): ") |
| 25 | + userInput := "" |
| 26 | + if _, err := fmt.Scanln(&userInput); err != nil { |
| 27 | + fmt.Printf("Unable to get response\n") |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + if userInput != "y" && userInput != "yes" { |
| 32 | + return |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + config, err := opConfig.FromFile("config.yaml") |
| 37 | + if err != nil { |
| 38 | + fmt.Printf("Unable to read configuration file: %v", err.Error()) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + paramsYamlFile, err := util.LoadDynamicYamlFromFile(config.Spec.Params) |
| 43 | + if err != nil { |
| 44 | + fmt.Println("Error parsing configuration file.") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + defaultNamespaceNode := paramsYamlFile.GetValue("application.defaultNamespace") |
| 49 | + if defaultNamespaceNode == nil { |
| 50 | + fmt.Printf("application.defaultNamespace is missing from your '%s' file\n", config.Spec.Params) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + if defaultNamespaceNode.Value == "default" { |
| 55 | + fmt.Println("Unable to delete onepanel in the 'default' namespace") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if defaultNamespaceNode.Value == "<namespace>" { |
| 60 | + fmt.Println("Unable to delete onepanel. No namespace set.") |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + filesToDelete := []string{ |
| 65 | + filepath.Join(".onepanel", "kubernetes.yaml"), |
| 66 | + filepath.Join(".onepanel", "application.kubernetes.yaml"), |
| 67 | + } |
| 68 | + |
| 69 | + for _, filePath := range filesToDelete { |
| 70 | + exists, err := files.Exists(filePath) |
| 71 | + if err != nil { |
| 72 | + fmt.Printf("Error checking if onepanel files exist: %v\n", err.Error()) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if !exists { |
| 77 | + fmt.Printf("'%v' file does not exist. Are you in the directory where you ran 'opctl init'?\n", filePath) |
| 78 | + return |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Printf("Deleting onepanel from your cluster...\n") |
| 83 | + for _, filePath := range filesToDelete { |
| 84 | + if err := util.KubectlDelete(filePath); err != nil { |
| 85 | + fmt.Printf("Unable to delete: %v\n", err.Error()) |
| 86 | + return |
| 87 | + } |
| 88 | + } |
| 89 | + }, |
| 90 | +} |
| 91 | + |
| 92 | +func init() { |
| 93 | + rootCmd.AddCommand(deleteCmd) |
| 94 | + deleteCmd.Flags().BoolVarP(&skipConfirmDelete, "yes", "y", false, "Add this in to skip the confirmation prompt") |
| 95 | +} |
0 commit comments