From 83189dbad35b282fab17a3fd3407516fbda3ebff Mon Sep 17 00:00:00 2001 From: astorath Date: Sat, 24 Apr 2021 02:07:24 +0300 Subject: [PATCH] Enable `helmfile-diff` to pass the output format to helm-diff (#1784) * tests: fix vagrant test run * feat: added an option to specify the different diff output format * renamed diff-output to output * renamed diff-output to output Co-authored-by: Andrey Tuzhilin --- .gitignore | 1 + Vagrantfile | 4 ++-- main.go | 14 ++++++++++++++ pkg/app/app.go | 2 ++ pkg/app/app_test.go | 5 +++++ pkg/app/config.go | 2 ++ pkg/app/diff_test.go | 5 +++++ pkg/state/state.go | 5 +++++ test/integration/run.sh | 5 ++++- 9 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8f84f97a..b1fd2451 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ vendor/ *.log .vagrant/ *.lock +test/integration/.gnupg/ diff --git a/Vagrantfile b/Vagrantfile index 69368c69..97c18512 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,11 +1,11 @@ Vagrant.configure("2") do |config| - config.vm.box = "ubuntu/xenial64" + config.vm.box = "ubuntu/focal64" config.vm.hostname = "minikube.box" config.vm.provision :shell, privileged: false, inline: <<-EOS set -e sudo apt-get update - sudo apt-get install -y make docker.io=18.09.7-* + sudo apt-get install -y make docker.io sudo systemctl start docker sudo usermod -G docker $USER cd /vagrant/.circleci diff --git a/main.go b/main.go index 0ab52a06..590bd232 100644 --- a/main.go +++ b/main.go @@ -225,6 +225,11 @@ func main() { Value: 0, Usage: "output NUM lines of context around changes", }, + cli.StringFlag{ + Name: "output", + Value: "", + Usage: "output format for diff plugin", + }, }, Action: action(func(a *app.App, c configImpl) error { return a.Diff(c) @@ -441,6 +446,11 @@ func main() { Value: 0, Usage: "output NUM lines of context around changes", }, + cli.StringFlag{ + Name: "output", + Value: "", + Usage: "output format for diff plugin", + }, cli.BoolFlag{ Name: "detailed-exitcode", Usage: "return a non-zero exit code 2 instead of 0 when there were changes detected AND the changes are synced successfully", @@ -855,6 +865,10 @@ func (c configImpl) Context() int { return c.c.Int("context") } +func (c configImpl) DiffOutput() string { + return c.c.String("output") +} + func (c configImpl) SkipCleanup() bool { return c.c.Bool("skip-cleanup") } diff --git a/pkg/app/app.go b/pkg/app/app.go index 638c118a..e7c63b3c 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -1153,6 +1153,7 @@ func (a *App) apply(r *Run, c ApplyConfigProvider) (bool, bool, []error) { diffOpts := &state.DiffOpts{ NoColor: c.NoColor(), Context: c.Context(), + Output: c.DiffOutput(), Set: c.Set(), SkipCleanup: c.RetainValuesFiles() || c.SkipCleanup(), SkipDiffOnInstall: c.SkipDiffOnInstall(), @@ -1378,6 +1379,7 @@ func (a *App) diff(r *Run, c DiffConfigProvider) (*string, bool, bool, []error) opts := &state.DiffOpts{ Context: c.Context(), + Output: c.DiffOutput(), NoColor: c.NoColor(), Set: c.Set(), } diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index d10602a5..9a2c158a 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2331,6 +2331,7 @@ type applyConfig struct { suppressDiff bool noColor bool context int + diffOutput string concurrency int detailedExitcode bool interactive bool @@ -2408,6 +2409,10 @@ func (a applyConfig) Context() int { return a.context } +func (a applyConfig) DiffOutput() string { + return a.diffOutput +} + func (a applyConfig) Concurrency() int { return a.concurrency } diff --git a/pkg/app/config.go b/pkg/app/config.go index 358a2eec..f797a61a 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -54,6 +54,7 @@ type ApplyConfigProvider interface { NoColor() bool Context() int + DiffOutput() string RetainValuesFiles() bool SkipCleanup() bool @@ -104,6 +105,7 @@ type DiffConfigProvider interface { DetailedExitcode() bool NoColor() bool Context() int + DiffOutput() string concurrencyConfig } diff --git a/pkg/app/diff_test.go b/pkg/app/diff_test.go index fa9fe044..e75cdd04 100644 --- a/pkg/app/diff_test.go +++ b/pkg/app/diff_test.go @@ -32,6 +32,7 @@ type diffConfig struct { suppressDiff bool noColor bool context int + diffOutput string concurrency int detailedExitcode bool interactive bool @@ -94,6 +95,10 @@ func (a diffConfig) Context() int { return a.context } +func (a diffConfig) DiffOutput() string { + return a.diffOutput +} + func (a diffConfig) Concurrency() int { return a.concurrency } diff --git a/pkg/state/state.go b/pkg/state/state.go index 1508eef9..998373b7 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1664,6 +1664,10 @@ func (st *HelmState) prepareDiffReleases(helm helmexec.Interface, additionalValu flags = append(flags, "--context", fmt.Sprintf("%d", opts.Context)) } + if opts.Output != "" { + flags = append(flags, "--output", fmt.Sprintf("%s", opts.Output)) + } + if opts.Set != nil { for _, s := range opts.Set { flags = append(flags, "--set", s) @@ -1739,6 +1743,7 @@ func (st *HelmState) createHelmContextWithWriter(spec *ReleaseSpec, w io.Writer) type DiffOpts struct { Context int + Output string NoColor bool Set []string diff --git a/test/integration/run.sh b/test/integration/run.sh index 8d246bb1..53775fb9 100755 --- a/test/integration/run.sh +++ b/test/integration/run.sh @@ -26,7 +26,7 @@ export HELM_HOME="${HELM_DATA_HOME}" export HELM_PLUGINS="${HELM_DATA_HOME}/plugins" export HELM_CONFIG_HOME="${helm_dir}/config" HELM_SECRETS_VERSION=3.5.0 -HELM_DIFF_VERSION=3.0.0-rc.7 +HELM_DIFF_VERSION=3.1.3 export GNUPGHOME="${PWD}/${dir}/.gnupg" export SOPS_PGP_FP="B2D6D7BBEC03B2E66571C8C00AD18E16CFDEF700" @@ -95,6 +95,9 @@ bash -c "${helmfile} -f ${dir}/happypath.yaml --no-color diff --detailed-exitcod info "Diffing ${dir}/happypath.yaml with limited context" bash -c "${helmfile} -f ${dir}/happypath.yaml diff --context 3 --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff" +info "Diffing ${dir}/happypath.yaml with altered output" +bash -c "${helmfile} -f ${dir}/happypath.yaml diff --output simple --detailed-exitcode; code="'$?'"; [ "'${code}'" -eq 2 ]" || fail "unexpected exit code returned by helmfile diff" + info "Templating ${dir}/happypath.yaml" rm -rf ${dir}/tmp ${helmfile} -f ${dir}/happypath.yaml --debug template --output-dir tmp