-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (58 loc) · 1.42 KB
/
main.go
File metadata and controls
68 lines (58 loc) · 1.42 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
package main
import (
"runtime/debug"
"strings"
"github.com/marcus/td/cmd"
"github.com/marcus/td/internal/db"
"github.com/marcus/td/internal/query"
)
// Version may be set at build time via -ldflags "-X main.Version=...".
// If left as "dev", we will attempt to derive a version from Go build info.
var Version = "dev"
func init() {
// Wire up TDQ validator to break import cycle between db and query packages
db.QueryValidator = func(queryStr string) error {
_, err := query.Parse(queryStr)
return err
}
}
func effectiveVersion(v string) string {
// If the build injected a real version, prefer it.
if v != "" && v != "dev" {
return v
}
info, ok := debug.ReadBuildInfo()
if !ok || info == nil {
return v
}
// When installed via `go install module@vX.Y.Z`, this will typically be `vX.Y.Z`.
if info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
// Otherwise, try to provide a slightly more useful dev version.
var rev, modified string
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
rev = s.Value
case "vcs.modified":
modified = s.Value
}
}
if rev != "" {
short := rev
if len(short) > 12 {
short = short[:12]
}
parts := []string{"devel", short}
if modified == "true" {
parts = append(parts, "dirty")
}
return strings.Join(parts, "+")
}
return v
}
func main() {
cmd.SetVersion(effectiveVersion(Version))
cmd.Execute()
}