Skip to content

Commit

Permalink
Merge pull request #47 from NETWAYS/version_refactor
Browse files Browse the repository at this point in the history
Refactor version logic
  • Loading branch information
RincewindsHat authored Jun 11, 2024
2 parents fffb5b9 + b30b2c9 commit b7d8fd6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ builds:
- amd64
- 386
- riscv64
ldflags:
- -X github.com/NETWAYS/check_system_basics/cmd.version={{.Version}}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 8 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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)
}

Expand Down
25 changes: 1 addition & 24 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit b7d8fd6

Please sign in to comment.