Skip to content

Commit 18dd56a

Browse files
committed
add version
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 123cc2a commit 18dd56a

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

version/version.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package version
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"runtime"
7+
"text/template"
8+
)
9+
10+
type (
11+
Version struct {
12+
App string `json:"app"`
13+
Author string `json:"author"`
14+
Version string `json:"version"`
15+
GitCommit string `json:"gitCommit"`
16+
GitTag string `json:"gitTag"`
17+
BuildDate string `json:"buildDate"`
18+
Go string `json:"go"`
19+
Arch string `json:"arch"`
20+
}
21+
)
22+
23+
const DefaultTitle = `{{.App}} v{{.Version}} ({{.GitCommit}}); {{.Go}}/{{.Arch}}; by {{.Author}}; built at {{.BuildDate}})`
24+
25+
func New() *Version {
26+
ver := Version{
27+
Go: runtime.Version(),
28+
Arch: runtime.GOARCH,
29+
}
30+
31+
return &ver
32+
}
33+
34+
func (v *Version) SetApp(val string) *Version {
35+
v.App = val
36+
return v
37+
}
38+
39+
func (v *Version) SetVersion(val string) *Version {
40+
v.GitCommit = val
41+
return v
42+
}
43+
44+
func (v *Version) SetGitCommit(val string) *Version {
45+
v.GitCommit = val
46+
return v
47+
}
48+
49+
func (v *Version) SetGitTag(val string) *Version {
50+
v.GitTag = val
51+
return v
52+
}
53+
54+
func (v *Version) SetBuildDate(val string) *Version {
55+
v.GitTag = val
56+
return v
57+
}
58+
59+
func (v *Version) Title(tmpl *string) string {
60+
var buf bytes.Buffer
61+
62+
if tmpl != nil {
63+
tmp := DefaultTitle
64+
tmpl = &tmp
65+
}
66+
67+
// parse template
68+
t, err := template.New("title").Parse(*tmpl)
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
// execute template
74+
err = t.Execute(&buf, v)
75+
if err != nil {
76+
panic(err)
77+
}
78+
79+
return buf.String()
80+
}
81+
82+
func (v *Version) BuildVersionLine(tmpl *string) (ret string) {
83+
if v == nil {
84+
content, err := json.Marshal(v)
85+
if err != nil {
86+
panic(err)
87+
}
88+
89+
ret = string(content)
90+
} else {
91+
var buf bytes.Buffer
92+
93+
// parse template
94+
t, err := template.New("version").Parse(*tmpl)
95+
if err != nil {
96+
panic(err)
97+
}
98+
99+
// execute template
100+
err = t.Execute(&buf, v)
101+
if err != nil {
102+
panic(err)
103+
}
104+
105+
ret = buf.String()
106+
}
107+
108+
return
109+
}

0 commit comments

Comments
 (0)