From 764f2d03a52d9bbc702d6e9b285db7cac082a192 Mon Sep 17 00:00:00 2001 From: jessicagreben Date: Wed, 24 Nov 2021 09:17:49 -0800 Subject: [PATCH] add const for exit codes Signed-off-by: jessicagreben --- cmd/promtool/main.go | 69 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 7ae656c68..184bc9fdb 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -56,6 +56,13 @@ import ( "github.com/prometheus/prometheus/scrape" ) +const ( + successExitCode = 0 + failureExitCode = 1 + // exit code 3 is used for "one or more lint issues detected". + lintErrExitCode = 3 +) + func main() { app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for the Prometheus monitoring system.").UsageWriter(os.Stdout) app.Version(version.Print("promtool")) @@ -294,9 +301,9 @@ func CheckConfig(agentMode bool, files ...string) int { } } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } // CheckWebConfig validates web configuration files. @@ -312,9 +319,9 @@ func CheckWebConfig(files ...string) int { fmt.Fprintln(os.Stderr, f, "SUCCESS") } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } func checkFileExists(fn string) error { @@ -503,9 +510,9 @@ func CheckRules(files ...string) int { fmt.Println() } if failed { - return 1 + return failureExitCode } - return 0 + return successExitCode } func checkRules(filename string) (int, []error) { @@ -513,7 +520,7 @@ func checkRules(filename string) (int, []error) { rgs, errs := rulefmt.ParseFile(filename) if errs != nil { - return 0, errs + return successExitCode, errs } numRules := 0 @@ -609,7 +616,7 @@ func CheckMetrics() int { problems, err := l.Lint() if err != nil { fmt.Fprintln(os.Stderr, "error while linting:", err) - return 1 + return failureExitCode } for _, p := range problems { @@ -617,10 +624,10 @@ func CheckMetrics() int { } if len(problems) > 0 { - return 3 + return lintErrExitCode } - return 0 + return successExitCode } // QueryInstant performs an instant query against a Prometheus server. @@ -636,7 +643,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } eTime := time.Now() @@ -644,7 +651,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { eTime, err = parseTime(evalTime) if err != nil { fmt.Fprintln(os.Stderr, "error parsing evaluation time:", err) - return 1 + return failureExitCode } } @@ -660,7 +667,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { p.printValue(val) - return 0 + return successExitCode } // QueryRange performs a range query against a Prometheus server. @@ -685,7 +692,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } var stime, etime time.Time @@ -696,7 +703,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin etime, err = parseTime(end) if err != nil { fmt.Fprintln(os.Stderr, "error parsing end time:", err) - return 1 + return failureExitCode } } @@ -706,13 +713,13 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin stime, err = parseTime(start) if err != nil { fmt.Fprintln(os.Stderr, "error parsing start time:", err) - return 1 + return failureExitCode } } if !stime.Before(etime) { fmt.Fprintln(os.Stderr, "start time is not before end time") - return 1 + return failureExitCode } if step == 0 { @@ -733,7 +740,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin } p.printValue(val) - return 0 + return successExitCode } // QuerySeries queries for a series against a Prometheus server. @@ -749,13 +756,13 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer) c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } stime, etime, err := parseStartTimeAndEndTime(start, end) if err != nil { fmt.Fprintln(os.Stderr, err) - return 1 + return failureExitCode } // Run query against client. @@ -769,7 +776,7 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer) } p.printSeries(val) - return 0 + return successExitCode } // QueryLabels queries for label values against a Prometheus server. @@ -785,13 +792,13 @@ func QueryLabels(url *url.URL, name, start, end string, p printer) int { c, err := api.NewClient(config) if err != nil { fmt.Fprintln(os.Stderr, "error creating API client:", err) - return 1 + return failureExitCode } stime, etime, err := parseStartTimeAndEndTime(start, end) if err != nil { fmt.Fprintln(os.Stderr, err) - return 1 + return failureExitCode } // Run query against client. @@ -808,7 +815,7 @@ func QueryLabels(url *url.URL, name, start, end string, p printer) int { } p.printLabelValues(val) - return 0 + return successExitCode } func handleAPIError(err error) int { @@ -819,7 +826,7 @@ func handleAPIError(err error) int { fmt.Fprintln(os.Stderr, "query error:", err) } - return 1 + return failureExitCode } func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) { @@ -911,9 +918,9 @@ func debugPprof(url string) int { endPointGroups: pprofEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } func debugMetrics(url string) int { @@ -923,9 +930,9 @@ func debugMetrics(url string) int { endPointGroups: metricsEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } func debugAll(url string) int { @@ -935,9 +942,9 @@ func debugAll(url string) int { endPointGroups: allEndpoints, }); err != nil { fmt.Fprintln(os.Stderr, "error completing debug command:", err) - return 1 + return failureExitCode } - return 0 + return successExitCode } type printer interface {