From 06b9429986985392406ce3eccec95a48d5cb2527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Tue, 11 Jun 2024 12:00:33 +0200 Subject: [PATCH 1/2] 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. --- Makefile | 2 +- cmd/root.go | 26 ++++++++------------------ main.go | 25 +------------------------ 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 0e30b7e..d43c1da 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ ifeq ($(GIT_COMMIT), $(GIT_LAST_TAG_COMMIT)) VERSION = $(shell git tag -l --contains $(GIT_COMMIT)) endif -GO_LINKERFLAGS := "-X main.version=$(VERSION)" +GO_LINKERFLAGS := "-X github.com/NETWAYS/check_system_basics/cmd.version=$(VERSION)" GO_LINKEROPTS := -ldflags $(GO_LINKERFLAGS) diff --git a/cmd/root.go b/cmd/root.go index c07d126..14bb92d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,21 +12,23 @@ import ( var Timeout = 30 var debug = false +var ( + version string +) + var rootCmd = &cobra.Command{ - Use: "check_system_basics", - Short: "Icinga check plugin to check various Linux metrics", + Use: "check_system_basics", + Short: "Icinga check plugin to check various Linux metrics", + Version: version, PersistentPreRun: func(cmd *cobra.Command, args []string) { go check.HandleTimeout(Timeout) }, Run: RunFunction, } -func Execute(version string) { +func Execute() { defer check.CatchPanic() - rootCmd.Version = version - rootCmd.VersionTemplate() - if err := rootCmd.Execute(); err != nil { check.ExitError(err) } @@ -52,8 +54,6 @@ func init() { flagSet := rootCmd.Flags() flagSet.Bool("dump-icinga2-config", false, "Dump icinga2 config for this plugin") - - flagSet.Bool("version", false, "Display version and other information about this program") } func RunFunction(cmd *cobra.Command, args []string) { @@ -69,16 +69,6 @@ func RunFunction(cmd *cobra.Command, args []string) { os.Exit(check.OK) } - showVersion, err := flagSet.GetBool("version") - if err != nil { - check.ExitError(err) - } - - if showVersion { - fmt.Println(cmd.Version) - os.Exit(check.OK) - } - Help(cmd, args) } diff --git a/main.go b/main.go index c1d078e..e219143 100644 --- a/main.go +++ b/main.go @@ -1,32 +1,9 @@ package main import ( - "fmt" - "github.com/NETWAYS/check_system_basics/cmd" ) -var ( - version string - commit = "" - date = "" -) - func main() { - cmd.Execute(buildVersion()) -} - -//goland:noinspection GoBoolExpressions -func buildVersion() string { - result := version - - if commit != "" { - result = fmt.Sprintf("%s\ncommit: %s", result, commit) - } - - if date != "" { - result = fmt.Sprintf("%s\ndate: %s", result, date) - } - - return result + cmd.Execute() } From b30b2c9ccf26e8f6ef8f61b573464a86b8e99b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:03:31 +0200 Subject: [PATCH 2/2] Also add version linking to goreleaser --- .goreleaser.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.goreleaser.yml b/.goreleaser.yml index 2e41128..a3348ec 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -24,3 +24,5 @@ builds: - amd64 - 386 - riscv64 + ldflags: + - -X github.com/NETWAYS/check_system_basics/cmd.version={{.Version}}