mirror of
https://github.com/onyx-and-iris/gobs-cli.git
synced 2025-06-09 13:10:35 +01: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:
parent
92761ab1b3
commit
17b8e53da3
@ -7,6 +7,8 @@ vars:
|
|||||||
PROGRAM: gobs-cli
|
PROGRAM: gobs-cli
|
||||||
SHELL: '{{if eq .OS "Windows_NT"}}powershell{{end}}'
|
SHELL: '{{if eq .OS "Windows_NT"}}powershell{{end}}'
|
||||||
BIN_DIR: bin
|
BIN_DIR: bin
|
||||||
|
VERSION:
|
||||||
|
sh: 'git describe --tags $(git rev-list --tags --max-count=1)'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
@ -35,13 +37,13 @@ tasks:
|
|||||||
build-windows:
|
build-windows:
|
||||||
desc: Build the gobs-cli project for Windows
|
desc: Build the gobs-cli project for Windows
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=windows GOARCH=amd64 go build -ldflags "-X 'main.version=debug'" -o {{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe
|
- GOOS=windows GOARCH=amd64 go build -ldflags "-X 'main.version={{.VERSION}}'" -o {{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe
|
||||||
internal: true
|
internal: true
|
||||||
|
|
||||||
build-linux:
|
build-linux:
|
||||||
desc: Build the gobs-cli project for Linux
|
desc: Build the gobs-cli project for Linux
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=linux GOARCH=amd64 go build -ldflags "-X 'main.version=debug'" -o {{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64
|
- GOOS=linux GOARCH=amd64 go build -ldflags "-X 'main.version={{.VERSION}}'" -o {{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64
|
||||||
internal: true
|
internal: true
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
12
version.go
12
version.go
@ -2,11 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "unknown"
|
var version string
|
||||||
|
|
||||||
// VersionFlag is a custom flag type for displaying version information.
|
// VersionFlag is a custom flag type for displaying version information.
|
||||||
type VersionFlag string
|
type VersionFlag string
|
||||||
@ -19,6 +21,14 @@ func (v VersionFlag) IsBool() bool { return true }
|
|||||||
|
|
||||||
// BeforeApply implements the kong.Flag interface for VersionFlag.
|
// BeforeApply implements the kong.Flag interface for VersionFlag.
|
||||||
func (v VersionFlag) BeforeApply(app *kong.Kong, _ kong.Vars) error { // nolint: unparam
|
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)
|
fmt.Printf("gobs-cli version: %s\n", version)
|
||||||
app.Exit(0) // Exit the application after printing the version
|
app.Exit(0) // Exit the application after printing the version
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user