From feac87bd33d6215798351a769ee5dbe2f3ec0aeb Mon Sep 17 00:00:00 2001 From: Hugo Gonzalez Date: Sun, 22 Jan 2017 22:46:09 +0100 Subject: [PATCH] Add version flag and build information --- cmd/clawiod/build.sh | 7 +++++-- cmd/clawiod/main.go | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cmd/clawiod/build.sh b/cmd/clawiod/build.sh index 34a6b5e..ab42078 100755 --- a/cmd/clawiod/build.sh +++ b/cmd/clawiod/build.sh @@ -1,8 +1,7 @@ #!/usr/bin/env bash # # ClawIO build script. Add build variables to compiled binary. -# -# Usage: +# # Usage: # # $ ./build.bash [output_filename] [git_repo] # @@ -46,6 +45,10 @@ commit_name="${pkg}.gitCommit" commit_value="$(git -C "${git_repo}" rev-parse --short HEAD)" ldflags+=("-X" "\"${commit_name}=${commit_value}\"") +# Application name +app_name="${pkg}.appName" +ldflags+=("-X" "\"${app_name}=${output_filename}\"") + releases_dir=${git_repo}/releases rm -rf ${releases_dir} diff --git a/cmd/clawiod/main.go b/cmd/clawiod/main.go index 5dd4db7..80fbe61 100644 --- a/cmd/clawiod/main.go +++ b/cmd/clawiod/main.go @@ -44,14 +44,31 @@ import ( "strings" ) -var flagConfigurationSource string +var ( + flagConfigurationSource string + flagVersion bool +) + +// Build information obtained with the help of -ldflags +var ( + appName string + buildDate string // date -u + gitTag string // git describe --exact-match HEAD + gitNearestTag string // git describe --abbrev=0 --tags HEAD + gitCommit string // git rev-parse HEAD +) func init() { flag.StringVar(&flagConfigurationSource, "conf", "file:clawiod.conf", "Configuration source where to obtain the configuration") + flag.BoolVar(&flagVersion, "version", false, "Show version") flag.Parse() } func main() { + if flagVersion { + handleVersion() + } + configurationSource, err := getConfigurationSource(flagConfigurationSource) if err != nil { fmt.Println(err) @@ -109,6 +126,15 @@ func main() { } } +func handleVersion() { + // if gitTag is not empty we are on release build + if gitTag != "" { + fmt.Printf("%s %s commit:%s release-build\n", appName, gitNearestTag, gitCommit) + os.Exit(0) + } + fmt.Printf("%s %s commit:%s dev-build\n", appName, gitNearestTag, gitCommit) + os.Exit(0) +} func getUserDriver(config root.Configuration) (root.UserDriver, error) { switch config.GetUserDriver() { case "memuserdriver":