mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Refactor how versioning information is indicated in the build and in the source.
This commit is contained in:
parent
237eb6219b
commit
6f18094680
3 changed files with 74 additions and 67 deletions
|
|
@ -8,20 +8,23 @@ before:
|
|||
|
||||
builds:
|
||||
- binary: magellan
|
||||
main: ./main.go
|
||||
# export GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)
|
||||
# export BUILD_HOST=$(hostname)
|
||||
# export GO_VERSION=$(go version | awk '{print $3}')
|
||||
# export BUILD_USER=$(whoami)
|
||||
ldflags:
|
||||
- "-X main.GitCommit={{.Commit}} \
|
||||
-X main.BuildTime={{.Timestamp}} \
|
||||
-X main.Version={{.Version}} \
|
||||
-X main.GitBranch={{.Branch}} \
|
||||
-X main.GitTag={{.Tag}} \
|
||||
-X main.GitState={{ .Env.GIT_STATE }} \
|
||||
-X main.BuildHost={{ .Env.BUILD_HOST }} \
|
||||
-X main.GoVersion={{ .Env.GO_VERSION }} \
|
||||
-X main.BuildUser={{ .Env.BUILD_USER }} "
|
||||
- "-X github.com/OpenCHAMI/magellan/internal/version.GitCommit={{ .Commit }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.BuildTime={{ .Timestamp }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.Version={{ .Version }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.GitBranch={{ .Branch }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.GitTag={{ .Tag }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.GitState={{ .Env.GIT_STATE }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.BuildHost={{ .Env.BUILD_HOST }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.GoVersion={{ .Env.GO_VERSION }} \
|
||||
-X github.com/OpenCHAMI/magellan/internal/version.BuildUser={{ .Env.BUILD_USER }} "
|
||||
tags:
|
||||
- version
|
||||
goos:
|
||||
- linux
|
||||
- darwin
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/internal/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -10,65 +9,10 @@ var versionCmd = &cobra.Command{
|
|||
Use: "version",
|
||||
Short: "Print version info and exit",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
PrintVersionInfo()
|
||||
version.PrintVersionInfo()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
}
|
||||
|
||||
// GitCommit stores the latest Git commit hash.
|
||||
// Set via -ldflags "-X main.GitCommit=$(git rev-parse HEAD)"
|
||||
var GitCommit string
|
||||
|
||||
// BuildTime stores the build timestamp in UTC.
|
||||
// Set via -ldflags "-X main.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
var BuildTime string
|
||||
|
||||
// Version indicates the version of the binary, such as a release number or semantic version.
|
||||
// Set via -ldflags "-X main.Version=v1.0.0"
|
||||
var Version string
|
||||
|
||||
// GitBranch holds the name of the Git branch from which the build was created.
|
||||
// Set via -ldflags "-X main.GitBranch=$(git rev-parse --abbrev-ref HEAD)"
|
||||
var GitBranch string
|
||||
|
||||
// GitTag represents the most recent Git tag at build time, if any.
|
||||
// Set via -ldflags "-X main.GitTag=$(git describe --tags --abbrev=0)"
|
||||
var GitTag string
|
||||
|
||||
// GitState indicates whether the working directory was "clean" or "dirty" (i.e., with uncommitted changes).
|
||||
// Set via -ldflags "-X main.GitState=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)"
|
||||
var GitState string
|
||||
|
||||
// BuildHost stores the hostname of the machine where the binary was built.
|
||||
// Set via -ldflags "-X main.BuildHost=$(hostname)"
|
||||
var BuildHost string
|
||||
|
||||
// GoVersion captures the Go version used to build the binary.
|
||||
// Typically, this can be obtained automatically with runtime.Version(), but you can set it manually.
|
||||
// Set via -ldflags "-X main.GoVersion=$(go version | awk '{print $3}')"
|
||||
var GoVersion string
|
||||
|
||||
// BuildUser is the username of the person or system that initiated the build process.
|
||||
// Set via -ldflags "-X main.BuildUser=$(whoami)"
|
||||
var BuildUser string
|
||||
|
||||
// PrintVersionInfo outputs all versioning information for troubleshooting or version checks.
|
||||
func PrintVersionInfo() {
|
||||
fmt.Printf("Version: %s\n", Version)
|
||||
fmt.Printf("Git Commit: %s\n", GitCommit)
|
||||
fmt.Printf("Build Time: %s\n", BuildTime)
|
||||
fmt.Printf("Git Branch: %s\n", GitBranch)
|
||||
fmt.Printf("Git Tag: %s\n", GitTag)
|
||||
fmt.Printf("Git State: %s\n", GitState)
|
||||
fmt.Printf("Build Host: %s\n", BuildHost)
|
||||
fmt.Printf("Go Version: %s\n", GoVersion)
|
||||
fmt.Printf("Build User: %s\n", BuildUser)
|
||||
}
|
||||
|
||||
func VersionInfo() string {
|
||||
return fmt.Sprintf("Version: %s, Git Commit: %s, Build Time: %s, Git Branch: %s, Git Tag: %s, Git State: %s, Build Host: %s, Go Version: %s, Build User: %s",
|
||||
Version, GitCommit, BuildTime, GitBranch, GitTag, GitState, BuildHost, GoVersion, BuildUser)
|
||||
}
|
||||
|
|
|
|||
60
internal/version/version.go
Normal file
60
internal/version/version.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GitCommit stores the latest Git commit hash.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.GitCommit=$(git rev-parse HEAD)"
|
||||
var GitCommit string
|
||||
|
||||
// BuildTime stores the build timestamp in UTC.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
var BuildTime string
|
||||
|
||||
// Version indicates the version of the binary, such as a release number or semantic version.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.Version=v1.0.0"
|
||||
var Version string
|
||||
|
||||
// GitBranch holds the name of the Git branch from which the build was created.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.GitBranch=$(git rev-parse --abbrev-ref HEAD)"
|
||||
var GitBranch string
|
||||
|
||||
// GitTag represents the most recent Git tag at build time, if any.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.GitTag=$(git describe --tags --abbrev=0)"
|
||||
var GitTag string
|
||||
|
||||
// GitState indicates whether the working directory was "clean" or "dirty" (i.e., with uncommitted changes).
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.GitState=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)"
|
||||
var GitState string
|
||||
|
||||
// BuildHost stores the hostname of the machine where the binary was built.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.BuildHost=$(hostname)"
|
||||
var BuildHost string
|
||||
|
||||
// GoVersion captures the Go version used to build the binary.
|
||||
// Typically, this can be obtained automatically with runtime.Version(), but you can set it manually.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.GoVersion=$(go version | awk '{print $3}')"
|
||||
var GoVersion string
|
||||
|
||||
// BuildUser is the username of the person or system that initiated the build process.
|
||||
// Set via -ldflags "-X github.com/OpenCHAMI/magellan/internal/version.BuildUser=$(whoami)"
|
||||
var BuildUser string
|
||||
|
||||
// PrintVersionInfo outputs all versioning information for troubleshooting or version checks.
|
||||
func PrintVersionInfo() {
|
||||
fmt.Printf("Version: %s\n", Version)
|
||||
fmt.Printf("Git Commit: %s\n", GitCommit)
|
||||
fmt.Printf("Build Time: %s\n", BuildTime)
|
||||
fmt.Printf("Git Branch: %s\n", GitBranch)
|
||||
fmt.Printf("Git Tag: %s\n", GitTag)
|
||||
fmt.Printf("Git State: %s\n", GitState)
|
||||
fmt.Printf("Build Host: %s\n", BuildHost)
|
||||
fmt.Printf("Go Version: %s\n", GoVersion)
|
||||
fmt.Printf("Build User: %s\n", BuildUser)
|
||||
}
|
||||
|
||||
func VersionInfo() string {
|
||||
return fmt.Sprintf("Version: %s, Git Commit: %s, Build Time: %s, Git Branch: %s, Git Tag: %s, Git State: %s, Build Host: %s, Go Version: %s, Build User: %s",
|
||||
Version, GitCommit, BuildTime, GitBranch, GitTag, GitState, BuildHost, GoVersion, BuildUser)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue