MCPcopy Index your code
hub / github.com/coder/coder / update

Method update

cli/update.go:13–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11)
12
13func (r *RootCmd) update() *serpent.Command {
14 var (
15 parameterFlags workspaceParameterFlags
16 bflags buildFlags
17 )
18 cmd := &serpent.Command{
19 Annotations: workspaceCommand,
20 Use: "update <workspace>",
21 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.",
22 Long: "Use --always-prompt to change the parameter values of the workspace.",
23 Middleware: serpent.Chain(
24 serpent.RequireNArgs(1),
25 ),
26 Handler: func(inv *serpent.Invocation) error {
27 client, err := r.InitClient(inv)
28 if err != nil {
29 return err
30 }
31
32 workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
33 if err != nil {
34 return err
35 }
36 if !workspace.Outdated && !parameterFlags.promptRichParameters && !parameterFlags.promptEphemeralParameters && len(parameterFlags.ephemeralParameters) == 0 {
37 _, _ = fmt.Fprintf(inv.Stdout, "Workspace is up-to-date.\n")
38 return nil
39 }
40
41 // #17840: If the workspace is already running, we will stop it before
42 // updating. Simply performing a new start transition may not work if the
43 // template specifies ignore_changes.
44 if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionStart {
45 build, err := stopWorkspace(inv, client, workspace, bflags)
46 if err != nil {
47 return xerrors.Errorf("stop workspace: %w", err)
48 }
49 // Wait for the stop to complete.
50 if err := cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID); err != nil {
51 return xerrors.Errorf("wait for stop: %w", err)
52 }
53 }
54
55 build, err := startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
56 if err != nil {
57 return xerrors.Errorf("start workspace: %w", err)
58 }
59
60 logs, closer, err := client.WorkspaceBuildLogsAfter(inv.Context(), build.ID, 0)
61 if err != nil {
62 return err
63 }
64 defer closer.Close()
65 for {
66 log, ok := <-logs
67 if !ok {
68 break
69 }
70 _, _ = fmt.Fprintf(inv.Stdout, "Output: %s\n", log.Output)

Callers 1

CoreSubcommandsMethod · 0.95

Calls 11

InitClientMethod · 0.95
WorkspaceBuildFunction · 0.92
ResolveWorkspaceMethod · 0.80
allOptionsMethod · 0.80
cliOptionsMethod · 0.80
stopWorkspaceFunction · 0.70
startWorkspaceFunction · 0.70
ContextMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected