-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from onepanelio/feat/onepanel.delete
feat: cli delete command
- Loading branch information
Showing
6 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
opConfig "github.com/onepanelio/cli/config" | ||
"github.com/onepanelio/cli/files" | ||
"github.com/onepanelio/cli/util" | ||
"github.com/spf13/cobra" | ||
"path/filepath" | ||
) | ||
|
||
var ( | ||
// skipConfirmDelete if true, will skip the confirmation prompt of the delete command | ||
skipConfirmDelete bool | ||
) | ||
|
||
var deleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Deletes onepanel cluster resources", | ||
Long: "Delete all onepanel kubernetes cluster resources. Does not delete database unless it is in-cluster.", | ||
Example: "delete", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if skipConfirmDelete == false { | ||
fmt.Print("Are you sure you want to delete onepanel? ('y' or 'yes' to confirm. Anything else to cancel): ") | ||
userInput := "" | ||
if _, err := fmt.Scanln(&userInput); err != nil { | ||
fmt.Printf("Unable to get response\n") | ||
return | ||
} | ||
|
||
if userInput != "y" && userInput != "yes" { | ||
return | ||
} | ||
} | ||
|
||
config, err := opConfig.FromFile("config.yaml") | ||
if err != nil { | ||
fmt.Printf("Unable to read configuration file: %v", err.Error()) | ||
return | ||
} | ||
|
||
paramsYamlFile, err := util.LoadDynamicYamlFromFile(config.Spec.Params) | ||
if err != nil { | ||
fmt.Println("Error parsing configuration file.") | ||
return | ||
} | ||
|
||
defaultNamespaceNode := paramsYamlFile.GetValue("application.defaultNamespace") | ||
if defaultNamespaceNode == nil { | ||
fmt.Printf("application.defaultNamespace is missing from your '%s' file\n", config.Spec.Params) | ||
return | ||
} | ||
|
||
if defaultNamespaceNode.Value == "default" { | ||
fmt.Println("Unable to delete onepanel in the 'default' namespace") | ||
return | ||
} | ||
|
||
if defaultNamespaceNode.Value == "<namespace>" { | ||
fmt.Println("Unable to delete onepanel. No namespace set.") | ||
return | ||
} | ||
|
||
filesToDelete := []string{ | ||
filepath.Join(".onepanel", "kubernetes.yaml"), | ||
filepath.Join(".onepanel", "application.kubernetes.yaml"), | ||
} | ||
|
||
for _, filePath := range filesToDelete { | ||
exists, err := files.Exists(filePath) | ||
if err != nil { | ||
fmt.Printf("Error checking if onepanel files exist: %v\n", err.Error()) | ||
return | ||
} | ||
|
||
if !exists { | ||
fmt.Printf("'%v' file does not exist. Are you in the directory where you ran 'opctl init'?\n", filePath) | ||
return | ||
} | ||
} | ||
|
||
fmt.Printf("Deleting onepanel from your cluster...\n") | ||
for _, filePath := range filesToDelete { | ||
if err := util.KubectlDelete(filePath); err != nil { | ||
fmt.Printf("Unable to delete: %v\n", err.Error()) | ||
return | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(deleteCmd) | ||
deleteCmd.Flags().BoolVarP(&skipConfirmDelete, "yes", "y", false, "Add this in to skip the confirmation prompt") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters