diff --git a/command/meta.go b/command/meta.go index 186736cfa..48bb3c4ae 100644 --- a/command/meta.go +++ b/command/meta.go @@ -440,14 +440,18 @@ func (m *Meta) process(args []string) []string { // Set colorization m.color = m.Color - for i, v := range args { + i := 0 // output index + for _, v := range args { if v == "-no-color" { m.color = false m.Color = false - args = append(args[:i], args[i+1:]...) - break + } else { + // copy and increment index + args[i] = v + i++ } } + args = args[:i] // Set the UI m.oldUi = m.Ui diff --git a/command/meta_test.go b/command/meta_test.go index 84f2a4840..0de9cf79f 100644 --- a/command/meta_test.go +++ b/command/meta_test.go @@ -56,6 +56,21 @@ func TestMetaColorize(t *testing.T) { if !m.Colorize().Disable { t.Fatal("should be disabled") } + + // Test disable #2 + // Verify multiple -no-color options are removed from args slice. + // E.g. an additional -no-color arg could be added by TF_CLI_ARGS. + m = new(Meta) + m.Color = true + args = []string{"foo", "-no-color", "bar", "-no-color"} + args2 = []string{"foo", "bar"} + args = m.process(args) + if !reflect.DeepEqual(args, args2) { + t.Fatalf("bad: %#v", args) + } + if !m.Colorize().Disable { + t.Fatal("should be disabled") + } } func TestMetaInputMode(t *testing.T) {