`coder update` stopped and restarted a running workspace without asking and had no `-y`, while `coder restart` and the dashboard's update dialog both confirm first. It now asks for confirmation before stopping a running workspace, using the dashboard's wording that updating can delete non-persistent data. `-y` skips it, and also answers the existing "Enqueue another stop?" prompt. **Behavior change:** **New prompt; can break scripts.** `coder update` on a running workspace without `-y` now blocks on stdin, or fails if stdin is closed. Stopped workspaces don't prompt. Parameter prompts such as `--always-prompt` aren't affected by `-y`. The [Update workspaces](https://coder.com/docs/user-guides/workspace-management#update-with-the-cli) guide now notes the prompt and `-y`. <details> <summary>Decisions</summary> - Mirrors `coder restart`'s prompt pattern. - Doc-check suggested a row in the 2.29 to 2.34 ESR upgrade guide. This change ships after 2.34, so it's documented in the user guide instead. </details> Closes [CLI-34](https://linear.app/codercom/issue/CLI-34) _This PR was generated by Coder Agents on behalf of @nickvigilante._
96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) update() *serpent.Command {
|
|
var (
|
|
parameterFlags workspaceParameterFlags
|
|
bflags buildFlags
|
|
)
|
|
cmd := &serpent.Command{
|
|
Annotations: serpent.Annotations(workspaceCommand).Mark(annotationClientSessionID, "").Mark(annotationFlightRecorder, ""),
|
|
Use: "update <workspace>",
|
|
Short: "Will update and start a given workspace if it is out of date. If the workspace is already running, it will be stopped first.",
|
|
Long: "Use --always-prompt to change the parameter values of the workspace.",
|
|
Middleware: serpent.Chain(
|
|
serpent.RequireNArgs(1),
|
|
),
|
|
Options: serpent.OptionSet{
|
|
cliui.SkipPromptOption(),
|
|
},
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The invocation logger records debug detail and emits it to stderr
|
|
// only if the command fails (see flightRecorderMiddleware).
|
|
client.SetLogger(inv.Logger)
|
|
|
|
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !workspace.Outdated && !parameterFlags.promptRichParameters && !parameterFlags.promptEphemeralParameters && len(parameterFlags.ephemeralParameters) == 0 {
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Workspace is up-to-date.\n")
|
|
return nil
|
|
}
|
|
|
|
// #17840: If the workspace is already running, we will stop it before
|
|
// updating. Simply performing a new start transition may not work if the
|
|
// template specifies ignore_changes.
|
|
if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionStart {
|
|
if workspace.LatestBuild.Status == codersdk.WorkspaceStatusRunning {
|
|
_, err = cliui.Prompt(inv, cliui.PromptOptions{
|
|
Text: "Updating your workspace will start the workspace on the latest template version. This can delete non-persistent data. Continue?",
|
|
IsConfirm: true,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
build, err := stopWorkspace(inv, client, workspace, bflags)
|
|
if err != nil {
|
|
return xerrors.Errorf("stop workspace: %w", err)
|
|
}
|
|
// Wait for the stop to complete.
|
|
if err := cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID); err != nil {
|
|
return xerrors.Errorf("wait for stop: %w", err)
|
|
}
|
|
}
|
|
|
|
build, err := startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
|
|
if err != nil {
|
|
return xerrors.Errorf("start workspace: %w", err)
|
|
}
|
|
|
|
logs, closer, err := client.WorkspaceBuildLogsAfter(inv.Context(), build.ID, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer closer.Close()
|
|
for {
|
|
log, ok := <-logs
|
|
if !ok {
|
|
break
|
|
}
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Output: %s\n", log.Output)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
|
|
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
|
|
return cmd
|
|
}
|