Skip to content

Commit b7d8fd6

Browse files
Merge pull request #47 from NETWAYS/version_refactor
Refactor version logic
2 parents fffb5b9 + b30b2c9 commit b7d8fd6

File tree

4 files changed

+12
-43
lines changed

4 files changed

+12
-43
lines changed

.goreleaser.yml

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ builds:
2424
- amd64
2525
- 386
2626
- riscv64
27+
ldflags:
28+
- -X github.com/NETWAYS/check_system_basics/cmd.version={{.Version}}

Makefile

+1-1
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

+8-18
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

+1-24
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)