Skip to content

Commit 06b9429

Browse files
author
Lorenz Kästle
committed
Refactor version logic
Previously the version was generated in the main function at runtime. This patch use the cobra cmd native functionality to include it, which reduces the logic in this program. The version is not generated in the Makefile and linked into the program at compile time.
1 parent fffb5b9 commit 06b9429

File tree

3 files changed

+10
-43
lines changed

3 files changed

+10
-43
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ifeq ($(GIT_COMMIT), $(GIT_LAST_TAG_COMMIT))
1515
VERSION = $(shell git tag -l --contains $(GIT_COMMIT))
1616
endif
1717

18-
GO_LINKERFLAGS := "-X main.version=$(VERSION)"
18+
GO_LINKERFLAGS := "-X github.com/NETWAYS/check_system_basics/cmd.version=$(VERSION)"
1919

2020
GO_LINKEROPTS := -ldflags $(GO_LINKERFLAGS)
2121

cmd/root.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import (
1212
var Timeout = 30
1313
var debug = false
1414

15+
var (
16+
version string
17+
)
18+
1519
var rootCmd = &cobra.Command{
16-
Use: "check_system_basics",
17-
Short: "Icinga check plugin to check various Linux metrics",
20+
Use: "check_system_basics",
21+
Short: "Icinga check plugin to check various Linux metrics",
22+
Version: version,
1823
PersistentPreRun: func(cmd *cobra.Command, args []string) {
1924
go check.HandleTimeout(Timeout)
2025
},
2126
Run: RunFunction,
2227
}
2328

24-
func Execute(version string) {
29+
func Execute() {
2530
defer check.CatchPanic()
2631

27-
rootCmd.Version = version
28-
rootCmd.VersionTemplate()
29-
3032
if err := rootCmd.Execute(); err != nil {
3133
check.ExitError(err)
3234
}
@@ -52,8 +54,6 @@ func init() {
5254

5355
flagSet := rootCmd.Flags()
5456
flagSet.Bool("dump-icinga2-config", false, "Dump icinga2 config for this plugin")
55-
56-
flagSet.Bool("version", false, "Display version and other information about this program")
5757
}
5858

5959
func RunFunction(cmd *cobra.Command, args []string) {
@@ -69,16 +69,6 @@ func RunFunction(cmd *cobra.Command, args []string) {
6969
os.Exit(check.OK)
7070
}
7171

72-
showVersion, err := flagSet.GetBool("version")
73-
if err != nil {
74-
check.ExitError(err)
75-
}
76-
77-
if showVersion {
78-
fmt.Println(cmd.Version)
79-
os.Exit(check.OK)
80-
}
81-
8272
Help(cmd, args)
8373
}
8474

main.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
11
package main
22

33
import (
4-
"fmt"
5-
64
"github.com/NETWAYS/check_system_basics/cmd"
75
)
86

9-
var (
10-
version string
11-
commit = ""
12-
date = ""
13-
)
14-
157
func main() {
16-
cmd.Execute(buildVersion())
17-
}
18-
19-
//goland:noinspection GoBoolExpressions
20-
func buildVersion() string {
21-
result := version
22-
23-
if commit != "" {
24-
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
25-
}
26-
27-
if date != "" {
28-
result = fmt.Sprintf("%s\ndate: %s", result, date)
29-
}
30-
31-
return result
8+
cmd.Execute()
329
}

0 commit comments

Comments
 (0)