From c155296df273149e18340057b75252e1f06a312e Mon Sep 17 00:00:00 2001 From: Tom Wilkie <tom.wilkie@gmail.com> Date: Wed, 26 Jul 2017 14:29:32 +0100 Subject: [PATCH] Exit with 0 for no differences found, 1 for difference found, 2 for other error. --- cmd/diff.go | 12 +++++++----- main.go | 10 +++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 27a32e70..27dbbe41 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -33,6 +33,8 @@ import ( const flagDiffStrategy = "diff-strategy" +var ErrDiffFound = fmt.Errorf("Differences found.") + func init() { diffCmd.PersistentFlags().String(flagDiffStrategy, "all", "Diff strategy, all or subset.") RootCmd.AddCommand(diffCmd) @@ -67,7 +69,7 @@ var diffCmd = &cobra.Command{ sort.Sort(utils.AlphabeticalOrder(objs)) - differencesFound := false + diffFound := false for _, obj := range objs { desc := fmt.Sprintf("%s/%s", obj.GetKind(), fqName(obj)) log.Debugf("Fetching ", desc) @@ -89,7 +91,7 @@ var diffCmd = &cobra.Command{ fmt.Fprintf(out, "- live %s\n+ config %s\n", desc, desc) if liveObj == nil { fmt.Fprintf(out, "%s doesn't exist on server\n", desc) - differencesFound = true + diffFound = true continue } @@ -100,7 +102,7 @@ var diffCmd = &cobra.Command{ diff := gojsondiff.New().CompareObjects(liveObjObject, obj.Object) if diff.Modified() { - differencesFound = true + diffFound = true fcfg := formatter.AsciiFormatterConfig{ Coloring: istty(out), } @@ -115,8 +117,8 @@ var diffCmd = &cobra.Command{ } } - if differencesFound { - return fmt.Errorf("Differences found.") + if diffFound { + return ErrDiffFound } return nil }, diff --git a/main.go b/main.go index 7940f2b8..9a12f9a6 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,8 @@ package main import ( + "os" + log "github.com/sirupsen/logrus" "github.com/ksonnet/kubecfg/cmd" @@ -32,7 +34,13 @@ func main() { // errors, like invalid command line flags. logFmt := cmd.NewLogFormatter(log.StandardLogger().Out) log.SetFormatter(logFmt) + log.Error(err.Error()) - log.Fatal(err.Error()) + switch err { + case cmd.ErrDiffFound: + os.Exit(1) + default: + os.Exit(2) + } } } -- GitLab