-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
50 lines (42 loc) · 1.26 KB
/
main.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
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/logandavies181/tfd/v2/cmd"
)
var version string
func main() {
cmd.Execute(buildVersion(version))
}
// buildVersion checks if version has been set at build time, otherwise uses debug.ReadBuildInfo to infer a tag.
// debug.ReadBuildInfo behaves differently given the following scenarios so buildVersion does the following:
// Locally compiled - Main.Version = "devel"; return "0.0.0+devel"
// go get tfd@<some git hash> - Main.Version = "0.0.0+v0.0.0-<timestamp>-<short_hash>"; return 0.0.0-<timestamp>-<short_hash>
// go get tfd@<tag> - Main.Version = "0.0.0-v<tag>"; return "<tag>"
func buildVersion(version string) string {
if version == "" {
debugInfo, ok := debug.ReadBuildInfo()
if !ok {
fmt.Fprintln(os.Stderr, "Could not determine build info. Your tfd version is corrupt")
os.Exit(1)
}
version = debugInfo.Main.Version
if version == "(devel)" {
return "0.0.0+devel"
}
foundVersionStart := false
version = strings.TrimLeftFunc(version, func(r rune) bool {
if r == 'v' {
// only false for first v - trim it but not the others
if foundVersionStart {
return false
}
foundVersionStart = true
}
return !foundVersionStart
})
}
return version
}