Skip to content

Commit

Permalink
Add version flag to commandline
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Petrausch committed Sep 19, 2020
1 parent 4168037 commit 9e4ab53
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
Expand All @@ -16,6 +17,13 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// These variables are set by goreleaser at linking time.
var (
version string
commit string
date string
)

var (
configFlag = flag.String(
"config",
Expand All @@ -32,10 +40,19 @@ var (
"0.0.0.0",
"listen address for HTTP server used to expose metrics",
)
versionFlag = flag.Bool(
"version",
false,
"show the builds version, date and commit",
)
)

func main() {
flag.Parse()
if *versionFlag {
mustShowVersion()
os.Exit(0)
}
c := make(chan os.Signal, 1)
hostName, err := os.Hostname()
if err != nil {
Expand Down Expand Up @@ -93,3 +110,20 @@ func main() {
func getListenAddress() string {
return fmt.Sprintf("%s:%s", *addressFlag, *portFlag)
}

func mustShowVersion() {
versionInfo := struct {
Version string
Commit string
Date string
}{
Version: version,
Commit: commit,
Date: date,
}

err := json.NewEncoder(os.Stdout).Encode(versionInfo)
if err != nil {
panic(err)
}
}

0 comments on commit 9e4ab53

Please sign in to comment.