Merge pull request #24 from helmfile/auto-detect-term-for-color-diff

feat: Auto-detect term for coloring helm-diff output
This commit is contained in:
Yusuke Kuoka 2022-04-15 07:24:42 +09:00 committed by GitHub
commit 42ad7beb6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 2 deletions

29
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,26 @@ func (c configImpl) Interactive() bool {
return c.c.GlobalBool("interactive")
}
func (c configImpl) Color() bool {
if c := c.c.GlobalBool("color"); c {
return c
}
if c.NoColor() {
return false
}
// 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")
}
@ -1000,6 +1025,10 @@ func action(do func(*app.App, configImpl) error) func(*cli.Context) error {
return err
}
if err := app.ValidateConfig(conf); err != nil {
return err
}
a := app.New(conf)
if err := do(a, conf); err != nil {

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

@ -0,0 +1,11 @@
package app
import "errors"
func ValidateConfig(conf ApplyConfigProvider) error {
if conf.NoColor() && conf.Color() {
return errors.New("--color and --no-color cannot be specified at the same time")
}
return nil
}

View File

@ -1733,6 +1733,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 {
@ -1817,8 +1819,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