Skip to content

Commit

Permalink
Merge pull request #15 from OpenCHAMI/synackd/fix-version
Browse files Browse the repository at this point in the history
fix: printing of version info
  • Loading branch information
alexlovelltroy authored Jan 8, 2025
2 parents b0c2802 + 60e7e1c commit ba9b7bc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
20 changes: 10 additions & 10 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ builds:
- v3

# If you want to build this locally, you can set the following environment variables:
# 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:
- "-s -w -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 }} "
- "-s -w \
-X github.com/OpenCHAMI/coresmd/internal/version.GitCommit={{ .Commit }} \
-X github.com/OpenCHAMI/coresmd/internal/version.BuildTime={{ .Date }} \
-X github.com/OpenCHAMI/coresmd/internal/version.Version={{ .Version }} \
-X github.com/OpenCHAMI/coresmd/internal/version.GitBranch={{ .Branch }} \
-X github.com/OpenCHAMI/coresmd/internal/version.GitTag={{ .Tag }} \
-X github.com/OpenCHAMI/coresmd/internal/version.GitState={{ .GitTreeState }} \
-X github.com/OpenCHAMI/coresmd/internal/version.BuildHost={{ .Env.BUILD_HOST }} \
-X github.com/OpenCHAMI/coresmd/internal/version.GoVersion={{ .Env.GO_VERSION }} \
-X github.com/OpenCHAMI/coresmd/internal/version.BuildUser={{ .Env.BUILD_USER }} "
binary: coredhcp
env:
# The bootloop plugin uses sqlite3 which requires CGO.
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ using GoReleaser.
To include detailed build metadata, ensure the following environment variables
are set:

* __GIT_STATE__: Indicates whether there are uncommitted changes in the working
directory. Set to clean if the repository is clean, or dirty if there are
uncommitted changes.
* __BUILD_HOST__: The hostname of the machine where the build is being
performed.
* __GO_VERSION__: The version of Go used for the build. GoReleaser uses this to
Expand Down
8 changes: 7 additions & 1 deletion bootloop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ var Plugin = plugins.Plugin{
Setup4: setup4,
}

func logVersion() {
log.Infof("initializing coresmd/bootloop %s (%s), built %s", version.Version, version.GitState, version.BuildTime)
log.WithFields(version.VersionInfo).Debugln("detailed version info")
}

func setup6(args ...string) (handler.Handler6, error) {
logVersion()
return nil, errors.New("bootloop does not currently support DHCPv6")
}

func setup4(args ...string) (handler.Handler4, error) {
log.Infof("initializing coresmd/bootloop %s (%s), built %s", version.Version, version.GitCommit, version.BuildTime)
logVersion()

// Ensure all required args were passed
if len(args) != 5 {
Expand Down
7 changes: 6 additions & 1 deletion coresmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ const (
defaultTFTPPort = 69
)

func logVersion() {
log.Infof("initializing coresmd/coresmd %s (%s), built %s", version.Version, version.GitState, version.BuildTime)
log.WithFields(version.VersionInfo).Debugln("detailed version info")
}

func setup6(args ...string) (handler.Handler6, error) {
return nil, errors.New("coresmd does not currently support DHCPv6")
}

func setup4(args ...string) (handler.Handler4, error) {
log.Infof("initializing coresmd/coresmd %s (%s), built %s", version.Version, version.GitCommit, version.BuildTime)
logVersion()

// Ensure all required args were passed
if len(args) != 6 {
Expand Down
38 changes: 22 additions & 16 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package version

import "fmt"
import "runtime"

// GitCommit stores the latest Git commit hash.
// Set via -ldflags "-X main.GitCommit=$(git rev-parse HEAD)"
Expand Down Expand Up @@ -39,20 +39,26 @@ var GoVersion string
// 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)
}
// Compiler that built the running binary.
var Compiler string = runtime.Compiler

// CPU architecture of the running binary.
var Arch string = runtime.GOARCH

// OS the running binary was built for.
var Os string = runtime.GOOS

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)
// VersionInfo is all of the fields above combined into a map to be logged.
var VersionInfo = map[string]interface{}{
"version": Version,
"tag": GitTag,
"commit": GitCommit,
"branch": GitBranch,
"state": GitState,
"build_timestamp": BuildTime,
"build_host": BuildHost,
"build_user": BuildUser,
"go_version": GoVersion,
"arch": Arch,
"os": Os,
}

0 comments on commit ba9b7bc

Please sign in to comment.