From 0a27ca67d00c2f3e3510fa523be506e021a83ca4 Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Mon, 9 Feb 2015 16:14:55 -0500 Subject: [PATCH] Add version info to binaries --- main.go | 6 ++++++ version.go | 15 +++++++++++++++ version_test.go | 13 +++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 version.go create mode 100644 version_test.go diff --git a/main.go b/main.go index 125f44c..e9a61c7 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "path/filepath" ) +var version = flag.Bool("version", false, "print version information and exit") var list = flag.Bool("list", false, "list mode") var host = flag.String("host", "", "host mode") @@ -14,6 +15,11 @@ func main() { flag.Parse() file := flag.Arg(0) + if *version == true { + fmt.Printf("%s version %d\n", os.Args[0], versionInfo()) + return + } + if file == "" { fmt.Printf("Usage: %s [options] path\n", os.Args[0]) os.Exit(1) diff --git a/version.go b/version.go new file mode 100644 index 0000000..a520ffe --- /dev/null +++ b/version.go @@ -0,0 +1,15 @@ +package main + +// Deliberately uninitialized. See below. +var build_version string + +// versionInfo returns a string containing the version information of the +// current build. It's empty by default, but can be included as part of the +// build process by setting the main.build_version variable. +func versionInfo() string { + if build_version != "" { + return build_version + } else { + return "unknown" + } +} diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..cea3ffa --- /dev/null +++ b/version_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestVersionInfo(t *testing.T) { + assert.Equal(t, "unknown", versionInfo()) + + build_version = "vXYZ" + assert.Equal(t, "vXYZ", versionInfo()) +}