mirror of
https://github.com/onyx-and-iris/gobs-cli.git
synced 2026-04-18 07:03:37 +00:00
read version from build info if version was not injected at build time (go install)
inject version from tag at build time for local builds
This commit is contained in:
12
version.go
12
version.go
@@ -2,11 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/kong"
|
||||
)
|
||||
|
||||
var version = "unknown"
|
||||
var version string
|
||||
|
||||
// VersionFlag is a custom flag type for displaying version information.
|
||||
type VersionFlag string
|
||||
@@ -19,6 +21,14 @@ func (v VersionFlag) IsBool() bool { return true }
|
||||
|
||||
// BeforeApply implements the kong.Flag interface for VersionFlag.
|
||||
func (v VersionFlag) BeforeApply(app *kong.Kong, _ kong.Vars) error { // nolint: unparam
|
||||
if version == "" {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to read build info")
|
||||
}
|
||||
version = strings.Split(info.Main.Version, "-")[0]
|
||||
}
|
||||
|
||||
fmt.Printf("gobs-cli version: %s\n", version)
|
||||
app.Exit(0) // Exit the application after printing the version
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user