Skip to content

Commit 0a27ca6

Browse files
committed
Add version info to binaries
1 parent d536bd0 commit 0a27ca6

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import (
77
"path/filepath"
88
)
99

10+
var version = flag.Bool("version", false, "print version information and exit")
1011
var list = flag.Bool("list", false, "list mode")
1112
var host = flag.String("host", "", "host mode")
1213

1314
func main() {
1415
flag.Parse()
1516
file := flag.Arg(0)
1617

18+
if *version == true {
19+
fmt.Printf("%s version %d\n", os.Args[0], versionInfo())
20+
return
21+
}
22+
1723
if file == "" {
1824
fmt.Printf("Usage: %s [options] path\n", os.Args[0])
1925
os.Exit(1)

version.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
// Deliberately uninitialized. See below.
4+
var build_version string
5+
6+
// versionInfo returns a string containing the version information of the
7+
// current build. It's empty by default, but can be included as part of the
8+
// build process by setting the main.build_version variable.
9+
func versionInfo() string {
10+
if build_version != "" {
11+
return build_version
12+
} else {
13+
return "unknown"
14+
}
15+
}

version_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestVersionInfo(t *testing.T) {
9+
assert.Equal(t, "unknown", versionInfo())
10+
11+
build_version = "vXYZ"
12+
assert.Equal(t, "vXYZ", versionInfo())
13+
}

0 commit comments

Comments
 (0)