Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for handling -no-color and -var options #933

Merged
merged 1 commit into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions modules/terraform/apply_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terraform

import (
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -201,3 +202,28 @@ func TestTgApplyUseLockNoError(t *testing.T) {
// make sure -lock CLI option is passed down correctly
require.Contains(t, out, "-lock=true")
}

func TestApplyWithPlanFile(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name())
require.NoError(t, err)
planFilePath := filepath.Join(testFolder, "plan.out")

options := &Options{
TerraformDir: testFolder,
Vars: map[string]interface{}{
"cnt": 1,
},
NoColor: true,
PlanFilePath: planFilePath,
}
_, err = InitAndPlanE(t, options)
require.NoError(t, err)
require.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath)

out, err := ApplyE(t, options)
require.NoError(t, err)
require.Contains(t, out, "1 added, 0 changed, 0 destroyed.")
require.NotRegexp(t, `\[\d*m`, out, "Output should not contain color escape codes")
}
4 changes: 0 additions & 4 deletions modules/terraform/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ var commandsWithParallelism = []string{

// GetCommonOptions extracts commons terraform options
func GetCommonOptions(options *Options, args ...string) (*Options, []string) {
if options.NoColor && !collections.ListContains(args, "-no-color") {
args = append(args, "-no-color")
}

if options.TerraformBinary == "" {
options.TerraformBinary = "terraform"
}
Expand Down
15 changes: 13 additions & 2 deletions modules/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ func FormatArgs(options *Options, args ...string) []string {
lockSupported := collections.ListContains(TerraformCommandsWithLockSupport, commandType)
planFileSupported := collections.ListContains(TerraformCommandsWithPlanFileSupport, commandType)

// Include -var and -var-file flags unless we're running 'apply' with a plan file
includeVars := !(commandType == "apply" && len(options.PlanFilePath) > 0)

terraformArgs = append(terraformArgs, args...)
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)

if includeVars {
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
}

terraformArgs = append(terraformArgs, FormatTerraformArgs("-target", options.Targets)...)

if options.NoColor {
terraformArgs = append(terraformArgs, "-no-color")
}

if lockSupported {
// If command supports locking, handle lock arguments
terraformArgs = append(terraformArgs, FormatTerraformLockAsArgs(options.Lock, options.LockTimeout)...)
Expand Down