From 6f781461d49fa1ccb033e4980d440240b7f0dcc2 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 24 Jan 2024 16:07:02 -0600 Subject: [PATCH 1/3] Support `FORCE_COLOR` env variable --- cmd/skaffold/app/cmd/cmd.go | 13 ++++++++++--- pkg/skaffold/output/color.go | 4 ++-- pkg/skaffold/output/output.go | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cmd/skaffold/app/cmd/cmd.go b/cmd/skaffold/app/cmd/cmd.go index 2be5da683f9..0b7e2a0217e 100644 --- a/cmd/skaffold/app/cmd/cmd.go +++ b/cmd/skaffold/app/cmd/cmd.go @@ -48,7 +48,7 @@ var ( opts config.SkaffoldOptions v string defaultColor int - forceColors bool + forceColor bool overwrite bool interactive bool timestamps bool @@ -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) @@ -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)") + 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(×tamps, "timestamps", false, "Print timestamps in logs") @@ -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) diff --git a/pkg/skaffold/output/color.go b/pkg/skaffold/output/color.go index 8b34388e6d7..5b343c7edbc 100644 --- a/pkg/skaffold/output/color.go +++ b/pkg/skaffold/output/color.go @@ -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 diff --git a/pkg/skaffold/output/output.go b/pkg/skaffold/output/output.go index d3a3806a263..76937a80c51 100644 --- a/pkg/skaffold/output/output.go +++ b/pkg/skaffold/output/output.go @@ -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, } From 7dfab3db338cc54740c1aeefbf21061757e7ff2b Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 24 Jan 2024 16:17:27 -0600 Subject: [PATCH 2/3] Document global ENV variables --- docs-v2/content/en/docs/references/cli/_index.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs-v2/content/en/docs/references/cli/_index.md b/docs-v2/content/en/docs/references/cli/_index.md index e7287f6c5c6..9581440869c 100644 --- a/docs-v2/content/en/docs/references/cli/_index.md +++ b/docs-v2/content/en/docs/references/cli/_index.md @@ -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 @@ -1116,6 +1117,15 @@ The following options can be passed to any command: ``` +Env vars: + +* `SKAFFOLD_COLOR` (same as `--color`) +* `SKAFFOLD_INTERACTIVE` (same as `--interactive`) +* `SKAFFOLD_TIMESTAMPS` (same as `--timestamps`) +* `SKAFFOLD_UPDATE_CHECK` (same as `--update-check`) +* `SKAFFOLD_VERBOSITY` (same as `--verbosity`) +* `SKAFFOLD_FORCE_COLOR` or `FORCE_COLOR` (same as `--force-color`) + ### skaffold render Generate rendered Kubernetes manifests From 799de9b60dddd6ea590b3cb2ee62db534707ba09 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 24 Jan 2024 16:25:25 -0600 Subject: [PATCH 3/3] Order ENV variables --- docs-v2/content/en/docs/references/cli/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs-v2/content/en/docs/references/cli/_index.md b/docs-v2/content/en/docs/references/cli/_index.md index 9581440869c..e25fdde0b3b 100644 --- a/docs-v2/content/en/docs/references/cli/_index.md +++ b/docs-v2/content/en/docs/references/cli/_index.md @@ -1120,11 +1120,11 @@ 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_FORCE_COLOR` or `FORCE_COLOR` (same as `--force-color`) ### skaffold render