Skip to content

Commit

Permalink
bt: add color flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Sep 19, 2024
1 parent 4dc4641 commit 74c7203
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func program(args []string) int {
opt := getoptions.New()
opt.Self("", "Terraform build system built as a no lock-in wrapper")
opt.Bool("quiet", false, opt.GetEnv("QUIET"))
opt.String("color", "auto", opt.Description("show colored output"), opt.ValidValues("always", "auto", "never"))
opt.SetUnknownMode(getoptions.Pass)

configCMD(ctx, opt)
Expand Down Expand Up @@ -76,7 +77,7 @@ func program(args []string) int {
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
if errors.Is(err, getoptions.ErrorParsing) {
fmt.Fprintf(os.Stderr, "\n"+opt.Help())
fmt.Fprintf(os.Stderr, "\n%s", opt.Help())
}
return 1
}
Expand Down
1 change: 1 addition & 0 deletions bt/stack/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func generateDAG(opt *getoptions.GetOpt, id string, cfg *sconfig.Config, normal
nopt.Bool("show", opt.Value("show").(bool))
nopt.Bool("lock", opt.Value("lock").(bool))
nopt.String("profile", opt.Value("profile").(string))
nopt.String("color", opt.Value("color").(string))
nopt.String("ws", ws)
nopt.StringSlice("replace", 1, 99)
nopt.StringSlice("target", 1, 99)
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
parallelism := opt.Value("parallelism").(int)
ws := opt.Value("ws").(string)
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
Expand Down Expand Up @@ -67,7 +68,7 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
cmd := []string{cfg.TFProfile[cfg.Profile(profile)].BinaryName, "apply"}
cmd = append(cmd, "-parallelism", fmt.Sprintf("%d", parallelism))
cmd = append(cmd, "-input", planFile)
if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestApply(t *testing.T) {
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.String("ws", "dev")
opt.String("color", "auto")
err := applyRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestApply error: %s", err)
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestApply(t *testing.T) {
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.String("ws", "dev")
opt.String("color", "auto")
err := applyRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestApply error: %s", err)
Expand Down Expand Up @@ -124,6 +126,7 @@ func TestApply(t *testing.T) {
opt.Int("parallelism", 10)
opt.String("profile", "dev")
opt.String("ws", "dev")
opt.String("color", "auto")
err := applyRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestApply error: %s", err)
Expand Down Expand Up @@ -163,6 +166,7 @@ func TestApply(t *testing.T) {
opt.Int("parallelism", 10)
opt.String("profile", "prod")
opt.String("ws", "prod")
opt.String("color", "auto")
err := applyRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestApply error: %s", err)
Expand Down
1 change: 1 addition & 0 deletions bt/terraform/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestBuild(t *testing.T) {
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.String("ws", "")
opt.String("color", "auto")
opt.Bool("destroy", false)
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false)
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func initCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
Expand All @@ -43,7 +44,7 @@ func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
// }
cmd = append(cmd, "-backend-config", bb[0])
}
if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.String("profile", "default")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -80,6 +81,7 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.String("profile", "default")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -118,6 +120,7 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.String("profile", "dev")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -155,6 +158,7 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.String("profile", "prod")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
targets := opt.Value("target").([]string)
replacements := opt.Value("replace").([]string)
ws := opt.Value("ws").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
Expand Down Expand Up @@ -181,7 +182,7 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
for _, r := range replacements {
cmd = append(cmd, "-replace", r)
}
if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestPlan(t *testing.T) {
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false)
opt.String("ws", "")
opt.String("color", "auto")
opt.StringSlice("var", 1, 1)
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
Expand Down Expand Up @@ -95,6 +96,7 @@ func TestPlan(t *testing.T) {
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false)
opt.String("ws", "dev")
opt.String("color", "auto")
opt.StringSlice("var", 1, 1)
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
Expand Down Expand Up @@ -140,6 +142,7 @@ func TestPlan(t *testing.T) {
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false)
opt.String("ws", "dev")
opt.String("color", "auto")
opt.StringSlice("var", 1, 1)
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
Expand Down Expand Up @@ -185,6 +188,7 @@ func TestPlan(t *testing.T) {
opt.Bool("detailed-exitcode", false)
opt.Bool("ignore-cache", false)
opt.String("ws", "prod")
opt.String("color", "auto")
opt.StringSlice("var", 1, 1)
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/show_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) err
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
ws := opt.Value("ws").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
Expand All @@ -48,7 +49,7 @@ func showPlanRun(ctx context.Context, opt *getoptions.GetOpt, args []string) err
} else {
cmd = append(cmd, fmt.Sprintf(".tf.plan-%s", ws))
}
if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/varfile_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func varFileCMDRun(fn VarFileCMDer, cmd ...string) getoptions.CommandFn {
profile := opt.Value("profile").(string)
varFiles := opt.Value("var-file").([]string)
ws := opt.Value("ws").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
Expand Down Expand Up @@ -94,7 +95,7 @@ func varFileCMDRun(fn VarFileCMDer, cmd ...string) getoptions.CommandFn {
for _, v := range varFiles {
cmd = append(cmd, "-var-file", v)
}
if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, fn.cmdFunction(ws)...)
Expand Down
6 changes: 4 additions & 2 deletions bt/terraform/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func workspaceSelectCMD(ctx context.Context, parent *getoptions.GetOpt) *getopti
// TODO: Allow using both the arg and the --ws flag
func workspaceSelectRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "ERROR: missing <workspace-name>\n")
fmt.Fprintf(os.Stderr, "%s", opt.Help(getoptions.HelpSynopsis))
Expand All @@ -80,7 +81,7 @@ func workspaceSelectRun(ctx context.Context, opt *getoptions.GetOpt, args []stri

cmd := []string{cfg.TFProfile[cfg.Profile(profile)].BinaryName, "workspace", "select", wsName}

if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down Expand Up @@ -135,11 +136,12 @@ func workspaceNewRun(ctx context.Context, opt *getoptions.GetOpt, args []string)
func workspaceFn(cmd ...string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)

cfg := config.ConfigFromContext(ctx)
LogConfig(cfg, profile)

if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down
3 changes: 2 additions & 1 deletion bt/terraform/ws_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func wsCMDRun(cmd ...string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
profile := opt.Value("profile").(string)
ws := opt.Value("ws").(string)
color := opt.Value("color").(string)
dryRun := false
if opt.Value("dry-run") != nil {
dryRun = opt.Value("dry-run").(bool)
Expand Down Expand Up @@ -44,7 +45,7 @@ func wsCMDRun(cmd ...string) getoptions.CommandFn {
planFile = fmt.Sprintf(".tf.plan-%s", ws)
}

if !isatty.IsTerminal(os.Stdout.Fd()) {
if color == "never" || (color == "auto" && !isatty.IsTerminal(os.Stdout.Fd())) {
cmd = append(cmd, "-no-color")
}
cmd = append(cmd, args...)
Expand Down

0 comments on commit 74c7203

Please sign in to comment.