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:
parent
60621ff318
commit
211852cd38
|
|
@ -37,7 +37,7 @@ jobs:
|
||||||
run: make check test
|
run: make check test
|
||||||
|
|
||||||
- name: Prepare tar to upload built binaries
|
- 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
|
- name: Upload built binaries
|
||||||
uses: actions/upload-artifact@v2
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
|
|
@ -45,7 +45,7 @@ jobs:
|
||||||
path: built-binaries.tar
|
path: built-binaries.tar
|
||||||
retention-days: 1
|
retention-days: 1
|
||||||
- name: Display built binaries
|
- name: Display built binaries
|
||||||
run: ls -l helmfile diff-yamls yamldiff
|
run: ls -l helmfile diff-yamls dyff
|
||||||
|
|
||||||
integration_tests:
|
integration_tests:
|
||||||
needs: tests
|
needs: tests
|
||||||
|
|
@ -115,7 +115,7 @@ jobs:
|
||||||
- name: Extract tar to get built binaries
|
- name: Extract tar to get built binaries
|
||||||
run: tar -xvf built-binaries.tar
|
run: tar -xvf built-binaries.tar
|
||||||
- name: Display built binaries
|
- name: Display built binaries
|
||||||
run: ls -l helmfile diff-yamls yamldiff
|
run: ls -l helmfile diff-yamls dyff
|
||||||
|
|
||||||
- name: Install test dependencies
|
- name: Install test dependencies
|
||||||
env:
|
env:
|
||||||
|
|
@ -149,7 +149,7 @@ jobs:
|
||||||
- name: Extract tar to get built binaries
|
- name: Extract tar to get built binaries
|
||||||
run: tar -xvf built-binaries.tar
|
run: tar -xvf built-binaries.tar
|
||||||
- name: Display built binaries
|
- name: Display built binaries
|
||||||
run: ls -l helmfile diff-yamls yamldiff
|
run: ls -l helmfile diff-yamls dyff
|
||||||
- name: Run helmfile init
|
- name: Run helmfile init
|
||||||
env:
|
env:
|
||||||
TERM: xterm
|
TERM: xterm
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ dist/
|
||||||
/helmfile
|
/helmfile
|
||||||
/helmfile.lock
|
/helmfile.lock
|
||||||
/diff-yamls
|
/diff-yamls
|
||||||
/yamldiff
|
/dyff
|
||||||
test/integration/tmp$
|
test/integration/tmp$
|
||||||
vendor/
|
vendor/
|
||||||
*.log
|
*.log
|
||||||
|
|
|
||||||
2
Makefile
2
Makefile
|
|
@ -40,7 +40,7 @@ check:
|
||||||
|
|
||||||
build-test-tools:
|
build-test-tools:
|
||||||
go build test/diff-yamls/diff-yamls.go
|
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
|
.PHONY: build-test-tools
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ if [[ helm_major_version -eq 3 ]]; then
|
||||||
for i in $(seq 10); do
|
for i in $(seq 10); do
|
||||||
info "Comparing template/chart-needs #$i"
|
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"
|
${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=$?
|
echo code=$?
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -419,7 +419,7 @@ spec:
|
||||||
- "--drivername=disk.csi.azure.com"
|
- "--drivername=disk.csi.azure.com"
|
||||||
- "--volume-attach-limit=-1"
|
- "--volume-attach-limit=-1"
|
||||||
- "--cloud-config-secret-name=azure-cloud-provider"
|
- "--cloud-config-secret-name=azure-cloud-provider"
|
||||||
- "--cloud-config-secret-namespace=helmfile-tests"
|
- "--cloud-config-secret-namespace=kube-system"
|
||||||
- "--custom-user-agent="
|
- "--custom-user-agent="
|
||||||
- "--user-agent-suffix=OSS-helm"
|
- "--user-agent-suffix=OSS-helm"
|
||||||
- "--allow-empty-cloud-config=true"
|
- "--allow-empty-cloud-config=true"
|
||||||
|
|
@ -624,7 +624,7 @@ spec:
|
||||||
- "--drivername=disk.csi.azure.com"
|
- "--drivername=disk.csi.azure.com"
|
||||||
- "--volume-attach-limit=-1"
|
- "--volume-attach-limit=-1"
|
||||||
- "--cloud-config-secret-name=azure-cloud-provider"
|
- "--cloud-config-secret-name=azure-cloud-provider"
|
||||||
- "--cloud-config-secret-namespace=helmfile-tests"
|
- "--cloud-config-secret-namespace=kube-system"
|
||||||
- "--custom-user-agent="
|
- "--custom-user-agent="
|
||||||
- "--user-agent-suffix=OSS-helm"
|
- "--user-agent-suffix=OSS-helm"
|
||||||
- "--allow-empty-cloud-config=true"
|
- "--allow-empty-cloud-config=true"
|
||||||
|
|
@ -866,7 +866,7 @@ spec:
|
||||||
- "--vm-type="
|
- "--vm-type="
|
||||||
- "--drivername=disk.csi.azure.com"
|
- "--drivername=disk.csi.azure.com"
|
||||||
- "--cloud-config-secret-name=azure-cloud-provider"
|
- "--cloud-config-secret-name=azure-cloud-provider"
|
||||||
- "--cloud-config-secret-namespace=helmfile-tests"
|
- "--cloud-config-secret-namespace=kube-system"
|
||||||
- "--custom-user-agent="
|
- "--custom-user-agent="
|
||||||
- "--user-agent-suffix=OSS-helm"
|
- "--user-agent-suffix=OSS-helm"
|
||||||
- "--allow-empty-cloud-config=false"
|
- "--allow-empty-cloud-config=false"
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ if [[ helm_major_version -eq 3 ]]; then
|
||||||
for i in $(seq 10); do
|
for i in $(seq 10); do
|
||||||
info "Comparing build/direct #$i"
|
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"
|
${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=$?
|
echo code=$?
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
@ -55,7 +55,7 @@ if [[ helm_major_version -eq 3 ]]; then
|
||||||
for i in $(seq 10); do
|
for i in $(seq 10); do
|
||||||
info "Comparing build/reverse #$i"
|
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"
|
${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=$?
|
echo code=$?
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ if [[ helm_major_version -eq 3 ]]; then
|
||||||
for i in $(seq 10); do
|
for i in $(seq 10); do
|
||||||
info "Comparing build/yaml-overwrite #$i"
|
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"
|
${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=$?
|
echo code=$?
|
||||||
done
|
done
|
||||||
test_pass "yaml overwrite feature"
|
test_pass "yaml overwrite feature"
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue