Skip to content
Merged
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
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <argument>` 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:
Expand Down Expand Up @@ -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"
```

Expand Down Expand Up @@ -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"
```

Expand Down Expand Up @@ -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"
```

Expand Down Expand Up @@ -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"
```

Expand Down Expand Up @@ -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"
```

Expand Down
46 changes: 43 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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=<value>.
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"
}
54 changes: 54 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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`)
})
}
Loading