cli/cmd: make component delete use errors instead of Fatalf
To soften the dependency on log.Entry and to separate the functionality
from CLI code. This also makes code easier to move around and avoids
hiding function complexity.
Part of
@@ -63,16 +63,24 @@ func runDelete(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
}
+ if err := componentDelete(contextLogger, args); err != nil {
+ contextLogger.Fatalf("Deleting components failed: %v", err)
+ }
+}
+
+// componentDelete implements 'lokoctl component delete' separated from CLI
+// dependencies.
+func componentDelete(contextLogger *log.Entry, componentsList []string) error {
lokoConfig, diags := getLokoConfig()
if diags.HasErrors() {
- contextLogger.Fatal(diags)
+ return diags
}
- componentsToDelete := selectComponentNames(args, *lokoConfig.RootConfig)
+ componentsToDelete := selectComponentNames(componentsList, *lokoConfig.RootConfig)
componentObjects, err := componentNamesToObjects(componentsToDelete)
if err != nil {
- contextLogger.Fatalf("getting component objects: %v", err)
+ return fmt.Errorf("getting component objects: %v", err)
}
confirmationMessage := fmt.Sprintf(
@@ -83,18 +91,21 @@ func runDelete(cmd *cobra.Command, args []string) {
if !confirm && !askForConfirmation(confirmationMessage) {
contextLogger.Info("Components deletion cancelled.")
- return
+ return nil
}
kubeconfig, err := getKubeconfig(contextLogger, lokoConfig, false)
if err != nil {
contextLogger.Debugf("Error in finding kubeconfig file: %s", err)
- contextLogger.Fatal("Suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
+
+ return fmt.Errorf("suitable kubeconfig file not found. Did you run 'lokoctl cluster apply' ?")
}
if err := deleteComponents(kubeconfig, componentObjects); err != nil {
- contextLogger.Fatal(err)
+ return fmt.Errorf("deleting components: %w", err)
}
+
+ return nil
}
// selectComponentNames returns list of components to operate on. If explicit list is empty,
