use dyff as yamldiff tool (#542)

Signed-off-by: yxxhero <aiopsclub@163.com>

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2022-11-27 08:47:09 +08:00 committed by GitHub
parent 60621ff318
commit 211852cd38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 13 additions and 159 deletions

View File

@ -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

2
.gitignore vendored
View File

@ -3,7 +3,7 @@ dist/
/helmfile
/helmfile.lock
/diff-yamls
/yamldiff
/dyff
test/integration/tmp$
vendor/
*.log

View File

@ -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:

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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 <file1> <file2>`")
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
}
}