From 211852cd387b8805e983368ba869613e9390ca70 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Sun, 27 Nov 2022 08:47:09 +0800 Subject: [PATCH] use dyff as yamldiff tool (#542) Signed-off-by: yxxhero Signed-off-by: yxxhero --- .github/workflows/ci.yaml | 8 +- .gitignore | 2 +- Makefile | 2 +- test/integration/test-cases/chart-needs.sh | 2 +- .../test-cases/chart-needs/output/template | 6 +- test/integration/test-cases/secretssops.sh | 4 +- test/integration/test-cases/yaml-overwrite.sh | 2 +- test/yamldiff/yamldiff.go | 146 ------------------ 8 files changed, 13 insertions(+), 159 deletions(-) delete mode 100644 test/yamldiff/yamldiff.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d4314a44..9aa8842e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,7 +37,7 @@ jobs: run: make check test - name: Prepare tar to upload built binaries - run: tar -cvf built-binaries.tar helmfile diff-yamls yamldiff + run: tar -cvf built-binaries.tar helmfile diff-yamls dyff - name: Upload built binaries uses: actions/upload-artifact@v2 with: @@ -45,7 +45,7 @@ jobs: path: built-binaries.tar retention-days: 1 - name: Display built binaries - run: ls -l helmfile diff-yamls yamldiff + run: ls -l helmfile diff-yamls dyff integration_tests: needs: tests @@ -115,7 +115,7 @@ jobs: - name: Extract tar to get built binaries run: tar -xvf built-binaries.tar - name: Display built binaries - run: ls -l helmfile diff-yamls yamldiff + run: ls -l helmfile diff-yamls dyff - name: Install test dependencies env: @@ -149,7 +149,7 @@ jobs: - name: Extract tar to get built binaries run: tar -xvf built-binaries.tar - name: Display built binaries - run: ls -l helmfile diff-yamls yamldiff + run: ls -l helmfile diff-yamls dyff - name: Run helmfile init env: TERM: xterm diff --git a/.gitignore b/.gitignore index 3a9fcd48..e43c6dc0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ dist/ /helmfile /helmfile.lock /diff-yamls -/yamldiff +/dyff test/integration/tmp$ vendor/ *.log diff --git a/Makefile b/Makefile index d91a4a3c..faca381b 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ check: build-test-tools: go build test/diff-yamls/diff-yamls.go - go build test/yamldiff/yamldiff.go + curl --progress-bar --location https://github.com/homeport/dyff/releases/download/v1.5.6/dyff_1.5.6_linux_amd64.tar.gz | tar -xzf - -C `pwd` dyff .PHONY: build-test-tools test: diff --git a/test/integration/test-cases/chart-needs.sh b/test/integration/test-cases/chart-needs.sh index c17c30d3..f3a0e47f 100644 --- a/test/integration/test-cases/chart-needs.sh +++ b/test/integration/test-cases/chart-needs.sh @@ -21,7 +21,7 @@ if [[ helm_major_version -eq 3 ]]; then for i in $(seq 10); do info "Comparing template/chart-needs #$i" ${helmfile} -f ${chart_need_case_input_dir}/helmfile.yaml template --include-needs > ${chart_needs_template_reverse} || fail "\"helmfile template\" shouldn't fail" - ./yamldiff ${chart_need_case_output_dir}/template ${chart_needs_template_reverse} || fail "\"helmfile template\" should be consistent" + ./dyff between -bs ${chart_need_case_output_dir}/template ${chart_needs_template_reverse} || fail "\"helmfile template\" should be consistent" echo code=$? done diff --git a/test/integration/test-cases/chart-needs/output/template b/test/integration/test-cases/chart-needs/output/template index 5703f036..f6042e32 100644 --- a/test/integration/test-cases/chart-needs/output/template +++ b/test/integration/test-cases/chart-needs/output/template @@ -419,7 +419,7 @@ spec: - "--drivername=disk.csi.azure.com" - "--volume-attach-limit=-1" - "--cloud-config-secret-name=azure-cloud-provider" - - "--cloud-config-secret-namespace=helmfile-tests" + - "--cloud-config-secret-namespace=kube-system" - "--custom-user-agent=" - "--user-agent-suffix=OSS-helm" - "--allow-empty-cloud-config=true" @@ -624,7 +624,7 @@ spec: - "--drivername=disk.csi.azure.com" - "--volume-attach-limit=-1" - "--cloud-config-secret-name=azure-cloud-provider" - - "--cloud-config-secret-namespace=helmfile-tests" + - "--cloud-config-secret-namespace=kube-system" - "--custom-user-agent=" - "--user-agent-suffix=OSS-helm" - "--allow-empty-cloud-config=true" @@ -866,7 +866,7 @@ spec: - "--vm-type=" - "--drivername=disk.csi.azure.com" - "--cloud-config-secret-name=azure-cloud-provider" - - "--cloud-config-secret-namespace=helmfile-tests" + - "--cloud-config-secret-namespace=kube-system" - "--custom-user-agent=" - "--user-agent-suffix=OSS-helm" - "--allow-empty-cloud-config=false" diff --git a/test/integration/test-cases/secretssops.sh b/test/integration/test-cases/secretssops.sh index 323f015b..a9f77b91 100644 --- a/test/integration/test-cases/secretssops.sh +++ b/test/integration/test-cases/secretssops.sh @@ -47,7 +47,7 @@ if [[ helm_major_version -eq 3 ]]; then for i in $(seq 10); do info "Comparing build/direct #$i" ${helmfile} -f ${secretssops_case_input_dir}/secretssops.yaml -e direct template --skip-deps > ${direct} || fail "\"helmfile template\" shouldn't fail" - ./yamldiff ${secretssops_case_output_dir}/direct.build.yaml ${direct} || fail "\"helmfile template\" should be consistent" + ./dyff between -bs ${secretssops_case_output_dir}/direct.build.yaml ${direct} || fail "\"helmfile template\" should be consistent" echo code=$? done @@ -55,7 +55,7 @@ if [[ helm_major_version -eq 3 ]]; then for i in $(seq 10); do info "Comparing build/reverse #$i" ${helmfile} -f ${secretssops_case_input_dir}/secretssops.yaml -e reverse template --skip-deps > ${reverse} || fail "\"helmfile template\" shouldn't fail" - ./yamldiff ${secretssops_case_output_dir}/reverse.build.yaml ${reverse} || fail "\"helmfile template\" should be consistent" + ./dyff between -bs ${secretssops_case_output_dir}/reverse.build.yaml ${reverse} || fail "\"helmfile template\" should be consistent" echo code=$? done diff --git a/test/integration/test-cases/yaml-overwrite.sh b/test/integration/test-cases/yaml-overwrite.sh index 4147beb4..83d7bbde 100644 --- a/test/integration/test-cases/yaml-overwrite.sh +++ b/test/integration/test-cases/yaml-overwrite.sh @@ -10,7 +10,7 @@ if [[ helm_major_version -eq 3 ]]; then for i in $(seq 10); do info "Comparing build/yaml-overwrite #$i" ${helmfile} -f ${yaml_overwrite_case_input_dir}/issue.657.yaml template --skip-deps > ${yaml_overwrite_reverse} || fail "\"helmfile template\" shouldn't fail" - ./yamldiff ${yaml_overwrite_case_output_dir}/overwritten.yaml ${yaml_overwrite_reverse} || fail "\"helmfile template\" should be consistent" + ./dyff between -bs ${yaml_overwrite_case_output_dir}/overwritten.yaml ${yaml_overwrite_reverse} || fail "\"helmfile template\" should be consistent" echo code=$? done test_pass "yaml overwrite feature" diff --git a/test/yamldiff/yamldiff.go b/test/yamldiff/yamldiff.go deleted file mode 100644 index 1bc6739c..00000000 --- a/test/yamldiff/yamldiff.go +++ /dev/null @@ -1,146 +0,0 @@ -package main - -import ( - "fmt" - "io" - "os" - "strings" - - "github.com/jessevdk/go-flags" - "github.com/kylelemons/godebug/pretty" - "github.com/logrusorgru/aurora" - "github.com/mattn/go-isatty" - "gopkg.in/yaml.v3" -) - -var version = "latest" - -func main() { - var opts struct { - NoColor bool `long:"no-color" description:"disable colored output" required:"false"` - Version func() `long:"version" description:"print version and exit"` - // Ignore []string `long:"ignore" description:"ignore vaules by yaml path" required:"false"` - } - - opts.Version = func() { - fmt.Fprintf(os.Stderr, "%v\n", version) - os.Exit(0) - } - - args, err := flags.Parse(&opts) - if err != nil { - if err.(*flags.Error).Type == flags.ErrHelp { - os.Exit(0) - } - os.Exit(1) - } - if len(args) != 2 { - fmt.Println("requires exactly two arguments to diff, use `yamldiff `") - os.Exit(1) - } - - formatter := newFormatter(opts.NoColor) - - errors := stat(args[0], args[1]) - failOnErr(formatter, errors...) - - yaml1, err := unmarshal(args[0]) - if err != nil { - failOnErr(formatter, err) - } - yaml2, err := unmarshal(args[1]) - if err != nil { - failOnErr(formatter, err) - } - - diff, ndiff := computeDiff(formatter, yaml1, yaml2) - if ndiff > 0 { - fmt.Println(diff) - os.Exit(2) - } -} - -func stat(filenames ...string) []error { - var errs []error - for _, filename := range filenames { - if filename == "-" { - continue - } - _, err := os.Stat(filename) - if err != nil { - errs = append(errs, fmt.Errorf("cannot find file: %v. Does it exist?", filename)) - } - } - return errs -} - -func unmarshal(filename string) (interface{}, error) { - var contents []byte - var err error - if filename == "-" { - contents, err = io.ReadAll(os.Stdin) - } else { - contents, err = os.ReadFile(filename) - } - if err != nil { - return nil, err - } - var ret interface{} - err = yaml.Unmarshal(contents, &ret) - if err != nil { - return nil, err - } - return ret, nil -} - -func failOnErr(formatter aurora.Aurora, errs ...error) { - if len(errs) == 0 { - return - } - var errMessages []string - for _, err := range errs { - errMessages = append(errMessages, err.Error()) - } - fmt.Fprintf(os.Stderr, "%v\n\n", formatter.Red(strings.Join(errMessages, "\n"))) - os.Exit(1) -} - -func computeDiff(formatter aurora.Aurora, a interface{}, b interface{}) (string, int) { - diffs := make([]string, 0) - ndiffs := 0 - for _, s := range strings.Split(pretty.Compare(a, b), "\n") { - switch { - case strings.HasPrefix(s, "+"): - diffs = append(diffs, formatter.Bold(formatter.Green(s)).String()) - ndiffs++ - case strings.HasPrefix(s, "-"): - diffs = append(diffs, formatter.Bold(formatter.Red(s)).String()) - ndiffs++ - default: - diffs = append(diffs, s) - } - } - return strings.Join(diffs, "\n"), ndiffs -} - -func newFormatter(noColor bool) aurora.Aurora { - var formatter aurora.Aurora - if noColor || !isTerminal() { - formatter = aurora.NewAurora(false) - } else { - formatter = aurora.NewAurora(true) - } - return formatter -} - -func isTerminal() bool { - fd := os.Stdout.Fd() - switch { - case isatty.IsTerminal(fd): - return true - case isatty.IsCygwinTerminal(fd): - return true - default: - return false - } -}