/
update.go
67 lines (59 loc) · 1.65 KB
/
update.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
package cmd
import (
"github.com/Masterminds/semver"
"github.com/caicloud/build-infra/pkg/update"
"github.com/caicloud/build-infra/pkg/version"
"github.com/caicloud/nirvana/cli"
"github.com/caicloud/nirvana/log"
"github.com/spf13/cobra"
)
// NewCmdUpdate ...
func NewCmdUpdate() *cli.Command {
cmd := cli.NewCommand(&cobra.Command{
Use: "update",
Short: "update caimake",
Run: func(cmd *cobra.Command, args []string) {
latest, err := update.GetGithubLatestRelease(cli.GetString("repo"))
if err != nil {
log.Fatal(err)
}
current := version.Get().Version
if cli.GetBool("check") {
log.Infof("Current version: %v", current)
log.Infof("Latest version: %v", latest.Version)
return
}
latestSemver, err := semver.NewVersion(latest.Version)
if err != nil {
log.Fatalf("latest version [%v] does not follow semantic version, %v", latest.Version, err)
}
currentSemver, err := semver.NewVersion(current)
if err != nil {
log.Fatalf("current version [%v] does not follow semantic version, %v", current, err)
}
if currentSemver.GreaterThan(latestSemver) || currentSemver.Equal(latestSemver) {
log.Infof("The binary is up to date!")
log.Infof("Caimake %v is currently the newest version available.", current)
return
}
err = update.DoUpdate(latest.DownloadURL, latest.Size)
if err != nil {
log.Fatal(err)
}
},
})
cmd.AddFlag(
cli.BoolFlag{
Name: "check",
Shorthand: "c",
Usage: "Check latest version only",
},
cli.StringFlag{
Name: "repo",
Shorthand: "r",
Usage: "Github repo name",
DefValue: "caicloud/build-infra",
},
)
return cmd
}