Skip to content

Commit 3512571

Browse files
authored
Merge pull request #25847 from zachwhaley/fix-tf-cli-args-no-color
Fix error when multiple -no-color arguments are used
2 parents 19efd7b + af8d5a6 commit 3512571

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

command/meta.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,18 @@ func (m *Meta) process(args []string) []string {
440440

441441
// Set colorization
442442
m.color = m.Color
443-
for i, v := range args {
443+
i := 0 // output index
444+
for _, v := range args {
444445
if v == "-no-color" {
445446
m.color = false
446447
m.Color = false
447-
args = append(args[:i], args[i+1:]...)
448-
break
448+
} else {
449+
// copy and increment index
450+
args[i] = v
451+
i++
449452
}
450453
}
454+
args = args[:i]
451455

452456
// Set the UI
453457
m.oldUi = m.Ui

command/meta_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ func TestMetaColorize(t *testing.T) {
5656
if !m.Colorize().Disable {
5757
t.Fatal("should be disabled")
5858
}
59+
60+
// Test disable #2
61+
// Verify multiple -no-color options are removed from args slice.
62+
// E.g. an additional -no-color arg could be added by TF_CLI_ARGS.
63+
m = new(Meta)
64+
m.Color = true
65+
args = []string{"foo", "-no-color", "bar", "-no-color"}
66+
args2 = []string{"foo", "bar"}
67+
args = m.process(args)
68+
if !reflect.DeepEqual(args, args2) {
69+
t.Fatalf("bad: %#v", args)
70+
}
71+
if !m.Colorize().Disable {
72+
t.Fatal("should be disabled")
73+
}
5974
}
6075

6176
func TestMetaInputMode(t *testing.T) {

0 commit comments

Comments
 (0)