-
Notifications
You must be signed in to change notification settings - Fork 45
/
version.go
93 lines (83 loc) · 2.25 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
88
89
90
91
92
93
package cli
import (
"context"
"fmt"
"runtime"
"github.com/blang/semver"
"github.com/caarlos0/spin"
su "github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/spf13/cobra"
bi "github.com/textileio/textile/v2/buildinfo"
"github.com/textileio/textile/v2/cmd"
)
func getLatestRelease() (*su.Release, error) {
s := spin.New("%s Checking for new Hub CLI version")
s.Start()
defer s.Stop()
config := su.Config{
Filters: []string{
"hub_v",
},
}
updater, err := su.NewUpdater(config)
if err != nil {
return nil, err
}
latest, found, err := updater.DetectLatest(cmd.Repo)
if err != nil {
return nil, err
} else if !found {
return nil, fmt.Errorf("%s_%s build not found", runtime.GOOS, runtime.GOARCH)
}
return latest, nil
}
func getAPIVersion() (string, error) {
s := spin.New("%s Checking Hub API version")
s.Start()
defer s.Stop()
ctx, cancel := context.WithTimeout(Auth(context.Background()), cmd.Timeout)
defer cancel()
res, err := clients.Hub.BuildInfo(ctx)
if err != nil {
return "", fmt.Errorf("getting API build info: %v", err)
}
if res.GitSummary == "" {
res.GitSummary = "git"
}
return res.GitSummary, nil
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Show version info",
Long: `Shows version info.`,
Args: cobra.ExactArgs(0),
Run: func(c *cobra.Command, args []string) {
isGovvvBuild := bi.GitSummary != ""
isReleaseBuild := bi.Version != "git"
displayVersion := bi.Version
if !isReleaseBuild && isGovvvBuild {
displayVersion = bi.GitSummary
}
cmd.Message("%s", aurora.Green(displayVersion))
apiVersion, err := getAPIVersion()
if err != nil {
cmd.Warn("Unable to get Hub API version: %v", err)
} else {
cmd.Message("Hub API: %s", apiVersion)
}
latestRelease, err := getLatestRelease()
if err != nil {
cmd.Warn("Unable to get latest version: %v", err)
} else {
latest := "v" + latestRelease.Version.String()
current, err := semver.ParseTolerant(bi.Version)
if err != nil || current.LT(latestRelease.Version) {
// Display message if outdated of we're running a custom build
cmd.Message("New version of hub available! %s -> %s. Run %s to update.",
aurora.Red(displayVersion),
aurora.Cyan(latest),
aurora.White("hub update").Bold())
}
}
},
}