Skip to content

Commit 8365484

Browse files
committed
chore: refactor and extract stopWorkspace function
1 parent 8436f1c commit 8365484

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

cli/stop.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,11 @@ func (r *RootCmd) stop() *serpent.Command {
3737
if err != nil {
3838
return err
3939
}
40-
if workspace.LatestBuild.Job.Status == codersdk.ProvisionerJobPending {
41-
// cliutil.WarnMatchedProvisioners also checks if the job is pending
42-
// but we still want to avoid users spamming multiple builds that will
43-
// not be picked up.
44-
cliui.Warn(inv.Stderr, "The workspace is already stopping!")
45-
cliutil.WarnMatchedProvisioners(inv.Stderr, workspace.LatestBuild.MatchedProvisioners, workspace.LatestBuild.Job)
46-
if _, err := cliui.Prompt(inv, cliui.PromptOptions{
47-
Text: "Enqueue another stop?",
48-
IsConfirm: true,
49-
Default: cliui.ConfirmNo,
50-
}); err != nil {
51-
return err
52-
}
53-
}
5440

55-
wbr := codersdk.CreateWorkspaceBuildRequest{
56-
Transition: codersdk.WorkspaceTransitionStop,
57-
}
58-
if bflags.provisionerLogDebug {
59-
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
60-
}
61-
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
41+
build, err := stopWorkspace(inv, client, workspace, bflags)
6242
if err != nil {
6343
return err
6444
}
65-
cliutil.WarnMatchedProvisioners(inv.Stderr, build.MatchedProvisioners, build.Job)
6645

6746
err = cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID)
6847
if err != nil {
@@ -71,8 +50,8 @@ func (r *RootCmd) stop() *serpent.Command {
7150

7251
_, _ = fmt.Fprintf(
7352
inv.Stdout,
74-
"\nThe %s workspace has been stopped at %s!\n", cliui.Keyword(workspace.Name),
75-
53+
"\nThe %s workspace has been stopped at %s!\n",
54+
cliui.Keyword(workspace.Name),
7655
cliui.Timestamp(time.Now()),
7756
)
7857
return nil
@@ -82,3 +61,32 @@ func (r *RootCmd) stop() *serpent.Command {
8261

8362
return cmd
8463
}
64+
65+
func stopWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, bflags buildFlags) (codersdk.WorkspaceBuild, error) {
66+
if workspace.LatestBuild.Job.Status == codersdk.ProvisionerJobPending {
67+
// cliutil.WarnMatchedProvisioners also checks if the job is pending
68+
// but we still want to avoid users spamming multiple builds that will
69+
// not be picked up.
70+
cliui.Warn(inv.Stderr, "The workspace is already stopping!")
71+
cliutil.WarnMatchedProvisioners(inv.Stderr, workspace.LatestBuild.MatchedProvisioners, workspace.LatestBuild.Job)
72+
if _, err := cliui.Prompt(inv, cliui.PromptOptions{
73+
Text: "Enqueue another stop?",
74+
IsConfirm: true,
75+
Default: cliui.ConfirmNo,
76+
}); err != nil {
77+
return codersdk.WorkspaceBuild{}, err
78+
}
79+
}
80+
wbr := codersdk.CreateWorkspaceBuildRequest{
81+
Transition: codersdk.WorkspaceTransitionStop,
82+
}
83+
if bflags.provisionerLogDebug {
84+
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
85+
}
86+
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
87+
if err != nil {
88+
return codersdk.WorkspaceBuild{}, err
89+
}
90+
91+
return build, nil
92+
}

cli/update.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"golang.org/x/xerrors"
77

88
"github.com/coder/coder/v2/cli/cliui"
9-
"github.com/coder/coder/v2/cli/cliutil"
109
"github.com/coder/coder/v2/codersdk"
1110
"github.com/coder/serpent"
1211
)
@@ -40,19 +39,10 @@ func (r *RootCmd) update() *serpent.Command {
4039
// updating. Simply performing a new start transition may not work if the
4140
// template specifies ignore_changes.
4241
if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionStart {
43-
_, _ = fmt.Fprintf(inv.Stdout, "Stopping workspace %s before updating.\n", workspace.Name)
44-
wbr := codersdk.CreateWorkspaceBuildRequest{
45-
Transition: codersdk.WorkspaceTransitionStop,
46-
TemplateVersionID: workspace.LatestBuild.TemplateVersionID,
47-
}
48-
if bflags.provisionerLogDebug {
49-
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
50-
}
51-
build, err := client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
42+
build, err := stopWorkspace(inv, client, workspace, bflags)
5243
if err != nil {
5344
return xerrors.Errorf("stop workspace: %w", err)
5445
}
55-
cliutil.WarnMatchedProvisioners(inv.Stderr, build.MatchedProvisioners, build.Job)
5646
// Wait for the stop to complete.
5747
if err := cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID); err != nil {
5848
return xerrors.Errorf("wait for stop: %w", err)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy