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 apply 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 it strictly
from *cobra.Command, so it is easier to move this code around in the
future.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 16, 2020
1 parent cdf5f77 commit e707440
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cli/cmd/component-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ func runApply(cmd *cobra.Command, args []string) {
log.SetLevel(log.DebugLevel)
}

if err := componentApply(contextLogger, args); err != nil {
contextLogger.Fatalf("Applying components failed: %v", err)
}
}

// componentApply implements 'lokoctl component apply' separated from CLI
// dependencies.
func componentApply(contextLogger *log.Entry, componentsList []string) error {
lokoConfig, diags := getLokoConfig()
if diags.HasErrors() {
contextLogger.Fatal(diags)
return diags
}

componentsToApply := args
componentsToApply := componentsList
if len(componentsToApply) == 0 {
for _, component := range lokoConfig.RootConfig.Components {
componentsToApply = append(componentsToApply, component.Name)
Expand All @@ -76,12 +84,15 @@ func runApply(cmd *cobra.Command, args []string) {
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 := applyComponents(lokoConfig, kubeconfig, componentsToApply...); err != nil {
contextLogger.Fatal(err)
return fmt.Errorf("applying components: %w", err)
}

return nil
}

func applyComponents(lokoConfig *config.Config, kubeconfig []byte, componentNames ...string) error {
Expand Down

0 comments on commit e707440

Please sign in to comment.