From b69876e563401eba298b4aaeb6501743e1a78e0c Mon Sep 17 00:00:00 2001 From: Can Sentay Date: Sat, 28 Nov 2026 20:48:21 +0300 Subject: [PATCH] fix: accept the color modes of Helm 4 in --color Helm 4 has a global --color flag that takes never, auto or always. It parses that flag from the whole command line before it starts a plugin, and hands a --color= it accepted on to the plugin unchanged. helm-diff then failed, because its --color only took a bool: $ helm diff upgrade --color=always my-release ./chart Error: invalid argument "always" for "--color" flag: strconv.ParseBool: parsing "always": invalid syntax The same happened with Helm's own `helm --color=never diff ...`. A bare --color followed by another argument never reaches helm-diff on Helm 4: Helm reads that argument as the mode and rejects it. Let --color also take always, never and auto. It still works without a value and with true or false. Document the Helm 4 behaviour in the README. --- README.md | 27 ++++++++++++++++++------ cmd/root.go | 46 ++++++++++++++++++++++++++++++++++++++--- cmd/root_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 cmd/root_test.go diff --git a/README.md b/README.md index b755b45f..3bb48cea 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ Available Commands: Flags: --allow-unreleased enables diffing of releases that are not yet deployed via Helm -a, --api-versions stringArray Kubernetes api versions used for Capabilities.APIVersions - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" -C, --context int output NUM lines of context around changes (default -1) --detailed-exitcode return a non-zero exit code when there are changes --devel use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored. @@ -336,6 +336,21 @@ rules: A kind that is missing from the role makes the diff fail on that resource, so the set has to cover everything the chart renders — including the kinds it only renders under some values. +### Colors with Helm 4 + +Helm 4 has a global `--color` flag of its own that takes a value (`never`, `auto` or `always`), and it reads that flag before it starts the plugin. A bare `--color` followed by another argument is therefore read by Helm as `--color ` and rejected: + +```console +$ helm diff upgrade --color -f values.yaml my-release ./chart +level=WARN msg="command failed" error="invalid color mode \"-f\": must be one of: never, auto, always" +``` + +Give the value after an `=` instead. Helm passes `--color=always`, `--color=never` and `--color=auto` on to helm-diff, which accepts them as well. `HELM_DIFF_COLOR=true` works with any Helm version. + +```shell +helm diff upgrade --color=always -f values.yaml my-release ./chart +``` + ## Commands: ### local: @@ -392,7 +407,7 @@ Flags: -f, --values valueFiles specify values in a YAML file (can specify multiple) (default []) Global Flags: - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" --no-color remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" ``` @@ -510,7 +525,7 @@ Flags: --version string specify the exact chart version to use. If this is not specified, the latest version is used Global Flags: - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" --no-color remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" ``` @@ -551,7 +566,7 @@ Flags: -q, --suppress-secrets suppress secrets in the output Global Flags: - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" --no-color remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" ``` @@ -597,7 +612,7 @@ Flags: -q, --suppress-secrets suppress secrets in the output Global Flags: - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" --no-color remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" ``` @@ -637,7 +652,7 @@ Flags: -q, --suppress-secrets suppress secrets in the output Global Flags: - --color color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" + --color color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" --no-color remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not "dumb" ``` diff --git a/cmd/root.go b/cmd/root.go index 1f4e032a..50aebfb6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "errors" "os" "strconv" "strings" @@ -34,6 +35,7 @@ The Helm Diff Plugin // New creates a new cobra client func New() *cobra.Command { chartCommand := newChartCommand() + var color colorFlag cmd := &cobra.Command{ Use: "diff", @@ -46,8 +48,7 @@ func New() *cobra.Command { var fc *bool if cmd.Flags().Changed("color") { - v, _ := cmd.Flags().GetBool("color") - fc = &v + fc = color.value } else { v, err := strconv.ParseBool(os.Getenv("HELM_DIFF_COLOR")) if err == nil { @@ -85,7 +86,8 @@ func New() *cobra.Command { // add no-color as global flag cmd.PersistentFlags().Bool("no-color", false, "remove colors from the output. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not \"dumb\"") - cmd.PersistentFlags().Bool("color", false, "color output. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not \"dumb\"") + cmd.PersistentFlags().Var(&color, "color", "color output. Also accepts always, never and auto, the values of the global --color flag of Helm 4. You can control the value for this flag via HELM_DIFF_COLOR=[true|false]. If both --no-color and --color are unspecified, coloring enabled only when the stdout is a term and TERM is not \"dumb\"") + cmd.PersistentFlags().Lookup("color").NoOptDefVal = envTrue // add flagset from chartCommand cmd.Flags().AddFlagSet(chartCommand.Flags()) cmd.AddCommand(newVersionCmd(), chartCommand) @@ -99,3 +101,41 @@ func New() *cobra.Command { cmd.SetHelpCommand(&cobra.Command{}) // Disable the help command return cmd } + +// colorFlag is the value of the --color flag. Besides a bool, it accepts the +// values of the global --color flag of Helm 4, which Helm 4 passes on to the +// plugin when they are given as --color=. +type colorFlag struct { + // value is nil when the terminal decides. + value *bool +} + +func (c *colorFlag) Set(s string) error { + switch s { + case "always": + s = envTrue + case "never": + s = envFalse + case "auto": + c.value = nil + return nil + } + v, err := strconv.ParseBool(s) + if err != nil { + return errors.New(`must be a bool or one of "always", "never", "auto"`) + } + c.value = &v + return nil +} + +func (c *colorFlag) String() string { + if c.value == nil { + return "" + } + return strconv.FormatBool(*c.value) +} + +// Type reports a bool, so that the help shows --color like the other switches. +func (c *colorFlag) Type() string { + return "bool" +} diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 00000000..ad18f131 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "io" + "testing" + + "github.com/gonvenience/bunt" + "github.com/mgutz/ansi" + "github.com/stretchr/testify/require" +) + +func TestColorFlag(t *testing.T) { + resetColors := func() { + ansi.DisableColors(false) + bunt.SetColorSettings(bunt.AUTO, bunt.AUTO) + } + t.Cleanup(resetColors) + + for _, tt := range []struct { + arg string + colored bool + }{ + {arg: "--color", colored: true}, + {arg: "--color=true", colored: true}, + {arg: "--color=false", colored: false}, + // Helm 4 passes its own --color flag on to the plugin. + {arg: "--color=always", colored: true}, + {arg: "--color=never", colored: false}, + // The captured stdout is not a terminal. + {arg: "--color=auto", colored: false}, + } { + t.Run(tt.arg, func(t *testing.T) { + resetColors() + + cmd := New() + cmd.SetArgs([]string{"version", tt.arg}) + var err error + _, captureErr := captureStdout(func() { err = cmd.Execute() }) + require.NoError(t, captureErr) + require.NoError(t, err) + + require.Equal(t, tt.colored, ansi.Color("x", "red") != "x") + require.Equal(t, tt.colored, bunt.UseColors()) + }) + } + + t.Run("invalid value", func(t *testing.T) { + cmd := New() + cmd.SetArgs([]string{"version", "--color=sometimes"}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + require.ErrorContains(t, cmd.Execute(), `invalid argument "sometimes" for "--color" flag`) + }) +}