Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
cli/cmd: make component delete use errors instead of Fatalf
Browse files Browse the repository at this point in the history
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 #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 16, 2020
1 parent 8f02bf8 commit fa617c5
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions cli/cmd/component-delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

// componentApply 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(
Expand All @@ -82,18 +90,22 @@ 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,
Expand Down

0 comments on commit fa617c5

Please sign in to comment.