-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathversion.go
66 lines (57 loc) · 1.35 KB
/
version.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"runtime/debug"
"strings"
"github.com/alecthomas/kong"
)
type versionFlag bool
func (v versionFlag) BeforeReset(app *kong.Kong) error {
fmt.Fprint(app.Stdout, "git-spice ", _version)
if report := generateBuildReport(); report != "" {
fmt.Fprintf(app.Stdout, " (%s)", report)
}
fmt.Fprintln(app.Stdout)
fmt.Fprintln(app.Stdout, "Copyright (C) 2024 Abhinav Gupta")
fmt.Fprintln(app.Stdout, " <https://github.com/abhinav/git-spice>")
fmt.Fprintln(app.Stdout, "This program comes with ABSOLUTELY NO WARRANTY")
fmt.Fprintln(app.Stdout, "This is free software, and you are welcome to redistribute it")
fmt.Fprintln(app.Stdout, "under certain conditions; see source for details.")
app.Exit(0)
return nil
}
var generateBuildReport = func() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
var (
revision string
dirty bool
time string
)
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
dirty = setting.Value == "true"
case "vcs.time":
time = setting.Value
}
}
var out strings.Builder
if revision != "" {
out.WriteString(revision)
if dirty {
out.WriteString("-dirty")
}
}
if time != "" {
if out.Len() > 0 {
fmt.Fprint(&out, " ")
}
out.WriteString(time)
}
return out.String()
}