-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (90 loc) · 2.25 KB
/
Copy pathmain.go
File metadata and controls
97 lines (90 loc) · 2.25 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
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"os"
"runtime/debug"
tea "github.com/charmbracelet/bubbletea"
)
// Build metadata. buildTime and gitCommit can be injected at link time via
// -ldflags "-X main.buildTime=... -X main.gitCommit=..." (or -X main.buildDate=...);
// when unset, buildInfo falls back to the VCS data the Go toolchain embeds.
var (
buildTime string
buildDate string
gitCommit string
)
// buildInfo returns the commit the binary was built from and when it was
// compiled, preferring -ldflags values and falling back to Go's embedded VCS info.
func buildInfo() (commit, date string) {
commit = gitCommit
date = buildTime
if date == "" {
date = buildDate
}
dirty := false
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if commit == "" {
commit = s.Value
}
case "vcs.time":
if date == "" {
date = s.Value
}
case "vcs.modified":
dirty = s.Value == "true"
}
}
}
if commit == "" {
commit = "unknown"
}
if date == "" {
date = "unknown"
}
if len(commit) > 7 && commit != "unknown" {
commit = commit[:7]
}
if dirty {
commit += " (dirty)"
}
return commit, date
}
// versionString reports the build's commit, date, and Go version. Fields fall
// back to "unknown" when build info is unavailable (e.g. `go run`).
func versionString() string {
commit, date := buildInfo()
goVer := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
goVer = info.GoVersion
}
return fmt.Sprintf("git_pruner\n commit: %s\n date: %s\n go: %s", commit, date, goVer)
}
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "version", "--version", "-v":
fmt.Println(versionString())
return
}
os.Exit(runCLI(os.Args[1:], os.Stdout, os.Stderr))
}
settingsPath = defaultSettingsPath()
m, err := startupModel()
if err != nil {
fmt.Fprintln(os.Stderr, "git_pruner:", err)
os.Exit(1)
}
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
final, err := p.Run()
if err != nil {
fmt.Fprintln(os.Stderr, "git_pruner:", err)
os.Exit(1)
}
// Printed after the alt screen closes, so the commands stay in scrollback.
if fm, ok := final.(model); ok {
fmt.Print(restoreSummary(append(fm.history, fm.results...)))
}
}