From 001b89c09049ce182ba3f6d0e15ef1d75aaaff02 Mon Sep 17 00:00:00 2001 From: Scott Miller Date: Fri, 4 Jun 2021 11:52:49 -0500 Subject: [PATCH] Return different exit codes for different diagnose results (#11758) This allows operators to run diagnose in scripts and detect the difference between success, warning, and failure. Exit codes are now: 0: Success (no warnings) 1: Failure (some test failed) 2: Warning (some test warned) 3: User input failure such as a bad flag 4: Other error --- command/operator_diagnose.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/command/operator_diagnose.go b/command/operator_diagnose.go index 2ad0e8d9ca..f34830f729 100644 --- a/command/operator_diagnose.go +++ b/command/operator_diagnose.go @@ -141,7 +141,7 @@ func (c *OperatorDiagnoseCommand) Run(args []string) int { f := c.Flags() if err := f.Parse(args); err != nil { c.UI.Error(err.Error()) - return 1 + return 3 } return c.RunWithParsedFlags() } @@ -150,7 +150,7 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int { if len(c.flagConfigs) == 0 { c.UI.Error("Must specify a configuration file using -config.") - return 1 + return 3 } if c.diagnose == nil { @@ -171,7 +171,7 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int { resultsJS, err := json.MarshalIndent(results, "", " ") if err != nil { fmt.Fprintf(os.Stderr, "error marshalling results: %v", err) - return 2 + return 4 } c.UI.Output(string(resultsJS)) } else { @@ -180,6 +180,13 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int { } if err != nil { + return 4 + } + // Use a different return code + switch results.Status { + case diagnose.WarningStatus: + return 2 + case diagnose.ErrorStatus: return 1 } return 0