-
Notifications
You must be signed in to change notification settings - Fork 230
/
update.go
135 lines (111 loc) · 2.97 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"fmt"
"time"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/CircleCI-Public/circleci-cli/telemetry"
"github.com/CircleCI-Public/circleci-cli/update"
"github.com/CircleCI-Public/circleci-cli/version"
"github.com/spf13/cobra"
"github.com/briandowns/spinner"
)
type updateCommandOptions struct {
cfg *settings.Config
dryRun bool
args []string
}
func newUpdateCommand(config *settings.Config) *cobra.Command {
opts := updateCommandOptions{
cfg: config,
dryRun: false,
}
update := &cobra.Command{
Use: "update",
Short: "Update the tool to the latest version",
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
opts.cfg.SkipUpdateCheck = true
telemetryClient, ok := telemetry.FromContext(cmd.Context())
if ok {
_ = telemetryClient.Track(telemetry.CreateUpdateEvent(telemetry.GetCommandInformation(cmd, cmd.Name() != "update")))
}
},
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return updateCLI(opts)
},
}
update.AddCommand(&cobra.Command{
Use: "check",
Hidden: true,
Short: "Check if there are any updates available",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
opts.dryRun = true
},
RunE: func(_ *cobra.Command, _ []string) error {
return updateCLI(opts)
},
})
update.AddCommand(&cobra.Command{
Use: "install",
Hidden: true,
Short: "Update the tool to the latest version",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return updateCLI(opts)
},
})
update.AddCommand(&cobra.Command{
Use: "build-agent",
Hidden: true,
Short: "This command has no effect, and is kept for backwards compatibility",
PreRun: func(cmd *cobra.Command, args []string) {
opts.args = args
},
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
})
update.PersistentFlags().BoolVar(&opts.dryRun, "check", false, "Check if there are any updates available without installing")
return update
}
func updateCLI(opts updateCommandOptions) error {
slug := "CircleCI-Public/circleci-cli"
spr := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
spr.Suffix = " Checking for updates..."
spr.Start()
check, err := update.CheckForUpdates(opts.cfg.GitHubAPI, slug, version.Version, version.PackageManager())
spr.Stop()
if err != nil {
return err
}
if !check.Found {
fmt.Println("No updates found.")
return nil
}
if update.IsLatestVersion(check) {
fmt.Println("Already up-to-date.")
return nil
}
if opts.cfg.Debug {
fmt.Println(update.DebugVersion(check))
}
fmt.Println(update.ReportVersion(check))
if opts.dryRun {
fmt.Println(update.HowToUpdate(check))
return nil
}
spr.Suffix = " Installing update..."
spr.Restart()
message, err := update.InstallLatest(check)
spr.Stop()
if err != nil {
return err
}
fmt.Println(message)
return nil
}