Skip to content

Commit

Permalink
--version cli flag
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanotorresi committed Jun 3, 2020
1 parent 71f3338 commit 1ad7364
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# this is the what ends up in the RPM "Version" field and it is also used as suffix for the built binaries
# if you want to commit to OBS it must be a remotely available Git reference
VERSION ?= $(shell git rev-parse --short HEAD)
VERSION ?= $(shell git describe --tags --abbrev=0)dev+git.$(shell git show -s --format=%ct.%h HEAD)
DATE = $(shell date --iso-8601=seconds)

# we only use this to comply with RPM changelog conventions at SUSE
AUTHOR ?= shap-staff@suse.de
Expand All @@ -25,7 +26,7 @@ build-all: clean-bin $(ARCHS)

$(ARCHS):
@mkdir -p build/bin
CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o build/bin/sap_host_exporter-$(VERSION)-$@
CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION) -X main.buildDate=$(DATE)" -o build/bin/sap_host_exporter-$(VERSION)-$@

install:
go install
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/spf13/viper"
)

func New() (*viper.Viper, error) {
func New(flagSet *flag.FlagSet) (*viper.Viper, error) {
config := viper.New()

err := config.BindPFlags(flag.CommandLine)
err := config.BindPFlags(flagSet)
if err != nil {
return nil, errors.Wrap(err, "could not bind config to CLI flags")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ func loadConfigurationFromFile(config *viper.Viper, path string) error {
return errors.Wrap(err, "could not read file")
}

log.Info("Using config file: ", path)

return nil
}
52 changes: 47 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"net/http"
"os"
"runtime"

"github.com/SUSE/sap_host_exporter/collector/registry"
"github.com/SUSE/sap_host_exporter/collector/start_service"
Expand All @@ -16,26 +18,52 @@ import (
flag "github.com/spf13/pflag"
)

var (
// the released version
version string
// the time the binary was built
buildDate string
// global --help flag
helpFlag *bool
// global --version flag
versionFlag *bool
)

func init() {
flag.String("port", "9680", "The port number to listen on for HTTP requests")
flag.String("address", "0.0.0.0", "The address to listen on for HTTP requests")
flag.String("log-level", "info", "The minimum logging level; levels are, in ascending order: debug, info, warn, error")
flag.String("sap-control-url", "localhost:50013", "The URL of the SAPControl SOAP web service, e.g. $HOST:$PORT")
flag.String("sap-control-uds", "", "The path to the SAPControl Unix Domain Socket. If set, this will be used instead of the URL.")
flag.StringP("config", "c", "", "The path to a custom configuration file. NOTE: it must be in yaml format.")
flag.CommandLine.SortFlags = false

helpFlag = flag.BoolP("help", "h", false, "show this help message")
versionFlag = flag.Bool("version", false, "show version and build information")
}

func main() {
var err error

flag.Parse()

config, err := config.New()
switch {
case *helpFlag:
showHelp()
case *versionFlag:
showVersion()
default:
run()
}
}

func run() {
var err error

globalConfig, err := config.New(flag.CommandLine)
if err != nil {
log.Fatalf("Could not initialize config: %s", err)
}

client := sapcontrol.NewSoapClient(config)
client := sapcontrol.NewSoapClient(globalConfig)
webService := sapcontrol.NewWebService(client)
currentSapInstance, err := webService.GetCurrentInstance()
if err != nil {
Expand All @@ -62,11 +90,25 @@ func main() {
prometheus.Unregister(prometheus.NewGoCollector())
}

fullListenAddress := fmt.Sprintf("%s:%s", config.Get("address"), config.Get("port"))
fullListenAddress := fmt.Sprintf("%s:%s", globalConfig.Get("address"), globalConfig.Get("port"))

http.HandleFunc("/", internal.Landing)
http.Handle("/metrics", promhttp.Handler())

log.Infof("Serving metrics on %s", fullListenAddress)
log.Fatal(http.ListenAndServe(fullListenAddress, nil))
}

func showHelp() {
flag.Usage()
os.Exit(0)
}

func showVersion() {
if buildDate == "" {
buildDate = "at unknown time"
}
fmt.Printf("version %s\nbuilt with %s %s/%s %s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate)
os.Exit(0)
}

3 changes: 2 additions & 1 deletion packaging/obs/prometheus-sap_host_exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Prometheus exporter for Pacemaker HA clusters metrics
%setup -q -T -D -a 1 # unpack go dependencies in vendor.tar.gz, which was prepared by the source services

%define shortname sap_host_exporter
%define build_date %(date --iso-8601=seconds)

%build

export CGO_ENABLED=0
go build -mod=vendor \
-ldflags="-s -w -X main.version=%{version}" \
-ldflags="-s -w -X main.version=%{version} -X main.buildDate=%{build_date}" \
-o %{shortname}

%install
Expand Down

0 comments on commit 1ad7364

Please sign in to comment.