Skip to content
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

feat: support FORCE_COLOR env variable #9274

Open
wants to merge 3 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
13 changes: 10 additions & 3 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
opts config.SkaffoldOptions
v string
defaultColor int
forceColors bool
forceColor bool
overwrite bool
interactive bool
timestamps bool
Expand Down Expand Up @@ -86,7 +86,7 @@ func NewSkaffoldCommand(out, errOut io.Writer) *cobra.Command {
// These are used for command completion and send debug messages on stderr.
if cmd.Name() != cobra.ShellCompRequestCmd && cmd.Name() != cobra.ShellCompNoDescRequestCmd {
instrumentation.SetCommand(cmd.Name())
out := output.GetWriter(context.Background(), out, defaultColor, forceColors, timestamps)
out := output.GetWriter(context.Background(), out, defaultColor, forceColor, timestamps)
cmd.Root().SetOut(out)
cmd.Root().SetErr(errOut)

Expand Down Expand Up @@ -205,7 +205,8 @@ func NewSkaffoldCommand(out, errOut io.Writer) *cobra.Command {
templates.ActsAsRootCommand(rootCmd, nil, groups...)
rootCmd.PersistentFlags().StringVarP(&v, "verbosity", "v", log.DefaultLogLevel.String(), fmt.Sprintf("Log level: one of %v", log.AllLevels))
rootCmd.PersistentFlags().IntVar(&defaultColor, "color", int(output.DefaultColorCode), "Specify the default output color in ANSI escape codes")
rootCmd.PersistentFlags().BoolVar(&forceColors, "force-colors", false, "Always print color codes (hidden)")
rootCmd.PersistentFlags().BoolVar(&forceColor, "force-colors", false, "Always print color codes (hidden)")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way I should deprecate this option?

rootCmd.PersistentFlags().BoolVar(&forceColor, "force-color", false, "Always print color codes")
rootCmd.PersistentFlags().BoolVar(&interactive, "interactive", true, "Allow user prompts for more information")
rootCmd.PersistentFlags().BoolVar(&update.EnableCheck, "update-check", true, "Check for a more recent version of Skaffold")
rootCmd.PersistentFlags().BoolVar(&timestamps, "timestamps", false, "Print timestamps in logs")
Expand Down Expand Up @@ -244,6 +245,12 @@ func setEnvVariablesFromFile() {
// Each flag can also be set with an env variable whose name starts with `SKAFFOLD_`.
func setFlagsFromEnvVariables(rootCmd *cobra.Command) {
rootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if f.Name == "force-color" {
if val, present := os.LookupEnv("FORCE_COLOR"); present {
rootCmd.PersistentFlags().Set(f.Name, val)
}
}

envVar := FlagToEnvVarName(f)
if val, present := os.LookupEnv(envVar); present {
rootCmd.PersistentFlags().Set(f.Name, val)
Expand Down
10 changes: 10 additions & 0 deletions docs-v2/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ Env vars:
The following options can be passed to any command:

--color=34: Specify the default output color in ANSI escape codes
--force-color=false: Always print color codes
--interactive=true: Allow user prompts for more information
--timestamps=false: Print timestamps in logs
--update-check=true: Check for a more recent version of Skaffold
Expand All @@ -1116,6 +1117,15 @@ The following options can be passed to any command:

```

Env vars:

* `SKAFFOLD_COLOR` (same as `--color`)
* `SKAFFOLD_FORCE_COLOR` or `FORCE_COLOR` (same as `--force-color`)
* `SKAFFOLD_INTERACTIVE` (same as `--interactive`)
* `SKAFFOLD_TIMESTAMPS` (same as `--timestamps`)
* `SKAFFOLD_UPDATE_CHECK` (same as `--update-check`)
* `SKAFFOLD_VERBOSITY` (same as `--verbosity`)

### skaffold render

Generate rendered Kubernetes manifests
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/output/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ var DefaultColorCodes = []Color{
}

// SetupColors conditionally wraps the input `Writer` with a color enabled `Writer`.
func SetupColors(ctx context.Context, out io.Writer, defaultColor int, forceColors bool) io.Writer {
func SetupColors(ctx context.Context, out io.Writer, defaultColor int, forceColor bool) io.Writer {
_, isTerm := term.IsTerminal(out)
supportsColor, err := term.SupportsColor(ctx)
if err != nil {
log.Entry(context.TODO()).Debugf("error checking for color support: %v", err)
}

useColors := (isTerm && supportsColor) || forceColors
useColors := (isTerm && supportsColor) || forceColor
if useColors {
// Use EnableColorsStdout to enable use of color on Windows
useColors = false // value is updated if color-enablement is successful
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func (s skaffoldWriter) Write(p []byte) (int, error) {
return written, nil
}

func GetWriter(ctx context.Context, out io.Writer, defaultColor int, forceColors bool, timestamps bool) io.Writer {
func GetWriter(ctx context.Context, out io.Writer, defaultColor int, forceColor bool, timestamps bool) io.Writer {
if _, isSW := out.(skaffoldWriter); isSW {
return out
}

return skaffoldWriter{
MainWriter: SetupColors(ctx, out, defaultColor, forceColors),
MainWriter: SetupColors(ctx, out, defaultColor, forceColor),
EventWriter: eventV2.NewLogger(constants.DevLoop, "-1"),
timestamps: timestamps,
}
Expand Down