Skip to content

Commit ff66e6f

Browse files
committed
improve version
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 644b270 commit ff66e6f

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Usage:
1919
kube-janitor [OPTIONS]
2020
2121
Application Options:
22+
--version Show version
23+
--version.template= Version go template, eg {{.Version}}
2224
--log.level=[trace|debug|info|warning|error] Log level (default: info) [$LOG_LEVEL]
2325
--log.format=[logfmt|json] Log format (default: logfmt) [$LOG_FORMAT]
2426
--log.source=[|short|file|full] Show source for every log message (useful for debugging and bug reports) [$LOG_SOURCE]

common.version.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/webdevops/go-common/version"
8+
)
9+
10+
var (
11+
// Git version information
12+
gitCommit = "<unknown>"
13+
gitTag = "<unknown>"
14+
buildDate = "<unknown>"
15+
)
16+
17+
func printStartup(app, author string) {
18+
appVersion := version.New(
19+
version.WithApp(app),
20+
version.WithAuthor(author),
21+
version.WithVersion(gitTag),
22+
version.WithGitCommit(gitCommit),
23+
version.WithBuildDate(buildDate),
24+
)
25+
26+
if Opts.Version.Version {
27+
fmt.Println(appVersion.BuildVersionLine(Opts.Version.Template))
28+
os.Exit(0)
29+
}
30+
31+
logger.Info(appVersion.Title(nil))
32+
logger.Info(string(Opts.GetJson()))
33+
}

config/opts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77

88
type (
99
Opts struct {
10+
Version struct {
11+
Version bool `long:"version" description:"Show version"`
12+
Template *string `long:"version.template" description:"Version go template, eg {{.Version}}"`
13+
}
14+
1015
// logger
1116
Logger struct {
1217
Level string `long:"log.level" env:"LOG_LEVEL" description:"Log level" choice:"trace" choice:"debug" choice:"info" choice:"warning" choice:"error" default:"info"` // nolint:staticcheck // multiple choices are ok

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/jmespath-community/go-jmespath v1.1.1
1313
github.com/patrickmn/go-cache v2.1.0+incompatible
1414
github.com/prometheus/client_golang v1.23.2
15-
github.com/webdevops/go-common v0.0.0-20251219213826-139615203ee5
15+
github.com/webdevops/go-common v0.0.0-20251222130718-713f8c390b81
1616
k8s.io/api v0.35.0
1717
k8s.io/apimachinery v0.35.0
1818
k8s.io/client-go v0.35.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
123123
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
124124
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
125125
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
126-
github.com/webdevops/go-common v0.0.0-20251219213826-139615203ee5 h1:tWKJuCBPLrmThNw2YFDdh3yx95No75Tev+zgMxJ1RCQ=
127-
github.com/webdevops/go-common v0.0.0-20251219213826-139615203ee5/go.mod h1:2RZgXC980Lwz2M00Ghm+8/fGY864X7xzXPzFR2RojHc=
126+
github.com/webdevops/go-common v0.0.0-20251222130718-713f8c390b81 h1:aTQaT6FWdu02ZZl3aaCtWxu6HQA4QsmmU6TqLiTkLd0=
127+
github.com/webdevops/go-common v0.0.0-20251222130718-713f8c390b81/go.mod h1:FRDcaUKsREtkbLMx1Xoe6DFsTo3FI8PtGBgtJERPGBk=
128128
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
129129
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
130130
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=

main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log/slog"
77
"net/http"
88
"os"
9-
"runtime"
109

1110
"github.com/jessevdk/go-flags"
1211
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -24,19 +23,14 @@ const (
2423
var (
2524
argparser *flags.Parser
2625
Opts config.Opts
27-
28-
// Git version information
29-
gitCommit = "<unknown>"
30-
gitTag = "<unknown>"
31-
buildDate = "<unknown>"
3226
)
3327

3428
func main() {
3529
initArgparser()
3630
initLogger()
3731

38-
logger.Info(fmt.Sprintf("starting kube-janitor v%s (%s; %s; by %v at %v)", gitTag, gitCommit, runtime.Version(), Author, buildDate))
39-
logger.Info(string(Opts.GetJson()))
32+
printStartup("kube-janitor", Author)
33+
4034
initSystem()
4135

4236
janitor := kube_janitor.New()

0 commit comments

Comments
 (0)