Skip to content

add colors flag #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 85 additions & 85 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
o.configureApply(&c)
}

args := []string{"apply", "-no-color", "-auto-approve", "-input=false"}
args := []string{"apply", "-auto-approve", "-input=false"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
34 changes: 34 additions & 0 deletions tfexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
}

func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]string, args ...string) *exec.Cmd {
if !tf.colors {
args = addColorFlag(args)
}

cmd := exec.CommandContext(ctx, tf.execPath, args...)

cmd.Env = tf.buildEnv(mergeEnv)
Expand All @@ -190,6 +194,36 @@ func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]
return cmd
}

func addColorFlag(args []string) []string {
found := false
insertIndex := 0
for i, a := range args {
if strings.HasPrefix(a, "-") && insertIndex == 0 {
insertIndex = i
}

if a == "-no-color" {
found = true
}
}
if insertIndex == 0 {
insertIndex = len(args)
}
if !found {
args = insert(args, insertIndex, "-no-color")
}
return args
}

func insert(a []string, index int, value string) []string {
if len(a) == index {
return append(a, value)
}
a = append(a[:index+1], a[index:]...)
a[index] = value
return a
}

func (tf *Terraform) runTerraformCmdJSON(ctx context.Context, cmd *exec.Cmd, v interface{}) error {
var outbuf = bytes.Buffer{}
cmd.Stdout = mergeWriters(cmd.Stdout, &outbuf)
Expand Down
2 changes: 1 addition & 1 deletion tfexec/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (tf *Terraform) destroyCmd(ctx context.Context, opts ...DestroyOption) (*ex
o.configureDestroy(&c)
}

args := []string{"destroy", "-no-color", "-auto-approve", "-input=false"}
args := []string{"destroy", "-auto-approve", "-input=false"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
2 changes: 1 addition & 1 deletion tfexec/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (tf *Terraform) formatCmd(ctx context.Context, args []string, opts ...Forma
o.configureFormat(&c)
}

args = append([]string{"fmt", "-no-color"}, args...)
args = append([]string{"fmt"}, args...)

if c.recursive {
args = append(args, "-recursive")
Expand Down
2 changes: 1 addition & 1 deletion tfexec/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (tf *Terraform) getCmd(ctx context.Context, opts ...GetCmdOption) (*exec.Cm
o.configureGet(&c)
}

args := []string{"get", "-no-color"}
args := []string{"get"}

args = append(args, "-update="+fmt.Sprint(c.update))

Expand Down
4 changes: 4 additions & 0 deletions tfexec/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestGraphCmd_v013(t *testing.T) {

assertCmd(t, []string{
"graph",
"-no-color",
}, nil, graphCmd)
})

Expand All @@ -40,6 +41,7 @@ func TestGraphCmd_v013(t *testing.T) {
assertCmd(t, []string{
"graph",
"teststate",
"-no-color",
"-draw-cycles",
"-type=output",
}, nil, graphCmd)
Expand All @@ -62,6 +64,7 @@ func TestGraphCmd_v1(t *testing.T) {

assertCmd(t, []string{
"graph",
"-no-color",
}, nil, graphCmd)
})

Expand All @@ -73,6 +76,7 @@ func TestGraphCmd_v1(t *testing.T) {

assertCmd(t, []string{
"graph",
"-no-color",
"-plan=teststate",
"-draw-cycles",
"-type=output",
Expand Down
2 changes: 1 addition & 1 deletion tfexec/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (tf *Terraform) importCmd(ctx context.Context, address, id string, opts ...
o.configureImport(&c)
}

args := []string{"import", "-no-color", "-input=false"}
args := []string{"import", "-input=false"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
2 changes: 1 addition & 1 deletion tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
o.configureInit(&c)
}

args := []string{"init", "-no-color", "-input=false"}
args := []string{"init", "-force-copy", "-input=false"}

// string opts: only pass if set
if c.fromModule != "" {
Expand Down
2 changes: 2 additions & 0 deletions tfexec/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestInitCmd_v012(t *testing.T) {
assertCmd(t, []string{
"init",
"-no-color",
"-force-copy",
"-input=false",
"-lock-timeout=0s",
"-backend=true",
Expand All @@ -53,6 +54,7 @@ func TestInitCmd_v012(t *testing.T) {
assertCmd(t, []string{
"init",
"-no-color",
"-force-copy",
"-input=false",
"-from-module=testsource",
"-lock-timeout=999s",
Expand Down
2 changes: 1 addition & 1 deletion tfexec/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (tf *Terraform) outputCmd(ctx context.Context, opts ...OutputOption) *exec.
o.configureOutput(&c)
}

args := []string{"output", "-no-color", "-json"}
args := []string{"output", "-json"}

// string opts: only pass if set
if c.state != "" {
Expand Down
2 changes: 1 addition & 1 deletion tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (tf *Terraform) planCmd(ctx context.Context, opts ...PlanOption) (*exec.Cmd
o.configurePlan(&c)
}

args := []string{"plan", "-no-color", "-input=false", "-detailed-exitcode"}
args := []string{"plan", "-input=false", "-detailed-exitcode"}

// string opts: only pass if set
if c.lockTimeout != "" {
Expand Down
2 changes: 2 additions & 0 deletions tfexec/providers_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestProvidersLockCmd(t *testing.T) {
assertCmd(t, []string{
"providers",
"lock",
"-no-color",
}, nil, lockCmd)
})

Expand All @@ -33,6 +34,7 @@ func TestProvidersLockCmd(t *testing.T) {
assertCmd(t, []string{
"providers",
"lock",
"-no-color",
"-fs-mirror=test",
"-net-mirror=test",
"-platform=linux_amd64",
Expand Down
2 changes: 1 addition & 1 deletion tfexec/providers_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (tf *Terraform) ProvidersSchema(ctx context.Context) (*tfjson.ProviderSchem
}

func (tf *Terraform) providersSchemaCmd(ctx context.Context, args ...string) *exec.Cmd {
allArgs := []string{"providers", "schema", "-json", "-no-color"}
allArgs := []string{"providers", "schema", "-json"}
allArgs = append(allArgs, args...)

return tf.buildTerraformCmd(ctx, nil, allArgs...)
Expand Down
2 changes: 1 addition & 1 deletion tfexec/providers_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestProvidersSchemaCmd(t *testing.T) {
assertCmd(t, []string{
"providers",
"schema",
"-json",
"-no-color",
"-json",
}, nil, schemaCmd)
}
2 changes: 1 addition & 1 deletion tfexec/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (tf *Terraform) refreshCmd(ctx context.Context, opts ...RefreshCmdOption) (
o.configureRefresh(&c)
}

args := []string{"refresh", "-no-color", "-input=false"}
args := []string{"refresh", "-input=false"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
1 change: 0 additions & 1 deletion tfexec/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ func (tf *Terraform) showCmd(ctx context.Context, jsonOutput bool, mergeEnv map[
if jsonOutput {
allArgs = append(allArgs, "-json")
}
allArgs = append(allArgs, "-no-color")
allArgs = append(allArgs, args...)

return tf.buildTerraformCmd(ctx, mergeEnv, allArgs...)
Expand Down
8 changes: 4 additions & 4 deletions tfexec/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestShowCmd(t *testing.T) {

assertCmd(t, []string{
"show",
"-json",
"-no-color",
"-json",
}, nil, showCmd)
}

Expand All @@ -43,8 +43,8 @@ func TestShowStateFileCmd(t *testing.T) {

assertCmd(t, []string{
"show",
"-json",
"-no-color",
"-json",
"statefilepath",
}, nil, showCmd)
}
Expand All @@ -64,8 +64,8 @@ func TestShowPlanFileCmd(t *testing.T) {

assertCmd(t, []string{
"show",
"-json",
"-no-color",
"-json",
"planfilepath",
}, nil, showCmd)
}
Expand All @@ -85,7 +85,7 @@ func TestShowPlanFileRawCmd(t *testing.T) {

assertCmd(t, []string{
"show",
"-no-color",
"planfilepath",
"-no-color",
}, nil, showCmd)
}
2 changes: 1 addition & 1 deletion tfexec/state_mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (tf *Terraform) stateMvCmd(ctx context.Context, source string, destination
o.configureStateMv(&c)
}

args := []string{"state", "mv", "-no-color"}
args := []string{"state", "mv"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
1 change: 1 addition & 0 deletions tfexec/state_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestStatePull(t *testing.T) {
assertCmd(t, []string{
"state",
"pull",
"-no-color",
}, nil, statePullCmd)
})
}
2 changes: 2 additions & 0 deletions tfexec/state_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestStatePushCmd(t *testing.T) {
assertCmd(t, []string{
"state",
"push",
"-no-color",
"-lock=false",
"-lock-timeout=0s",
"testpath",
Expand All @@ -39,6 +40,7 @@ func TestStatePushCmd(t *testing.T) {
assertCmd(t, []string{
"state",
"push",
"-no-color",
"-force",
"-lock=true",
"-lock-timeout=10s",
Expand Down
2 changes: 1 addition & 1 deletion tfexec/state_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (tf *Terraform) stateRmCmd(ctx context.Context, address string, opts ...Sta
o.configureStateRm(&c)
}

args := []string{"state", "rm", "-no-color"}
args := []string{"state", "rm"}

// string opts: only pass if set
if c.backup != "" {
Expand Down
2 changes: 1 addition & 1 deletion tfexec/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (tf *Terraform) taintCmd(ctx context.Context, address string, opts ...Taint
o.configureTaint(&c)
}

args := []string{"taint", "-no-color"}
args := []string{"taint"}

if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
Expand Down
11 changes: 9 additions & 2 deletions tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package tfexec
import (
"context"
"fmt"
"github.com/hashicorp/go-version"
"io"
"io/ioutil"
"log"
"os"
"sync"

"github.com/hashicorp/go-version"
)

type printfer interface {
Expand Down Expand Up @@ -47,6 +46,7 @@ type Terraform struct {
disablePluginTLS bool
skipProviderVerify bool
env map[string]string
colors bool

stdout io.Writer
stderr io.Writer
Expand Down Expand Up @@ -92,6 +92,7 @@ func NewTerraform(workingDir string, execPath string) (*Terraform, error) {
workingDir: workingDir,
env: nil, // explicit nil means copy os.Environ
logger: log.New(ioutil.Discard, "", 0),
colors: false,
}

return &tf, nil
Expand Down Expand Up @@ -213,6 +214,12 @@ func (tf *Terraform) SetSkipProviderVerify(skip bool) error {
return nil
}

// SetColor enables ansi colors for
// Terraform CLI execution.
func (tf *Terraform) SetColor(c bool) {
tf.colors = c
}

// WorkingDir returns the working directory for Terraform.
func (tf *Terraform) WorkingDir() string {
return tf.workingDir
Expand Down
2 changes: 2 additions & 0 deletions tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ func TestCheckpointDisablePropagation_v012(t *testing.T) {
assertCmd(t, []string{
"init",
"-no-color",
"-force-copy",
"-input=false",
"-lock-timeout=0s",
"-backend=true",
Expand Down Expand Up @@ -726,6 +727,7 @@ func TestCheckpointDisablePropagation_v012(t *testing.T) {
assertCmd(t, []string{
"init",
"-no-color",
"-force-copy",
"-input=false",
"-lock-timeout=0s",
"-backend=true",
Expand Down
2 changes: 1 addition & 1 deletion tfexec/untaint.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (tf *Terraform) untaintCmd(ctx context.Context, address string, opts ...Unt
o.configureUntaint(&c)
}

args := []string{"untaint", "-no-color"}
args := []string{"untaint"}

if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
Expand Down
2 changes: 1 addition & 1 deletion tfexec/upgrade012.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (tf *Terraform) upgrade012Cmd(ctx context.Context, opts ...Upgrade012Option
o.configureUpgrade012(&c)
}

args := []string{"0.12upgrade", "-no-color", "-yes"}
args := []string{"0.12upgrade", "-yes"}

// boolean opts: only pass if set
if c.force {
Expand Down
2 changes: 1 addition & 1 deletion tfexec/upgrade013.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (tf *Terraform) upgrade013Cmd(ctx context.Context, opts ...Upgrade013Option
o.configureUpgrade013(&c)
}

args := []string{"0.13upgrade", "-no-color", "-yes"}
args := []string{"0.13upgrade", "-yes"}

// optional positional argument
if c.dir != "" {
Expand Down
Loading