@@ -181,10 +181,10 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
181
181
182
182
func (tf * Terraform ) buildTerraformCmd (ctx context.Context , mergeEnv map [string ]string , args ... string ) * exec.Cmd {
183
183
if ! tf .colors {
184
- args = append (args , "-no-color" )
184
+ args = addColorFlag (args )
185
185
}
186
186
187
- cmd := exec .Command ( tf .execPath , args ... )
187
+ cmd := exec .CommandContext ( ctx , tf .execPath , args ... )
188
188
189
189
cmd .Env = tf .buildEnv (mergeEnv )
190
190
cmd .Dir = tf .workingDir
@@ -194,6 +194,36 @@ func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]
194
194
return cmd
195
195
}
196
196
197
+ func addColorFlag (args []string ) []string {
198
+ found := false
199
+ insertIndex := 0
200
+ for i , a := range args {
201
+ if strings .HasPrefix (a , "-" ) && insertIndex == 0 {
202
+ insertIndex = i
203
+ }
204
+
205
+ if a == "-no-color" {
206
+ found = true
207
+ }
208
+ }
209
+ if insertIndex == 0 {
210
+ insertIndex = len (args )
211
+ }
212
+ if ! found {
213
+ args = insert (args , insertIndex , "-no-color" )
214
+ }
215
+ return args
216
+ }
217
+
218
+ func insert (a []string , index int , value string ) []string {
219
+ if len (a ) == index {
220
+ return append (a , value )
221
+ }
222
+ a = append (a [:index + 1 ], a [index :]... )
223
+ a [index ] = value
224
+ return a
225
+ }
226
+
197
227
func (tf * Terraform ) runTerraformCmdJSON (ctx context.Context , cmd * exec.Cmd , v interface {}) error {
198
228
var outbuf = bytes.Buffer {}
199
229
cmd .Stdout = mergeWriters (cmd .Stdout , & outbuf )
0 commit comments