Add version flag

And print version at startup.
This commit is contained in:
Thorben Günther 2023-01-16 14:35:53 +01:00
parent 7548567992
commit 78801c98e4
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
4 changed files with 18 additions and 2 deletions

View file

@ -11,6 +11,7 @@ sources:
tasks: tasks:
- test: | - test: |
cd ntfy-alertmanager cd ntfy-alertmanager
go generate ./...
go test -v ./... go test -v ./...
- lint: | - lint: |
cd ntfy-alertmanager cd ntfy-alertmanager

1
.gitignore vendored
View file

@ -1 +1,2 @@
/ntfy-alertmanager /ntfy-alertmanager
version.txt

View file

@ -2,7 +2,8 @@ FROM golang:alpine as build
WORKDIR /app WORKDIR /app
COPY . . COPY . .
RUN go build -o /app/ntfy-alertmanager RUN apk add --no-cache git
RUN go generate ./... && go build -o /app/ntfy-alertmanager
FROM alpine:latest FROM alpine:latest

15
main.go
View file

@ -4,11 +4,13 @@ package main
import ( import (
"crypto/sha512" "crypto/sha512"
"crypto/subtle" "crypto/subtle"
_ "embed"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"os"
"strings" "strings"
"time" "time"
@ -17,6 +19,10 @@ import (
"golang.org/x/text/language" "golang.org/x/text/language"
) )
//go:generate sh -c "git describe --long > version.txt"
//go:embed version.txt
var version string
type receiver struct { type receiver struct {
cfg *config cfg *config
logger *log.Logger logger *log.Logger
@ -205,8 +211,15 @@ func (rcv *receiver) basicAuthMiddleware(handler http.HandlerFunc) http.HandlerF
func main() { func main() {
var configPath string var configPath string
flag.StringVar(&configPath, "config", "/etc/ntfy-alertmanager/config", "config file path") flag.StringVar(&configPath, "config", "/etc/ntfy-alertmanager/config", "config file path")
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "Show version and exit")
flag.Parse() flag.Parse()
if showVersion {
fmt.Println(version)
os.Exit(0)
}
logger := log.NewDefaultLogger() logger := log.NewDefaultLogger()
cfg, err := readConfig(configPath) cfg, err := readConfig(configPath)
@ -220,7 +233,7 @@ func main() {
receiver := &receiver{cfg: cfg, logger: logger} receiver := &receiver{cfg: cfg, logger: logger}
logger.Infof("Listening on %s", cfg.HTTPAddress) logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version)
if cfg.User != "" && cfg.Password != "" { if cfg.User != "" && cfg.Password != "" {
logger.Info("Enabling HTTP Basic Authentication") logger.Info("Enabling HTTP Basic Authentication")