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:
		
							parent
							
								
									8fb418e3c9
								
							
						
					
					
						commit
						97e0ca73ca
					
				
							
								
								
									
										21
									
								
								main.go
								
								
								
								
							
							
						
						
									
										21
									
								
								main.go
								
								
								
								
							|  | @ -12,6 +12,7 @@ import ( | ||||||
| 	"github.com/roboll/helmfile/pkg/state" | 	"github.com/roboll/helmfile/pkg/state" | ||||||
| 	"github.com/urfave/cli" | 	"github.com/urfave/cli" | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
|  | 	"golang.org/x/crypto/ssh/terminal" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var logger *zap.SugaredLogger | var logger *zap.SugaredLogger | ||||||
|  | @ -76,6 +77,10 @@ func main() { | ||||||
| 			Name:  "debug", | 			Name:  "debug", | ||||||
| 			Usage: "Enable verbose output for Helm and set log-level to debug, this disables --quiet/-q effect", | 			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{ | 		cli.BoolFlag{ | ||||||
| 			Name:  "no-color", | 			Name:  "no-color", | ||||||
| 			Usage: "Output without color", | 			Usage: "Output without color", | ||||||
|  | @ -942,6 +947,22 @@ func (c configImpl) Interactive() bool { | ||||||
| 	return c.c.GlobalBool("interactive") | 	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 { | func (c configImpl) NoColor() bool { | ||||||
| 	return c.c.GlobalBool("no-color") | 	return c.c.GlobalBool("no-color") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1259,6 +1259,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { | ||||||
| 	detailedExitCode := true | 	detailedExitCode := true | ||||||
| 
 | 
 | ||||||
| 	diffOpts := &state.DiffOpts{ | 	diffOpts := &state.DiffOpts{ | ||||||
|  | 		Color:             c.Color(), | ||||||
| 		NoColor:           c.NoColor(), | 		NoColor:           c.NoColor(), | ||||||
| 		Context:           c.Context(), | 		Context:           c.Context(), | ||||||
| 		Output:            c.DiffOutput(), | 		Output:            c.DiffOutput(), | ||||||
|  | @ -1479,6 +1480,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error) | ||||||
| 	opts := &state.DiffOpts{ | 	opts := &state.DiffOpts{ | ||||||
| 		Context:           c.Context(), | 		Context:           c.Context(), | ||||||
| 		Output:            c.DiffOutput(), | 		Output:            c.DiffOutput(), | ||||||
|  | 		Color:             c.Color(), | ||||||
| 		NoColor:           c.NoColor(), | 		NoColor:           c.NoColor(), | ||||||
| 		Set:               c.Set(), | 		Set:               c.Set(), | ||||||
| 		SkipDiffOnInstall: c.SkipDiffOnInstall(), | 		SkipDiffOnInstall: c.SkipDiffOnInstall(), | ||||||
|  |  | ||||||
|  | @ -2426,6 +2426,10 @@ func (a applyConfig) SuppressDiff() bool { | ||||||
| 	return a.suppressDiff | 	return a.suppressDiff | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (a applyConfig) Color() bool { | ||||||
|  | 	return false | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (a applyConfig) NoColor() bool { | func (a applyConfig) NoColor() bool { | ||||||
| 	return a.noColor | 	return a.noColor | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -56,6 +56,7 @@ type ApplyConfigProvider interface { | ||||||
| 
 | 
 | ||||||
| 	DetailedExitcode() bool | 	DetailedExitcode() bool | ||||||
| 
 | 
 | ||||||
|  | 	Color() bool | ||||||
| 	NoColor() bool | 	NoColor() bool | ||||||
| 	Context() int | 	Context() int | ||||||
| 	DiffOutput() string | 	DiffOutput() string | ||||||
|  | @ -113,6 +114,7 @@ type DiffConfigProvider interface { | ||||||
| 	IncludeNeeds() bool | 	IncludeNeeds() bool | ||||||
| 
 | 
 | ||||||
| 	DetailedExitcode() bool | 	DetailedExitcode() bool | ||||||
|  | 	Color() bool | ||||||
| 	NoColor() bool | 	NoColor() bool | ||||||
| 	Context() int | 	Context() int | ||||||
| 	DiffOutput() string | 	DiffOutput() string | ||||||
|  |  | ||||||
|  | @ -93,6 +93,10 @@ func (a diffConfig) SuppressDiff() bool { | ||||||
| 	return a.suppressDiff | 	return a.suppressDiff | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (a diffConfig) Color() bool { | ||||||
|  | 	return false | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (a diffConfig) NoColor() bool { | func (a diffConfig) NoColor() bool { | ||||||
| 	return a.noColor | 	return a.noColor | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1718,6 +1718,8 @@ func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValu | ||||||
| 
 | 
 | ||||||
| 				if opts.NoColor { | 				if opts.NoColor { | ||||||
| 					flags = append(flags, "--no-color") | 					flags = append(flags, "--no-color") | ||||||
|  | 				} else if opts.Color { | ||||||
|  | 					flags = append(flags, "--color") | ||||||
| 				} | 				} | ||||||
| 
 | 
 | ||||||
| 				if opts.Context > 0 { | 				if opts.Context > 0 { | ||||||
|  | @ -1804,6 +1806,11 @@ func (st *HelmState) createHelmContextWithWriter(spec *ReleaseSpec, w io.Writer) | ||||||
| type DiffOpts struct { | type DiffOpts struct { | ||||||
| 	Context int | 	Context int | ||||||
| 	Output  string | 	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 | 	NoColor           bool | ||||||
| 	Set               []string | 	Set               []string | ||||||
| 	SkipCleanup       bool | 	SkipCleanup       bool | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue