-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
87 lines (68 loc) · 1.72 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cli
import (
"encoding/json"
"fmt"
"net/http"
"time"
"golang.org/x/mod/semver"
)
const defaultVersion = "v0.0.0"
//nolint:gochecknoglobals // are set during build
var (
version = defaultVersion
commit = ""
date = time.Now().UTC().Format(time.RFC3339)
)
type VersionCliHandler struct {
Short bool `help:"Print only the version"`
SkipVersionCheck bool `help:"Skip version check"`
Client *http.Client `kong:"-"`
}
func (h VersionCliHandler) Run() error {
if h.Short {
fmt.Println(version)
return nil
}
if version == defaultVersion {
fmt.Println("Version is not set. This is a development build.")
return nil
}
fmt.Printf(`%s
Commit: %s
Built at %s
`, version, commit, date)
if h.SkipVersionCheck {
return nil
}
client := h.Client
if client == nil {
client = http.DefaultClient
}
resp, err := client.Get("https://api.github.com/repos/prskr/git-age/releases/latest")
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
//nolint:goerr113 // no need to wrap - there's no point to wrap this error
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
var release releaseInfo
if err := decoder.Decode(&release); err != nil {
return err
}
if result := semver.Compare(release.TagName, version); result > 0 {
fmt.Printf("A new version is available: %s 👉\n", release.TagName)
} else if result == 0 {
fmt.Println("You are using the latest version 🎉")
} else {
fmt.Println("You're using a version that is somewhat newer than the latest release! 👻")
}
return nil
}
type releaseInfo struct {
TagName string `json:"tag_name"`
}