From d384065fd027ace85cfc6bfc54455e412cefb21b Mon Sep 17 00:00:00 2001 From: natsuki-hoshino Date: Wed, 28 Aug 2024 06:38:14 +0000 Subject: [PATCH] [feat] add --version flag and output version information --- main.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/main.go b/main.go index c92d7d5..dab37c8 100644 --- a/main.go +++ b/main.go @@ -6,15 +6,27 @@ import ( "net/http" "os" + _ "embed" "github.com/alecthomas/kingpin" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) +var ( + version string + commit string + date string + builtBy string + + //go:embed VERSION + fallbackVersion string +) + func main() { var ( ctx = context.Background() app = kingpin.New("postfix_exporter", "Prometheus metrics exporter for postfix") + versionFlag = app.Flag("version", "Print version information").Bool() listenAddress = app.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9154").String() metricsPath = app.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String() postfixShowqPath = app.Flag("postfix.showq_path", "Path at which Postfix places its showq socket.").Default("/var/spool/postfix/public/showq").String() @@ -24,6 +36,26 @@ func main() { InitLogSourceFactories(app) kingpin.MustParse(app.Parse(os.Args[1:])) + if version == "" { + version = fallbackVersion + } + + if *versionFlag { + os.Stdout.WriteString(version) + os.Exit(0) + } + versionString := "postfix_exporter " + version + if commit != "" { + versionString += " (" + commit + ")" + } + if date != "" { + versionString += " built on " + date + } + if builtBy != "" { + versionString += " by: " + builtBy + } + log.Print(versionString) + logSrc, err := NewLogSourceFromFactories(ctx) if err != nil { log.Fatalf("Error opening log source: %s", err)