add const for exit codes

Signed-off-by: jessicagreben <jessicagrebens@gmail.com>
This commit is contained in:
jessicagreben 2021-11-24 09:17:49 -08:00
parent 5e746e4e88
commit 764f2d03a5

View file

@ -56,6 +56,13 @@ import (
"github.com/prometheus/prometheus/scrape" "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() { func main() {
app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for the Prometheus monitoring system.").UsageWriter(os.Stdout) app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for the Prometheus monitoring system.").UsageWriter(os.Stdout)
app.Version(version.Print("promtool")) app.Version(version.Print("promtool"))
@ -294,9 +301,9 @@ func CheckConfig(agentMode bool, files ...string) int {
} }
} }
if failed { if failed {
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
// CheckWebConfig validates web configuration files. // CheckWebConfig validates web configuration files.
@ -312,9 +319,9 @@ func CheckWebConfig(files ...string) int {
fmt.Fprintln(os.Stderr, f, "SUCCESS") fmt.Fprintln(os.Stderr, f, "SUCCESS")
} }
if failed { if failed {
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
func checkFileExists(fn string) error { func checkFileExists(fn string) error {
@ -503,9 +510,9 @@ func CheckRules(files ...string) int {
fmt.Println() fmt.Println()
} }
if failed { if failed {
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
func checkRules(filename string) (int, []error) { func checkRules(filename string) (int, []error) {
@ -513,7 +520,7 @@ func checkRules(filename string) (int, []error) {
rgs, errs := rulefmt.ParseFile(filename) rgs, errs := rulefmt.ParseFile(filename)
if errs != nil { if errs != nil {
return 0, errs return successExitCode, errs
} }
numRules := 0 numRules := 0
@ -609,7 +616,7 @@ func CheckMetrics() int {
problems, err := l.Lint() problems, err := l.Lint()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error while linting:", err) fmt.Fprintln(os.Stderr, "error while linting:", err)
return 1 return failureExitCode
} }
for _, p := range problems { for _, p := range problems {
@ -617,10 +624,10 @@ func CheckMetrics() int {
} }
if len(problems) > 0 { if len(problems) > 0 {
return 3 return lintErrExitCode
} }
return 0 return successExitCode
} }
// QueryInstant performs an instant query against a Prometheus server. // 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) c, err := api.NewClient(config)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error creating API client:", err) fmt.Fprintln(os.Stderr, "error creating API client:", err)
return 1 return failureExitCode
} }
eTime := time.Now() eTime := time.Now()
@ -644,7 +651,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int {
eTime, err = parseTime(evalTime) eTime, err = parseTime(evalTime)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error parsing evaluation time:", err) 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) p.printValue(val)
return 0 return successExitCode
} }
// QueryRange performs a range query against a Prometheus server. // 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) c, err := api.NewClient(config)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error creating API client:", err) fmt.Fprintln(os.Stderr, "error creating API client:", err)
return 1 return failureExitCode
} }
var stime, etime time.Time 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) etime, err = parseTime(end)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error parsing end time:", err) 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) stime, err = parseTime(start)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error parsing start time:", err) fmt.Fprintln(os.Stderr, "error parsing start time:", err)
return 1 return failureExitCode
} }
} }
if !stime.Before(etime) { if !stime.Before(etime) {
fmt.Fprintln(os.Stderr, "start time is not before end time") fmt.Fprintln(os.Stderr, "start time is not before end time")
return 1 return failureExitCode
} }
if step == 0 { if step == 0 {
@ -733,7 +740,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin
} }
p.printValue(val) p.printValue(val)
return 0 return successExitCode
} }
// QuerySeries queries for a series against a Prometheus server. // 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) c, err := api.NewClient(config)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error creating API client:", err) fmt.Fprintln(os.Stderr, "error creating API client:", err)
return 1 return failureExitCode
} }
stime, etime, err := parseStartTimeAndEndTime(start, end) stime, etime, err := parseStartTimeAndEndTime(start, end)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return 1 return failureExitCode
} }
// Run query against client. // Run query against client.
@ -769,7 +776,7 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer)
} }
p.printSeries(val) p.printSeries(val)
return 0 return successExitCode
} }
// QueryLabels queries for label values against a Prometheus server. // 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) c, err := api.NewClient(config)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "error creating API client:", err) fmt.Fprintln(os.Stderr, "error creating API client:", err)
return 1 return failureExitCode
} }
stime, etime, err := parseStartTimeAndEndTime(start, end) stime, etime, err := parseStartTimeAndEndTime(start, end)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return 1 return failureExitCode
} }
// Run query against client. // Run query against client.
@ -808,7 +815,7 @@ func QueryLabels(url *url.URL, name, start, end string, p printer) int {
} }
p.printLabelValues(val) p.printLabelValues(val)
return 0 return successExitCode
} }
func handleAPIError(err error) int { func handleAPIError(err error) int {
@ -819,7 +826,7 @@ func handleAPIError(err error) int {
fmt.Fprintln(os.Stderr, "query error:", err) fmt.Fprintln(os.Stderr, "query error:", err)
} }
return 1 return failureExitCode
} }
func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) { func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) {
@ -911,9 +918,9 @@ func debugPprof(url string) int {
endPointGroups: pprofEndpoints, endPointGroups: pprofEndpoints,
}); err != nil { }); err != nil {
fmt.Fprintln(os.Stderr, "error completing debug command:", err) fmt.Fprintln(os.Stderr, "error completing debug command:", err)
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
func debugMetrics(url string) int { func debugMetrics(url string) int {
@ -923,9 +930,9 @@ func debugMetrics(url string) int {
endPointGroups: metricsEndpoints, endPointGroups: metricsEndpoints,
}); err != nil { }); err != nil {
fmt.Fprintln(os.Stderr, "error completing debug command:", err) fmt.Fprintln(os.Stderr, "error completing debug command:", err)
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
func debugAll(url string) int { func debugAll(url string) int {
@ -935,9 +942,9 @@ func debugAll(url string) int {
endPointGroups: allEndpoints, endPointGroups: allEndpoints,
}); err != nil { }); err != nil {
fmt.Fprintln(os.Stderr, "error completing debug command:", err) fmt.Fprintln(os.Stderr, "error completing debug command:", err)
return 1 return failureExitCode
} }
return 0 return successExitCode
} }
type printer interface { type printer interface {