Repository navigation
Expand file tree
/
Copy pathupdate.go
More file actions
370 lines (322 loc) · 12.4 KB
/
Copy pathupdate.go
File metadata and controls
370 lines (322 loc) · 12.4 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"github.com/azure/azure-dev/cli/azd/cmd/actions"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
"github.com/azure/azure-dev/cli/azd/internal/tracing/resource"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/installer"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/update"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type updateFlags struct {
channel string
checkIntervalHours int
global *internal.GlobalCommandOptions
}
func newUpdateFlags(cmd *cobra.Command, global *internal.GlobalCommandOptions) *updateFlags {
flags := &updateFlags{}
flags.Bind(cmd.Flags(), global)
return flags
}
func (f *updateFlags) Bind(local *pflag.FlagSet, global *internal.GlobalCommandOptions) {
f.global = global
local.StringVar(
&f.channel,
"channel",
"",
"Update channel: stable or daily.",
)
local.IntVar(
&f.checkIntervalHours,
"check-interval-hours",
0,
"Override the update check interval in hours.",
)
}
func newUpdateCmd() *cobra.Command {
return &cobra.Command{
Use: "update",
Short: "Updates azd to the latest version.",
}
}
type updateAction struct {
flags *updateFlags
console input.Console
formatter output.Formatter
writer io.Writer
configManager config.UserConfigManager
commandRunner exec.CommandRunner
httpClient *http.Client // nil uses default; tests inject a failing client
}
func newUpdateAction(
flags *updateFlags,
console input.Console,
formatter output.Formatter,
writer io.Writer,
configManager config.UserConfigManager,
commandRunner exec.CommandRunner,
) actions.Action {
return &updateAction{
flags: flags,
console: console,
formatter: formatter,
writer: writer,
configManager: configManager,
commandRunner: commandRunner,
}
}
func (a *updateAction) Run(ctx context.Context) (*actions.ActionResult, error) {
// Non-production builds (dev and PR) should not self-update.
if internal.IsNonProdVersion() {
return nil, &internal.ErrorWithSuggestion{
Err: fmt.Errorf("not supported for dev or PR builds: %w", internal.ErrUnsupportedOperation),
Suggestion: "Build from source or install a release build to use 'azd update'.",
}
}
// Track install method for telemetry
installedBy := installer.InstalledBy()
tracing.SetUsageAttributes(
fields.UpdateInstallMethod.String(string(installedBy)),
)
userConfig, err := a.configManager.Load()
if err != nil {
userConfig = config.NewEmptyConfig()
}
// Show notice on first use
if !update.HasUpdateConfig(userConfig) {
a.console.MessageUxItem(ctx, &ux.MessageTitle{
Title: fmt.Sprintf(
"azd update is currently in Beta. "+
"To learn more about feature stages, visit %s.",
output.WithLinkFormat("https://aka.ms/azd-feature-stages")),
})
// Write a default channel so HasUpdateConfig returns true next time.
if err := update.SetChannel(userConfig, update.LoadUpdateConfig(userConfig).Channel); err != nil {
log.Printf("warning: failed to persist default update channel: %v", err)
} else if err := a.configManager.Save(userConfig); err != nil {
log.Printf("warning: failed to save config after setting default channel: %v", err)
}
}
// Determine current channel BEFORE persisting any flags
currentCfg := update.LoadUpdateConfig(userConfig)
switchingChannels := a.flags.channel != "" && update.Channel(a.flags.channel) != currentCfg.Channel
// Persist non-channel config flags immediately (check-interval)
err = a.persistNonChannelFlags(userConfig)
if err != nil {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeConfigFailed))
return nil, err
}
// If switching channels, persist channel to a temporary config for the version check
// but don't save to disk until after confirmation
channelChanged := false
if switchingChannels {
newChannel, err := update.ParseChannel(a.flags.channel)
if err != nil {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeInvalidInput))
return nil, err
}
_ = update.SetChannel(userConfig, newChannel)
channelChanged = true
} else if a.flags.channel != "" {
// Same channel explicitly set — just persist it
if err := update.SetChannel(userConfig, update.Channel(a.flags.channel)); err != nil {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeConfigFailed))
return nil, err
}
channelChanged = true
}
cfg := update.LoadUpdateConfig(userConfig)
// Track channel for telemetry
tracing.SetUsageAttributes(
fields.UpdateChannel.String(string(cfg.Channel)),
fields.UpdateFromVersion.String(internal.VersionInfo().Version.String()),
)
// If only config flags were set (no channel change, no update needed), just confirm.
// Non-channel config was already saved to disk inside persistNonChannelFlags.
if a.onlyConfigFlagsSet() {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeSuccess))
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: "Update preferences saved.",
},
}, nil
}
mgr := update.NewManager(a.commandRunner, a.httpClient)
// Block update in CI/CD environments
if resource.IsRunningOnCI() {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeSkippedCI))
return nil, &update.UpdateError{
Code: update.CodeSkippedCI,
Err: &internal.ErrorWithSuggestion{
Err: fmt.Errorf("azd update is not supported in CI/CD environments"),
Suggestion: "Use your pipeline to install the desired version directly.",
},
}
}
// Check if the user is trying to switch to daily via a package manager
if a.flags.channel == string(update.ChannelDaily) && update.IsPackageManagerInstall() {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodePackageManagerFailed))
uninstallCmd := update.PackageManagerUninstallCmd(installedBy)
return nil, &update.UpdateError{
Code: update.CodePackageManagerFailed,
Err: &internal.ErrorWithSuggestion{
Err: fmt.Errorf("daily builds aren't available via %s", installedBy),
Suggestion: fmt.Sprintf(
"Uninstall first with: %s\nThen install daily with: "+
"powershell -ex AllSigned -c \"& ([scriptblock]::Create("+
"(Invoke-RestMethod 'https://aka.ms/install-azd.ps1'))) -Version 'daily'\"",
uninstallCmd),
},
}
}
// Check for updates (always fresh for manual invocation)
a.console.ShowSpinner(ctx, "Checking for updates...", input.Step)
versionInfo, err := mgr.CheckForUpdate(ctx, cfg, true)
a.console.StopSpinner(ctx, "", input.StepDone)
if err != nil {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeVersionCheckFailed))
return nil, &update.UpdateError{
Code: update.CodeVersionCheckFailed, Err: err,
}
}
// Track target version
tracing.SetUsageAttributes(
fields.UpdateToVersion.String(versionInfo.Version),
)
if !versionInfo.HasUpdate && !switchingChannels {
currentVersion := internal.VersionInfo().Version.String()
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeAlreadyUpToDate))
header := fmt.Sprintf("azd is up to date (version %s) on the %s channel.", currentVersion, cfg.Channel)
if cfg.Channel == update.ChannelDaily {
header += " To check for stable updates, run: azd update --channel stable"
}
return &actions.ActionResult{
Message: &actions.ResultMessage{
Header: header,
},
}, nil
}
// Confirm channel switch with version details
if switchingChannels {
currentVersion := internal.VersionInfo().Version.String()
confirmMsg := fmt.Sprintf(
"Switch from %s channel (%s) to %s channel (%s)?",
currentCfg.Channel, currentVersion,
cfg.Channel, versionInfo.Version,
)
confirm, err := a.console.Confirm(ctx, input.ConsoleOptions{
Message: confirmMsg,
DefaultValue: true,
})
if err != nil || !confirm {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeChannelSwitchDecline))
a.console.Message(ctx, "Channel switch cancelled.")
return nil, nil
}
}
// Perform the update.
a.console.MessageUxItem(ctx, &ux.MessageTitle{Title: updateTitle(installedBy, versionInfo)})
stdout := a.console.Handles().Stdout
err = mgr.Update(ctx, versionInfo, stdout)
restartRequired := errors.Is(err, update.ErrRestartRequired)
if err != nil && !restartRequired {
// UpdateError already has the right code, just track it
if updateErr, ok := errors.AsType[*update.UpdateError](err); ok {
tracing.SetUsageAttributes(fields.UpdateResult.String(updateErr.Code))
} else {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeReplaceFailed))
}
return nil, err
}
// Persist channel config changes only after a successful update.
// Non-channel preferences were already saved before the update attempt.
// Treat save failures as non-fatal since the binary was already updated successfully.
// Guide the user to manually persist the channel if saving fails.
if channelChanged {
if err := a.configManager.Save(userConfig); err != nil {
log.Printf("warning: update succeeded but failed to save channel config: %v", err)
a.console.Message(ctx, output.WithWarningFormat(
"WARNING: failed to save channel preference. "+
"Run 'azd config set updates.channel %s' to persist it.",
cfg.Channel,
))
}
}
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeSuccess))
// Clean up any staged binary now that a manual update succeeded
update.CleanStagedUpdate()
return &actions.ActionResult{
Message: updatedMessage(installedBy, versionInfo, restartRequired),
}, nil
}
func updatedMessage(
installedBy installer.InstallType, target *update.VersionInfo, restartRequired bool,
) *actions.ResultMessage {
if restartRequired {
return &actions.ResultMessage{
Header: "Update installed. Restart Windows to complete the update.",
FollowUp: "After restarting, run `azd version` to confirm.",
}
}
return &actions.ResultMessage{
Header: updatedHeader(installedBy, target),
FollowUp: "Run `azd version` to confirm.",
}
}
// updateTitle returns the progress title shown before an update runs.
//
// A version is named only when azd controls exactly which build gets installed. Package
// managers resolve the version themselves, and daily installs come from a rolling folder
// that can advance between the check and the download, so neither can be promised a
// specific version — that mismatch is what this fix removes (Azure/azure-dev#9145).
func updateTitle(installedBy installer.InstallType, target *update.VersionInfo) string {
switch {
case update.CanPinVersion(installedBy, target.Channel):
return fmt.Sprintf("Updating azd to %s (%s)", target.Version, target.Channel)
case update.UsesPackageManager(installedBy):
return fmt.Sprintf("Updating azd via %s (%s)", installedBy, target.Channel)
default:
return fmt.Sprintf("Updating azd (%s)", target.Channel)
}
}
// updatedHeader returns the success message shown after an update completes.
// It reports the resolved version only when the install was pinned to it; see [updateTitle].
func updatedHeader(installedBy installer.InstallType, target *update.VersionInfo) string {
if !update.CanPinVersion(installedBy, target.Channel) {
return "Updated azd! Changes take effect on next invocation."
}
return fmt.Sprintf("Updated azd to version %s! Changes take effect on next invocation.", target.Version)
}
// persistNonChannelFlags saves check-interval flags to config.
// Channel is handled separately to allow confirmation before persisting.
func (a *updateAction) persistNonChannelFlags(cfg config.Config) error {
if a.flags.checkIntervalHours > 0 {
if err := update.SetCheckIntervalHours(cfg, a.flags.checkIntervalHours); err != nil {
return err
}
if err := a.configManager.Save(cfg); err != nil {
tracing.SetUsageAttributes(fields.UpdateResult.String(update.CodeConfigFailed))
return fmt.Errorf("failed to save config: %w", err)
}
}
return nil
}
// onlyConfigFlagsSet returns true if only config flags were provided (no channel that requires an update).
func (a *updateAction) onlyConfigFlagsSet() bool {
return a.flags.channel == "" && a.flags.checkIntervalHours > 0
}