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 initialize() return error instead of using Fatalf
Browse files Browse the repository at this point in the history
To remove a dependency on log.Entry to make moving this code around
easier and to avoid hiding function complexity from logger.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 7, 2020
1 parent c01551a commit 3aa6283
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func runClusterApply(cmd *cobra.Command, args []string) {

//nolint:funlen
func clusterApply(contextLogger *log.Entry) error {
c := initialize(contextLogger)
c, err := initialize(contextLogger)
if err != nil {
return fmt.Errorf("initializing: %w", err)
}

exists := clusterExists(contextLogger, &c.terraformExecutor)
if exists && !confirm {
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/cluster-destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func runClusterDestroy(cmd *cobra.Command, args []string) {
}

func clusterDestroy(contextLogger *log.Entry) error {
c := initialize(contextLogger)
c, err := initialize(contextLogger)
if err != nil {
return fmt.Errorf("initializing: %w", err)
}

if !clusterExists(contextLogger, &c.terraformExecutor) {
contextLogger.Println("Cluster already destroyed, nothing to do")
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ type cluster struct {

// initialize does common initialization actions between cluster operations
// and returns created objects to the caller for further use.
func initialize(contextLogger *log.Entry) *cluster {
func initialize(contextLogger *log.Entry) (*cluster, error) {
lokoConfig, diags := getLokoConfig()
if diags.HasErrors() {
contextLogger.Fatal(diags)
return nil, diags
}

p, diags := getConfiguredPlatform(lokoConfig, true)
Expand All @@ -64,7 +64,7 @@ func initialize(contextLogger *log.Entry) *cluster {
contextLogger.Error(diagnostic.Error())
}

contextLogger.Fatal("Errors found while loading cluster configuration")
return nil, fmt.Errorf("loading platform configuration")
}

// Get the configured backend for the cluster. Backend types currently supported: local, s3.
Expand All @@ -74,7 +74,7 @@ func initialize(contextLogger *log.Entry) *cluster {
contextLogger.Error(diagnostic.Error())
}

contextLogger.Fatal("Errors found while loading cluster configuration")
return nil, fmt.Errorf("loading backend configuration")
}

// Use a local backend if no backend is configured.
Expand All @@ -84,12 +84,12 @@ func initialize(contextLogger *log.Entry) *cluster {

assetDir, err := homedir.Expand(p.Meta().AssetDir)
if err != nil {
contextLogger.Fatalf("Error expanding path: %v", err)
return nil, fmt.Errorf("expanding path %q: %v", p.Meta().AssetDir, err)
}

// Validate backend configuration.
if err = b.Validate(); err != nil {
contextLogger.Fatalf("Failed to validate backend configuration: %v", err)
return nil, fmt.Errorf("validating backend configuration: %v", err)
}

ex := initializeTerraform(contextLogger, p, b)
Expand All @@ -99,7 +99,7 @@ func initialize(contextLogger *log.Entry) *cluster {
platform: p,
lokomotiveConfig: lokoConfig,
assetDir: assetDir,
}
}, nil
}

// initializeTerraform initialized Terraform directory using given backend and platform
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ func readKubeconfigFromTerraformState(contextLogger *log.Entry) ([]byte, error)
contextLogger.Warn("Kubeconfig file not found in assets directory, pulling kubeconfig from " +
"Terraform state, this might be slow. Run 'lokoctl cluster apply' to fix it.")

c := initialize(contextLogger)
c, err := initialize(contextLogger)
if err != nil {
return nil, fmt.Errorf("initializing: %w", err)
}

kubeconfig := ""

Expand Down

0 comments on commit 3aa6283

Please sign in to comment.