From 97e0ca73ca21288bac18906660d74d076ba34b53 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Wed, 6 Apr 2022 02:37:11 +0000 Subject: [PATCH] feat: Auto-detect term for coloring helm-diff output Since helm-diff has added an ability to auto-detect the term to decide if it should output with color or not, helmfile had been defaulted to no-color. This resoloves that, by adding a term-detection logic that is same as helm-diff. As a part of this work, I have also implemented a new global flag `--color`, which is used for forcing color without relying on the term-detection logic implemented in helmfile or explicitly setting the HELM_DIFF_COLOR envvar. I hope it is useful for folks. Ref https://github.com/roboll/helmfile/issues/2043 Signed-off-by: Yusuke Kuoka --- main.go | 21 +++++++++++++++++++++ pkg/app/app.go | 2 ++ pkg/app/app_test.go | 4 ++++ pkg/app/config.go | 2 ++ pkg/app/diff_test.go | 4 ++++ pkg/state/state.go | 11 +++++++++-- 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5d975d30..f9c4a759 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/roboll/helmfile/pkg/state" "github.com/urfave/cli" "go.uber.org/zap" + "golang.org/x/crypto/ssh/terminal" ) var logger *zap.SugaredLogger @@ -76,6 +77,10 @@ func main() { Name: "debug", Usage: "Enable verbose output for Helm and set log-level to debug, this disables --quiet/-q effect", }, + cli.BoolFlag{ + Name: "color", + Usage: "Output with color", + }, cli.BoolFlag{ Name: "no-color", Usage: "Output without color", @@ -942,6 +947,22 @@ func (c configImpl) Interactive() bool { return c.c.GlobalBool("interactive") } +func (c configImpl) Color() bool { + if c := c.c.GlobalBool("color"); c { + return c + } + + // We replicate the helm-diff behavior in helmfile + // because when when helmfile calls helm-diff, helm-diff has no access to term and therefore + // we can't rely on helm-diff's ability to auto-detect term for color output. + // See https://github.com/roboll/helmfile/issues/2043 + + term := terminal.IsTerminal(int(os.Stdout.Fd())) + // https://github.com/databus23/helm-diff/issues/281 + dumb := os.Getenv("TERM") == "dumb" + return term && !dumb +} + func (c configImpl) NoColor() bool { return c.c.GlobalBool("no-color") } diff --git a/pkg/app/app.go b/pkg/app/app.go index d03ed6d6..8d841f63 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1259,6 +1259,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { detailedExitCode := true diffOpts := &state.DiffOpts{ + Color: c.Color(), NoColor: c.NoColor(), Context: c.Context(), Output: c.DiffOutput(), @@ -1479,6 +1480,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error) opts := &state.DiffOpts{ Context: c.Context(), Output: c.DiffOutput(), + Color: c.Color(), NoColor: c.NoColor(), Set: c.Set(), SkipDiffOnInstall: c.SkipDiffOnInstall(), diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 86d4f3e9..b27ad140 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2426,6 +2426,10 @@ func (a applyConfig) SuppressDiff() bool { return a.suppressDiff } +func (a applyConfig) Color() bool { + return false +} + func (a applyConfig) NoColor() bool { return a.noColor } diff --git a/pkg/app/config.go b/pkg/app/config.go index e39281fb..99cfc1fa 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -56,6 +56,7 @@ type ApplyConfigProvider interface { DetailedExitcode() bool + Color() bool NoColor() bool Context() int DiffOutput() string @@ -113,6 +114,7 @@ type DiffConfigProvider interface { IncludeNeeds() bool DetailedExitcode() bool + Color() bool NoColor() bool Context() int DiffOutput() string diff --git a/pkg/app/diff_test.go b/pkg/app/diff_test.go index af801a97..91f60210 100644 --- a/pkg/app/diff_test.go +++ b/pkg/app/diff_test.go @@ -93,6 +93,10 @@ func (a diffConfig) SuppressDiff() bool { return a.suppressDiff } +func (a diffConfig) Color() bool { + return false +} + func (a diffConfig) NoColor() bool { return a.noColor } diff --git a/pkg/state/state.go b/pkg/state/state.go index 5f8bb8dc..0b0a3702 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1718,6 +1718,8 @@ func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValu if opts.NoColor { flags = append(flags, "--no-color") + } else if opts.Color { + flags = append(flags, "--color") } if opts.Context > 0 { @@ -1802,8 +1804,13 @@ func (st *HelmState) createHelmContextWithWriter(spec *ReleaseSpec, w io.Writer) } type DiffOpts struct { - Context int - Output string + Context int + Output string + // Color forces the color output on helm-diff. + // This takes effect only when NoColor is false. + Color bool + // NoColor forces disabling the color output on helm-diff. + // If this is true, Color has no effect. NoColor bool Set []string SkipCleanup bool