-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (63 loc) · 1.84 KB
/
main.go
File metadata and controls
77 lines (63 loc) · 1.84 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
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/bsv-blockchain/teranode/cmd/teranode"
)
// Name used by build script for the binaries. (Please keep on single line)
const progname = "teranode"
// Version & commit strings injected at build with -ldflags -X...
var version string
var commit string
func init() {
// If version and commit are empty (running via go run), populate them at runtime
if version == "" && commit == "" {
populateVersionInfo()
}
}
func populateVersionInfo() {
commit = runGitCommand("unknown", "rev-parse", "--short", "HEAD")
gitTag := runGitCommand("", "describe", "--tags", "--exact-match")
if gitTag != "" && strings.HasPrefix(gitTag, "v") {
version = gitTag
return
}
timestamp := runGitCommand(
time.Now().Format("20060102150405"),
"show", "-s", "--format=%cd", "--date=format:%Y%m%d%H%M%S", "HEAD",
)
if commit == "unknown" {
version = fmt.Sprintf("v0.0.0-%s-unknown", timestamp)
} else {
version = fmt.Sprintf("v0.0.0-%s-%s", timestamp, commit)
}
}
// runGitCommand runs a git command and returns the trimmed output, or fallback on failure.
func runGitCommand(fallback string, args ...string) string {
output, err := exec.Command("git", args...).Output()
if err != nil {
return fallback
}
return strings.TrimSpace(string(output))
}
func main() {
// Check for --version flag before any initialization
for _, arg := range os.Args[1:] {
if arg == "--version" || arg == "-v" {
fmt.Printf("%s version %s (commit: %s)\n", progname, version, commit)
os.Exit(0)
}
}
// Verify 64-bit architecture
if strconv.IntSize != 64 {
fmt.Fprintf(os.Stderr, "Error: %s requires a 64-bit architecture. Current architecture: %s\n", progname, runtime.GOARCH)
os.Exit(1)
}
// If not showing version, run the daemon
teranode.RunDaemon(progname, version, commit)
}