diff --git a/.goreleaser.yaml b/.goreleaser.yaml index de74958..87784a2 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -39,6 +39,17 @@ builds: - id: smd-init main: ./cmd/smd-init/ binary: smd-init + 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 }} \ + -extldflags '-static'" goos: - linux - darwin @@ -50,6 +61,17 @@ builds: - id: smd-loader main: ./cmd/smd-loader/ binary: smd-loader + 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 }} \ + -extldflags '-static'" goos: - linux - darwin diff --git a/cmd/smd-init/main.go b/cmd/smd-init/main.go index 15c5066..e060a59 100644 --- a/cmd/smd-init/main.go +++ b/cmd/smd-init/main.go @@ -174,6 +174,7 @@ func parseCmdLine() { var lg = log.New(os.Stdout, "", log.Lshortfile|log.LstdFlags|log.Lmicroseconds) func main() { + PrintVersionInfo() parseCmdLine() lg.Printf("smd-init: Starting...") diff --git a/cmd/smd-init/version.go b/cmd/smd-init/version.go new file mode 100644 index 0000000..1b848c0 --- /dev/null +++ b/cmd/smd-init/version.go @@ -0,0 +1,58 @@ +package main + +import "fmt" + +// 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) +} diff --git a/cmd/smd-loader/main.go b/cmd/smd-loader/main.go index 8b65a77..a49b15b 100644 --- a/cmd/smd-loader/main.go +++ b/cmd/smd-loader/main.go @@ -98,6 +98,7 @@ func nodeNids(hsmURL string) { } func main() { + PrintVersionInfo() var cancel context.CancelFunc ctx, cancel = context.WithCancel(context.Background()) diff --git a/cmd/smd-loader/version.go b/cmd/smd-loader/version.go new file mode 100644 index 0000000..1b848c0 --- /dev/null +++ b/cmd/smd-loader/version.go @@ -0,0 +1,58 @@ +package main + +import "fmt" + +// 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) +}