feat(diff,apply): --no-color for removing color from output (#848)
Resolves #788
This commit is contained in:
		
							parent
							
								
									cbf5b8b1e7
								
							
						
					
					
						commit
						fb2041555e
					
				
							
								
								
									
										8
									
								
								main.go
								
								
								
								
							
							
						
						
									
										8
									
								
								main.go
								
								
								
								
							|  | @ -75,6 +75,10 @@ func main() { | ||||||
| 			Name:  "kube-context", | 			Name:  "kube-context", | ||||||
| 			Usage: "Set kubectl context. Uses current context by default", | 			Usage: "Set kubectl context. Uses current context by default", | ||||||
| 		}, | 		}, | ||||||
|  | 		cli.BoolFlag{ | ||||||
|  | 			Name:  "no-color", | ||||||
|  | 			Usage: "Output without color", | ||||||
|  | 		}, | ||||||
| 		cli.StringFlag{ | 		cli.StringFlag{ | ||||||
| 			Name:  "log-level", | 			Name:  "log-level", | ||||||
| 			Usage: "Set log level, default info", | 			Usage: "Set log level, default info", | ||||||
|  | @ -537,6 +541,10 @@ func (c configImpl) Interactive() bool { | ||||||
| 	return c.c.GlobalBool("interactive") | 	return c.c.GlobalBool("interactive") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (c configImpl) NoColor() bool { | ||||||
|  | 	return c.c.GlobalBool("no-color") | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (c configImpl) Logger() *zap.SugaredLogger { | func (c configImpl) Logger() *zap.SugaredLogger { | ||||||
| 	return c.c.App.Metadata["logger"].(*zap.SugaredLogger) | 	return c.c.App.Metadata["logger"].(*zap.SugaredLogger) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -40,6 +40,8 @@ type ApplyConfigProvider interface { | ||||||
| 
 | 
 | ||||||
| 	SuppressSecrets() bool | 	SuppressSecrets() bool | ||||||
| 
 | 
 | ||||||
|  | 	NoColor() bool | ||||||
|  | 
 | ||||||
| 	concurrencyConfig | 	concurrencyConfig | ||||||
| 	interactive | 	interactive | ||||||
| 	loggingConfig | 	loggingConfig | ||||||
|  | @ -64,6 +66,7 @@ type DiffConfigProvider interface { | ||||||
| 	SuppressSecrets() bool | 	SuppressSecrets() bool | ||||||
| 
 | 
 | ||||||
| 	DetailedExitcode() bool | 	DetailedExitcode() bool | ||||||
|  | 	NoColor() bool | ||||||
| 
 | 
 | ||||||
| 	concurrencyConfig | 	concurrencyConfig | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -137,7 +137,11 @@ func (r *Run) Apply(c ApplyConfigProvider) []error { | ||||||
| 	// helm must be 2.11+ and helm-diff should be provided `--detailed-exitcode` in order for `helmfile apply` to work properly
 | 	// helm must be 2.11+ and helm-diff should be provided `--detailed-exitcode` in order for `helmfile apply` to work properly
 | ||||||
| 	detailedExitCode := true | 	detailedExitCode := true | ||||||
| 
 | 
 | ||||||
| 	releases, errs := st.DiffReleases(helm, c.Values(), c.Concurrency(), detailedExitCode, c.SuppressSecrets(), false) | 	diffOpts := &state.DiffOpts{ | ||||||
|  | 		NoColor: c.NoColor(), | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	releases, errs := st.DiffReleases(helm, c.Values(), c.Concurrency(), detailedExitCode, c.SuppressSecrets(), false, diffOpts) | ||||||
| 
 | 
 | ||||||
| 	releasesToBeDeleted, err := st.DetectReleasesToBeDeleted(helm) | 	releasesToBeDeleted, err := st.DetectReleasesToBeDeleted(helm) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
|  | @ -705,7 +705,12 @@ type diffPrepareResult struct { | ||||||
| 	errors  []*ReleaseError | 	errors  []*ReleaseError | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValues []string, concurrency int, detailedExitCode, suppressSecrets bool) ([]diffPrepareResult, []error) { | func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValues []string, concurrency int, detailedExitCode, suppressSecrets bool, opt ...DiffOpt) ([]diffPrepareResult, []error) { | ||||||
|  | 	opts := &DiffOpts{} | ||||||
|  | 	for _, o := range opt { | ||||||
|  | 		o.Apply(opts) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	releases := []*ReleaseSpec{} | 	releases := []*ReleaseSpec{} | ||||||
| 	for i, _ := range st.Releases { | 	for i, _ := range st.Releases { | ||||||
| 		if !st.Releases[i].Desired() { | 		if !st.Releases[i].Desired() { | ||||||
|  | @ -767,6 +772,10 @@ func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValu | ||||||
| 					flags = append(flags, "--suppress-secrets") | 					flags = append(flags, "--suppress-secrets") | ||||||
| 				} | 				} | ||||||
| 
 | 
 | ||||||
|  | 				if opts.NoColor { | ||||||
|  | 					flags = append(flags, "--no-color") | ||||||
|  | 				} | ||||||
|  | 
 | ||||||
| 				if len(errs) > 0 { | 				if len(errs) > 0 { | ||||||
| 					rsErrs := make([]*ReleaseError, len(errs)) | 					rsErrs := make([]*ReleaseError, len(errs)) | ||||||
| 					for i, e := range errs { | 					for i, e := range errs { | ||||||
|  | @ -812,10 +821,25 @@ func (st *HelmState) createHelmContext(spec *ReleaseSpec, workerIndex int) helme | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | type DiffOpts struct { | ||||||
|  | 	NoColor bool | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (o *DiffOpts) Apply(opts *DiffOpts) { | ||||||
|  | 	*opts = *o | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type DiffOpt interface{ Apply(*DiffOpts) } | ||||||
|  | 
 | ||||||
| // DiffReleases wrapper for executing helm diff on the releases
 | // DiffReleases wrapper for executing helm diff on the releases
 | ||||||
| // It returns releases that had any changes
 | // It returns releases that had any changes
 | ||||||
| func (st *HelmState) DiffReleases(helm helmexec.Interface, additionalValues []string, workerLimit int, detailedExitCode, suppressSecrets bool, triggerCleanupEvents bool) ([]*ReleaseSpec, []error) { | func (st *HelmState) DiffReleases(helm helmexec.Interface, additionalValues []string, workerLimit int, detailedExitCode, suppressSecrets bool, triggerCleanupEvents bool, opt ...DiffOpt) ([]*ReleaseSpec, []error) { | ||||||
| 	preps, prepErrs := st.prepareDiffReleases(helm, additionalValues, workerLimit, detailedExitCode, suppressSecrets) | 	opts := &DiffOpts{} | ||||||
|  | 	for _, o := range opt { | ||||||
|  | 		o.Apply(opts) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	preps, prepErrs := st.prepareDiffReleases(helm, additionalValues, workerLimit, detailedExitCode, suppressSecrets, opts) | ||||||
| 	if len(prepErrs) > 0 { | 	if len(prepErrs) > 0 { | ||||||
| 		return []*ReleaseSpec{}, prepErrs | 		return []*ReleaseSpec{}, prepErrs | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -60,6 +60,9 @@ test_start "happypath - simple rollout of httpbin chart" | ||||||
| info "Diffing ${dir}/happypath.yaml" | info "Diffing ${dir}/happypath.yaml" | ||||||
| bash -c "${helmfile} -f ${dir}/happypath.yaml diff --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff" | bash -c "${helmfile} -f ${dir}/happypath.yaml diff --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff" | ||||||
| 
 | 
 | ||||||
|  | info "Diffing ${dir}/happypath.yaml without color" | ||||||
|  | bash -c "${helmfile} -f ${dir}/happypath.yaml --no-color diff --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff" | ||||||
|  | 
 | ||||||
| info "Templating ${dir}/happypath.yaml" | info "Templating ${dir}/happypath.yaml" | ||||||
| ${helmfile} -f ${dir}/happypath.yaml template | ${helmfile} -f ${dir}/happypath.yaml template | ||||||
| code=$? | code=$? | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue