Skip to content

Commit

Permalink
Merge pull request #933 from tolgaio/master
Browse files Browse the repository at this point in the history
fix for handling -no-color and -var options
  • Loading branch information
brikis98 authored Jun 21, 2021
2 parents eb200a4 + 8969ed9 commit 7e38458
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
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

0 comments on commit 7e38458

Please sign in to comment.