-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
114 lines (93 loc) · 2.02 KB
/
version.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package version
import (
"flag"
"fmt"
"github.com/conductant/gohm/pkg/command"
"io"
"os"
)
var (
flagVersion = flag.Bool("version", false, "Show version info")
// These are bound by the -X key value ldflags option of the go compiler
gitRepo string
gitBranch string
gitTag string
gitCommitHash string
buildTimestamp string
buildNumber string
buildLabel string
)
var (
build_info = &Build{
Timestamp: buildTimestamp,
Label: buildLabel,
Number: buildNumber,
RepoUrl: gitRepo,
Tag: gitTag,
Branch: gitBranch,
Commit: gitCommitHash,
}
)
func init() {
command.Register("version", func() (command.Module, command.ErrorHandling) {
return BuildInfo(), command.PanicOnError
})
}
type Build struct {
RepoUrl string
Branch string
Tag string
Commit string
Label string
Timestamp string
Number string
}
func BuildInfo() *Build {
return build_info
}
func SetBuildInfo(b *Build) {
build_info = b
}
func (this *Build) HandleFlag() {
if *flagVersion {
fmt.Println(this.Notice())
os.Exit(0)
}
}
func (this *Build) Notice() string {
return fmt.Sprintf("%s: Repo=%s, Version=%s:%s, Build=%s, Label=%s, BuiltOn=%s",
os.Args[0], this.RepoUrl, this.Tag, this.Commit, this.Number, this.Label, this.Timestamp)
}
func (this *Build) GetRepoUrl() string {
return this.RepoUrl
}
func (this *Build) GetBranch() string {
return this.Branch
}
func (this *Build) GetTag() string {
return this.Tag
}
func (this *Build) GetCommitHash() string {
return this.Commit
}
func (this *Build) GetTimestamp() string {
return this.Timestamp
}
func (this *Build) GetNumber() string {
return this.Number
}
func (this *Build) GetLabel() string {
return this.Label
}
func (this *Build) Help(w io.Writer) {
fmt.Fprintln(w, "Prints the build version.")
}
func (this *Build) RegisterFlags(fs *flag.FlagSet) {
}
func (this *Build) Run(args []string, w io.Writer) error {
fmt.Fprintf(w, "%s\n", this.Notice())
return nil
}
func (this *Build) Close() error {
return nil
}