Skip to content

Commit

Permalink
Fix how we execute Terraform commands so stdout isn't dirtied with un…
Browse files Browse the repository at this point in the history
…related output
  • Loading branch information
brikis98 committed Apr 25, 2017
1 parent 3fc7468 commit 35c25b4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
14 changes: 2 additions & 12 deletions cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func runTerragrunt(terragruntOptions *options.TerragruntOptions) error {
}
}

return runTerraformCommand(terragruntOptions)
return shell.RunTerraformCommand(terragruntOptions, terragruntOptions.TerraformCliArgs...)
}

// Returns true if the command the user wants to execute is supposed to affect multiple Terraform modules, such as the
Expand Down Expand Up @@ -229,7 +229,7 @@ func downloadModules(terragruntOptions *options.TerragruntOptions) error {
return err
}
if shouldDownload {
return shell.RunShellCommand(terragruntOptions, terragruntOptions.TerraformPath, "get", "-update")
return shell.RunTerraformCommand(terragruntOptions, "get", "-update")
}
}

Expand Down Expand Up @@ -314,16 +314,6 @@ func outputAll(terragruntOptions *options.TerragruntOptions) error {
return stack.Output(terragruntOptions)
}

// Run the given Terraform command
func runTerraformCommand(terragruntOptions *options.TerragruntOptions) error {
return shell.RunShellCommand(terragruntOptions, terragruntOptions.TerraformPath, terragruntOptions.TerraformCliArgs...)
}

// Run the given Terraform command and return the stdout as a string
func runTerraformCommandAndCaptureOutput(terragruntOptions *options.TerragruntOptions) (string, error) {
return shell.RunShellCommandAndCaptureOutput(terragruntOptions, terragruntOptions.TerraformPath, terragruntOptions.TerraformCliArgs...)
}

// Custom error types

var DontManuallyConfigureRemoteState = fmt.Errorf("Instead of manually using the 'remote config' command, define your remote state settings in %s and Terragrunt will automatically configure it for you (and all your team members) next time you run it.", config.DefaultTerragruntConfigPath)
Expand Down
8 changes: 2 additions & 6 deletions cli/download_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"regexp"
"strings"
"github.com/gruntwork-io/terragrunt/shell"
)

// This struct represents information about Terraform source code that needs to be downloaded
Expand Down Expand Up @@ -348,11 +349,6 @@ func getTerraformSourceUrl(terragruntOptions *options.TerragruntOptions, terragr
func terraformInit(terraformSource *TerraformSource, terragruntOptions *options.TerragruntOptions) error {
terragruntOptions.Logger.Printf("Downloading Terraform configurations from %s into %s", terraformSource.CanonicalSourceURL, terraformSource.DownloadDir)

terragruntInitOptions := terragruntOptions.Clone(terragruntOptions.TerragruntConfigPath)

// Backend and get configuration will be handled separately
terragruntInitOptions.TerraformCliArgs = []string{"init", "-backend=false", "-get=false"}
terragruntInitOptions.TerraformCliArgs = append(terragruntInitOptions.TerraformCliArgs, terraformSource.CanonicalSourceURL.String(), terraformSource.DownloadDir)

return runTerraformCommand(terragruntInitOptions)
return shell.RunTerraformCommand(terragruntOptions, "init", "-backend=false", "-get=false", terraformSource.CanonicalSourceURL.String(), terraformSource.DownloadDir)
}
6 changes: 2 additions & 4 deletions cli/version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gruntwork-io/terragrunt/options"
"github.com/hashicorp/go-version"
"regexp"
"github.com/gruntwork-io/terragrunt/shell"
)

// The terraform --version output is of the format: Terraform v0.9.3
Expand Down Expand Up @@ -38,10 +39,7 @@ func checkTerraformVersionMeetsConstraint(currentVersion *version.Version, const

// Get the currently installed version of Terraform
func getTerraformVersion(terragruntOptions *options.TerragruntOptions) (*version.Version, error) {
terragruntOptionsCopy := terragruntOptions.Clone(terragruntOptions.TerragruntConfigPath)
terragruntOptionsCopy.TerraformCliArgs = []string{"--version"}

output, err := runTerraformCommandAndCaptureOutput(terragruntOptionsCopy)
output, err := shell.RunTerraformCommandAndCaptureOutput(terragruntOptions, "--version")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion remote/remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (remoteState RemoteState) ConfigureRemoteState(terragruntOptions *options.T
}

terragruntOptions.Logger.Printf("Configuring remote state for the %s backend", remoteState.Backend)
return shell.RunShellCommand(terragruntOptions, terragruntOptions.TerraformPath, initCommand(remoteState)...)
return shell.RunTerraformCommand(terragruntOptions, initCommand(remoteState)...)
}

return nil
Expand Down
10 changes: 10 additions & 0 deletions shell/run_shell_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ import (
"github.com/gruntwork-io/terragrunt/options"
)

// Run the given Terraform command
func RunTerraformCommand(terragruntOptions *options.TerragruntOptions, args ...string) error {
return RunShellCommand(terragruntOptions, terragruntOptions.TerraformPath, args...)
}

// Run the given Terraform command and return the stdout as a string
func RunTerraformCommandAndCaptureOutput(terragruntOptions *options.TerragruntOptions, args ...string) (string, error) {
return RunShellCommandAndCaptureOutput(terragruntOptions, terragruntOptions.TerraformPath, args...)
}

// Run the specified shell command with the specified arguments. Connect the command's stdin, stdout, and stderr to
// the currently running app.
func RunShellCommand(terragruntOptions *options.TerragruntOptions, command string, args ...string) error {
Expand Down

0 comments on commit 35c25b4

Please sign in to comment.