Skip to content
Snippets Groups Projects
Commit c155296d authored by Tom Wilkie's avatar Tom Wilkie
Browse files

Exit with 0 for no differences found, 1 for difference found, 2 for other error.

parent 56ef55da
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,8 @@ import ( ...@@ -33,6 +33,8 @@ import (
const flagDiffStrategy = "diff-strategy" const flagDiffStrategy = "diff-strategy"
var ErrDiffFound = fmt.Errorf("Differences found.")
func init() { func init() {
diffCmd.PersistentFlags().String(flagDiffStrategy, "all", "Diff strategy, all or subset.") diffCmd.PersistentFlags().String(flagDiffStrategy, "all", "Diff strategy, all or subset.")
RootCmd.AddCommand(diffCmd) RootCmd.AddCommand(diffCmd)
...@@ -67,7 +69,7 @@ var diffCmd = &cobra.Command{ ...@@ -67,7 +69,7 @@ var diffCmd = &cobra.Command{
sort.Sort(utils.AlphabeticalOrder(objs)) sort.Sort(utils.AlphabeticalOrder(objs))
differencesFound := false diffFound := false
for _, obj := range objs { for _, obj := range objs {
desc := fmt.Sprintf("%s/%s", obj.GetKind(), fqName(obj)) desc := fmt.Sprintf("%s/%s", obj.GetKind(), fqName(obj))
log.Debugf("Fetching ", desc) log.Debugf("Fetching ", desc)
...@@ -89,7 +91,7 @@ var diffCmd = &cobra.Command{ ...@@ -89,7 +91,7 @@ var diffCmd = &cobra.Command{
fmt.Fprintf(out, "- live %s\n+ config %s\n", desc, desc) fmt.Fprintf(out, "- live %s\n+ config %s\n", desc, desc)
if liveObj == nil { if liveObj == nil {
fmt.Fprintf(out, "%s doesn't exist on server\n", desc) fmt.Fprintf(out, "%s doesn't exist on server\n", desc)
differencesFound = true diffFound = true
continue continue
} }
...@@ -100,7 +102,7 @@ var diffCmd = &cobra.Command{ ...@@ -100,7 +102,7 @@ var diffCmd = &cobra.Command{
diff := gojsondiff.New().CompareObjects(liveObjObject, obj.Object) diff := gojsondiff.New().CompareObjects(liveObjObject, obj.Object)
if diff.Modified() { if diff.Modified() {
differencesFound = true diffFound = true
fcfg := formatter.AsciiFormatterConfig{ fcfg := formatter.AsciiFormatterConfig{
Coloring: istty(out), Coloring: istty(out),
} }
...@@ -115,8 +117,8 @@ var diffCmd = &cobra.Command{ ...@@ -115,8 +117,8 @@ var diffCmd = &cobra.Command{
} }
} }
if differencesFound { if diffFound {
return fmt.Errorf("Differences found.") return ErrDiffFound
} }
return nil return nil
}, },
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package main package main
import ( import (
"os"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/ksonnet/kubecfg/cmd" "github.com/ksonnet/kubecfg/cmd"
...@@ -32,7 +34,13 @@ func main() { ...@@ -32,7 +34,13 @@ func main() {
// errors, like invalid command line flags. // errors, like invalid command line flags.
logFmt := cmd.NewLogFormatter(log.StandardLogger().Out) logFmt := cmd.NewLogFormatter(log.StandardLogger().Out)
log.SetFormatter(logFmt) log.SetFormatter(logFmt)
log.Error(err.Error())
log.Fatal(err.Error()) switch err {
case cmd.ErrDiffFound:
os.Exit(1)
default:
os.Exit(2)
}
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment