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 initializeTerraform() return error
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.

Also, it no longer requires logger, so argument has been removed.

Part of #630

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Oct 8, 2020
1 parent 3e85f8d commit f21dc89
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ func initialize(contextLogger *log.Entry) (*cluster, error) {
return nil, fmt.Errorf("validating backend configuration: %v", err)
}

ex := initializeTerraform(contextLogger, p, b)
ex, err := initializeTerraform(p, b)
if err != nil {
return nil, fmt.Errorf("initializing Terraform: %w", err)
}

return &cluster{
terraformExecutor: *ex,
Expand All @@ -104,21 +107,21 @@ func initialize(contextLogger *log.Entry) (*cluster, error) {

// initializeTerraform initialized Terraform directory using given backend and platform
// and returns configured executor.
func initializeTerraform(contextLogger *log.Entry, p platform.Platform, b backend.Backend) *terraform.Executor {
func initializeTerraform(p platform.Platform, b backend.Backend) (*terraform.Executor, error) {
assetDir, err := homedir.Expand(p.Meta().AssetDir)
if err != nil {
contextLogger.Fatalf("Error expanding path: %v", err)
return nil, fmt.Errorf("expanding path %q: %w", p.Meta().AssetDir, err)
}

// Render backend configuration.
renderedBackend, err := b.Render()
if err != nil {
contextLogger.Fatalf("Failed to render backend configuration file: %v", err)
return nil, fmt.Errorf("rendering backend configuration: %w", err)
}

// Configure Terraform directory, module and backend.
if err := terraform.Configure(assetDir, renderedBackend); err != nil {
contextLogger.Fatalf("Failed to configure Terraform : %v", err)
return nil, fmt.Errorf("configuring Terraform: %w", err)
}

conf := terraform.Config{
Expand All @@ -128,18 +131,18 @@ func initializeTerraform(contextLogger *log.Entry, p platform.Platform, b backen

ex, err := terraform.NewExecutor(conf)
if err != nil {
contextLogger.Fatalf("Failed to create Terraform executor: %v", err)
return nil, fmt.Errorf("creating Terraform executor: %w", err)
}

if err := p.Initialize(ex); err != nil {
contextLogger.Fatalf("Failed to initialize Platform: %v", err)
return nil, fmt.Errorf("initializing Platform: %w", err)
}

if err := ex.Init(); err != nil {
contextLogger.Fatalf("Failed to initialize Terraform: %v", err)
return nil, fmt.Errorf("running 'terraform init': %w", err)
}

return ex
return ex, nil
}

// clusterExists determines if cluster has already been created by getting all
Expand Down

0 comments on commit f21dc89

Please sign in to comment.