Skip to content

Commit

Permalink
diff returns exit(1) if there are any differences [fixes #243]
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic authored and geofffranks committed Jan 28, 2018
1 parent 5296bae commit a22a96a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions ci/release_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `spruce diff` now returns exit status 1 if there are any differences; and exit status 0 if no differences [thanks @drnic]
23 changes: 13 additions & 10 deletions cmd/spruce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,16 @@ func main() {
usage()
return
}
output, err := diffFiles(options.Diff.Files)
output, differences, err := diffFiles(options.Diff.Files)
if err != nil {
PrintfStdErr("%s\n", err)
exit(2)
return
}
printfStdOut("%s\n", output)
if differences {
exit(1)
}

default:
usage()
Expand Down Expand Up @@ -342,38 +345,38 @@ func dequoteConcourse(input []byte) string {
return re.ReplaceAllString(string(input), "$1")
}

func diffFiles(paths []string) (string, error) {
func diffFiles(paths []string) (string, bool, error) {
if len(paths) != 2 {
return "", ansi.Errorf("incorrect number of files given to diffFiles(); please file a bug report")
return "", false, ansi.Errorf("incorrect number of files given to diffFiles(); please file a bug report")
}

data, err := ioutil.ReadFile(paths[0])
if err != nil {
return "", ansi.Errorf("@R{Error reading file} @m{%s}: %s\n", paths[0], err)
return "", false, ansi.Errorf("@R{Error reading file} @m{%s}: %s\n", paths[0], err)
}
a, err := parseYAML(data)
if err != nil {
return "", ansi.Errorf("@m{%s}: @R{%s}\n", paths[0], err)
return "", false, ansi.Errorf("@m{%s}: @R{%s}\n", paths[0], err)
}

data, err = ioutil.ReadFile(paths[1])
if err != nil {
return "", ansi.Errorf("@R{Error reading file} @m{%s}: %s\n", paths[1], err)
return "", false, ansi.Errorf("@R{Error reading file} @m{%s}: %s\n", paths[1], err)
}
b, err := parseYAML(data)
if err != nil {
return "", ansi.Errorf("@m{%s}: @R{%s}\n", paths[1], err)
return "", false, ansi.Errorf("@m{%s}: @R{%s}\n", paths[1], err)
}

d, err := Diff(a, b)
if err != nil {
return "", ansi.Errorf("@R{Failed to diff} @m{%s} -> @m{%s}: %s\n", paths[0], paths[1], err)
return "", false, ansi.Errorf("@R{Failed to diff} @m{%s} -> @m{%s}: %s\n", paths[0], paths[1], err)
}

if !d.Changed() {
return ansi.Sprintf("@G{both files are semantically equivalent; no differences found!}\n"), nil
return ansi.Sprintf("@G{both files are semantically equivalent; no differences found!}\n"), false, nil
}
return d.String("$"), nil
return d.String("$"), true, nil
}

type RootIsArrayError struct {
Expand Down

0 comments on commit a22a96a

Please sign in to comment.