From fa617c5fb9068e5da0397fe4dae02b3da7d4b506 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 16 Oct 2020 17:17:44 +0200 Subject: [PATCH] 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 #630 Signed-off-by: Mateusz Gozdek --- cli/cmd/component-delete.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/cli/cmd/component-delete.go b/cli/cmd/component-delete.go index 5029b73d1..f199d0460 100644 --- a/cli/cmd/component-delete.go +++ b/cli/cmd/component-delete.go @@ -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( @@ -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,