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 <ykuoka@gmail.com>
This commit is contained in:
Yusuke Kuoka 2022-04-06 02:37:11 +00:00
parent 8fb418e3c9
commit 97e0ca73ca
6 changed files with 42 additions and 2 deletions

21
main.go
View File

@ -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")
}

View File

@ -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(),

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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