-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
87 lines (76 loc) · 1.75 KB
/
Copy pathversion.go
File metadata and controls
87 lines (76 loc) · 1.75 KB
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
// Package version provides a simple way to version an executable.
package version
import (
"bytes"
"fmt"
"os"
"text/template"
)
// Use package variables because they can be set linker flags.
var (
Version string
FullVersion string
Date string
Commit string
)
// Printer allows the user to set a custom print template.
type Printer struct {
FullTemplate string
Template string
Flags []string
}
// NewPrinter sets the default print template.
func NewPrinter() *Printer {
p := Printer{}
p.Flags = []string{"version", "-version", "--version"}
p.Template = "{{.Version}}"
p.FullTemplate = ""
p.FullTemplate += "Version: {{.Version}}\n"
p.FullTemplate += "Build Date: {{.Date}}\n"
p.FullTemplate += "Commit: {{.Commit}}\n"
return &p
}
// FlagIsSet returns true if a version flag is set.
func (p *Printer) FlagIsSet(arg string) bool {
for _, flag := range p.Flags {
if arg == flag {
return true
}
}
return false
}
// Print prints version info, conditioned on the user input.
func (p *Printer) Print() error {
if len(os.Args) < 2 {
return nil
}
// Check if user is asking for version information.
if p.FlagIsSet(os.Args[1]) {
var tmplt string
if Version == FullVersion {
tmplt = p.Template
} else {
tmplt = p.FullTemplate
}
// Compile the print template.
t := template.Must(template.New("VersionPrinter").Parse(tmplt))
var b bytes.Buffer
// Execute the template and output the result.
err := t.Execute(&b, map[string]string{
"Version": Version,
"Date": Date,
"Commit": Commit,
})
if err != nil {
return err
}
fmt.Println(b.String())
os.Exit(0)
}
return nil
}
// Print is a shortcut to printing with the default template.
func Print() {
p := NewPrinter()
p.Print()
}