Skip to content

Commit e17dc82

Browse files
committedSep 6, 2018
Added version sub-command
1 parent 5e8ccda commit e17dc82

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
 

‎cmd_version.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Show our version.
3+
//
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"runtime"
12+
13+
"github.com/google/subcommands"
14+
)
15+
16+
var (
17+
version = "unreleased"
18+
)
19+
20+
type versionCmd struct {
21+
verbose bool
22+
}
23+
24+
//
25+
// Glue
26+
//
27+
func (*versionCmd) Name() string { return "version" }
28+
func (*versionCmd) Synopsis() string { return "Show our version." }
29+
func (*versionCmd) Usage() string {
30+
return `version :
31+
Report upon our version, and exit.
32+
`
33+
}
34+
35+
//
36+
// Flag setup
37+
//
38+
func (p *versionCmd) SetFlags(f *flag.FlagSet) {
39+
f.BoolVar(&p.verbose, "verbose", false, "Show go version the binary was generated with.")
40+
}
41+
42+
//
43+
// Show the version - using the "out"-writer.
44+
//
45+
func showVersion(verbose bool) {
46+
fmt.Printf("%s\n", version)
47+
if verbose {
48+
fmt.Printf("Built with %s\n", runtime.Version())
49+
}
50+
}
51+
52+
//
53+
// Entry-point.
54+
//
55+
func (p *versionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
56+
57+
showVersion(p.verbose)
58+
return subcommands.ExitSuccess
59+
}

‎main.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func main() {
2323
subcommands.Register(&lexCmd{}, "")
2424
subcommands.Register(&parseCmd{}, "")
2525
subcommands.Register(&runCmd{}, "")
26+
subcommands.Register(&versionCmd{}, "")
2627

2728
flag.Parse()
2829
ctx := context.Background()

0 commit comments

Comments
 (0)
Please sign in to comment.