-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flag.go
54 lines (47 loc) · 1.02 KB
/
flag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package versioninfo
import (
"flag"
"fmt"
"os"
"strconv"
"time"
)
// AddFlag adds -v and -version flags to the FlagSet.
// If triggered, the flags print version information and call os.Exit(0).
// If FlagSet is nil, it adds the flags to flag.CommandLine.
func AddFlag(f *flag.FlagSet) {
if f == nil {
f = flag.CommandLine
}
f.Var(boolFunc(printVersion), "v", "short alias for -version")
f.Var(boolFunc(printVersion), "version", "print version information and exit")
}
func printVersion(b bool) error {
if !b {
return nil
}
fmt.Println("Version:", Version)
fmt.Println("Revision:", Revision)
if Revision != "unknown" {
fmt.Println("Committed:", LastCommit.Format(time.RFC1123))
if DirtyBuild {
fmt.Println("Dirty Build")
}
}
os.Exit(0)
panic("unreachable")
}
type boolFunc func(bool) error
func (f boolFunc) IsBoolFlag() bool {
return true
}
func (f boolFunc) String() string {
return ""
}
func (f boolFunc) Set(s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
return f(b)
}