From aa9fbdf2418cfe4fcb594b70337e483c72ea91af Mon Sep 17 00:00:00 2001 From: Christian Volk Date: Thu, 4 Nov 2021 18:08:52 +0100 Subject: [PATCH] fix(terraformExecute): disable colored console output (#3231) * fix(terraformExecute): disable colored console output * fix * forgot a case * fix test * fix test Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> --- cmd/terraformExecute.go | 10 +++++++--- cmd/terraformExecute_test.go | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/terraformExecute.go b/cmd/terraformExecute.go index e1b3acb10..2389bc490 100644 --- a/cmd/terraformExecute.go +++ b/cmd/terraformExecute.go @@ -52,20 +52,24 @@ func runTerraformExecute(config *terraformExecuteOptions, telemetryData *telemet args := []string{} - if config.Command == "apply" { + if piperutils.ContainsString([]string{"apply", "destroy"}, config.Command) { args = append(args, "-auto-approve") } - if (config.Command == "apply" || config.Command == "plan") && config.TerraformSecrets != "" { + if piperutils.ContainsString([]string{"apply", "plan"}, config.Command) && config.TerraformSecrets != "" { args = append(args, fmt.Sprintf("-var-file=%s", config.TerraformSecrets)) } + if piperutils.ContainsString([]string{"init", "validate", "plan", "apply", "destroy"}, config.Command) { + args = append(args, "-no-color") + } + if config.AdditionalArgs != nil { args = append(args, config.AdditionalArgs...) } if config.Init { - err := runTerraform(utils, "init", []string{}, config.GlobalOptions) + err := runTerraform(utils, "init", []string{"-no-color"}, config.GlobalOptions) if err != nil { return err diff --git a/cmd/terraformExecute_test.go b/cmd/terraformExecute_test.go index ed6288ac6..f9433a8db 100644 --- a/cmd/terraformExecute_test.go +++ b/cmd/terraformExecute_test.go @@ -31,38 +31,38 @@ func TestRunTerraformExecute(t *testing.T) { { terraformExecuteOptions{ Command: "apply", - }, []string{"apply", "-auto-approve"}, []string{}, + }, []string{"apply", "-auto-approve", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "apply", TerraformSecrets: "/tmp/test", - }, []string{"apply", "-auto-approve", "-var-file=/tmp/test"}, []string{}, + }, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "plan", - }, []string{"plan"}, []string{}, + }, []string{"plan", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "plan", TerraformSecrets: "/tmp/test", - }, []string{"plan", "-var-file=/tmp/test"}, []string{}, + }, []string{"plan", "-var-file=/tmp/test", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "plan", TerraformSecrets: "/tmp/test", AdditionalArgs: []string{"-arg1"}, - }, []string{"plan", "-var-file=/tmp/test", "-arg1"}, []string{}, + }, []string{"plan", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{}, }, { terraformExecuteOptions{ Command: "apply", TerraformSecrets: "/tmp/test", AdditionalArgs: []string{"-arg1"}, - }, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-arg1"}, []string{}, + }, []string{"apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{}, }, { terraformExecuteOptions{ @@ -70,32 +70,32 @@ func TestRunTerraformExecute(t *testing.T) { TerraformSecrets: "/tmp/test", AdditionalArgs: []string{"-arg1"}, GlobalOptions: []string{"-chgdir=src"}, - }, []string{"-chgdir=src", "apply", "-auto-approve", "-var-file=/tmp/test", "-arg1"}, []string{}, + }, []string{"-chgdir=src", "apply", "-auto-approve", "-var-file=/tmp/test", "-no-color", "-arg1"}, []string{}, }, { terraformExecuteOptions{ Command: "apply", Init: true, - }, []string{"apply", "-auto-approve"}, []string{}, + }, []string{"apply", "-auto-approve", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "apply", GlobalOptions: []string{"-chgdir=src"}, Init: true, - }, []string{"-chgdir=src", "apply", "-auto-approve"}, []string{}, + }, []string{"-chgdir=src", "apply", "-auto-approve", "-no-color"}, []string{}, }, { terraformExecuteOptions{ Command: "apply", CliConfigFile: ".pipeline/.terraformrc", - }, []string{"apply", "-auto-approve"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"}, + }, []string{"apply", "-auto-approve", "-no-color"}, []string{"TF_CLI_CONFIG_FILE=.pipeline/.terraformrc"}, }, { terraformExecuteOptions{ Command: "plan", Workspace: "any-workspace", - }, []string{"plan"}, []string{"TF_WORKSPACE=any-workspace"}, + }, []string{"plan", "-no-color"}, []string{"TF_WORKSPACE=any-workspace"}, }, } @@ -118,7 +118,7 @@ func TestRunTerraformExecute(t *testing.T) { assert.NoError(t, err) if config.Init { - assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: append(config.GlobalOptions, "init")}, utils.Calls[0]) + assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: append(config.GlobalOptions, "init", "-no-color")}, utils.Calls[0]) assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[1]) } else { assert.Equal(t, mock.ExecCall{Exec: "terraform", Params: test.expectedArgs}, utils.Calls[0])