Skip to content

Commit 6c42a91

Browse files
committed
feat: add -version flag for printing version and build info
Closes #117
1 parent 669a0de commit 6c42a91

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

magefile.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ func Build() error {
6161
if arch == "" {
6262
arch = runtime.GOARCH
6363
}
64-
65-
if err := sh.RunWith(map[string]string{"GOARCH": arch}, "go", "build", "-o", "build/coraza-spoa"); err != nil {
64+
gitVersion, _ := sh.Output("git", "describe", "--tags", "--always", "--dirty")
65+
ldflags := fmt.Sprintf("-X 'main.Version=%s'", gitVersion)
66+
if err := sh.RunWith(map[string]string{
67+
"GOARCH": arch,
68+
}, "go", "build", "-ldflags="+ldflags, "-o", "build/coraza-spoa"); err != nil {
6669
return err
6770
}
6871
return nil

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@ package main
66
import (
77
"context"
88
"flag"
9+
"fmt"
910
"net"
1011
"net/http"
1112
"os"
1213
"os/signal"
1314
"runtime"
15+
"runtime/debug"
1416
"runtime/pprof"
1517
"syscall"
18+
_ "unsafe"
1619

1720
"github.com/prometheus/client_golang/prometheus/promhttp"
1821
"github.com/rs/zerolog"
1922

2023
"github.com/corazawaf/coraza-spoa/internal"
2124
)
2225

26+
var (
27+
Version = "dev"
28+
)
29+
2330
var (
2431
configPath string
2532
validateConfig bool
2633
autoReload bool
2734
cpuProfile string
2835
memProfile string
2936
metricsAddr string
37+
version bool
3038
globalLogger = zerolog.New(os.Stderr).With().Timestamp().Logger()
3139
)
3240

@@ -37,8 +45,17 @@ func main() {
3745
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
3846
flag.StringVar(&memProfile, "memprofile", "", "write memory profile to `file`")
3947
flag.StringVar(&metricsAddr, "metrics-addr", "", "ip:port bind for prometheus metrics")
48+
flag.BoolVar(&version, "version", false, "show version and exit")
4049
flag.Parse()
4150

51+
if version {
52+
fmt.Printf("Coraza SPOA Version: %s\n", Version)
53+
if bi, ok := debug.ReadBuildInfo(); ok {
54+
fmt.Printf("Build Info: %s", bi.String())
55+
}
56+
return
57+
}
58+
4259
if configPath == "" {
4360
globalLogger.Fatal().Msg("Configuration file is not set")
4461
}

0 commit comments

Comments
 (0)