Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56082f3

Browse files
authored
feat(cli): start workspace in no-wait mode (#17087)
Fixes: #16408
1 parent cd19e79 commit 56082f3

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

cli/start.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func (r *RootCmd) start() *serpent.Command {
1717
var (
1818
parameterFlags workspaceParameterFlags
1919
bflags buildFlags
20+
21+
noWait bool
2022
)
2123

2224
client := new(codersdk.Client)
@@ -28,7 +30,15 @@ func (r *RootCmd) start() *serpent.Command {
2830
serpent.RequireNArgs(1),
2931
r.InitClient(client),
3032
),
31-
Options: serpent.OptionSet{cliui.SkipPromptOption()},
33+
Options: serpent.OptionSet{
34+
{
35+
Flag: "no-wait",
36+
Description: "Return immediately after starting the workspace.",
37+
Value: serpent.BoolOf(&noWait),
38+
Hidden: false,
39+
},
40+
cliui.SkipPromptOption(),
41+
},
3242
Handler: func(inv *serpent.Invocation) error {
3343
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
3444
if err != nil {
@@ -80,6 +90,11 @@ func (r *RootCmd) start() *serpent.Command {
8090
}
8191
}
8292

93+
if noWait {
94+
_, _ = fmt.Fprintf(inv.Stdout, "The %s workspace has been started in no-wait mode. Workspace is building in the background.\n", cliui.Keyword(workspace.Name))
95+
return nil
96+
}
97+
8398
err = cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID)
8499
if err != nil {
85100
return err

cli/start_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,36 @@ func TestStart_Starting(t *testing.T) {
441441

442442
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
443443
}
444+
445+
func TestStart_NoWait(t *testing.T) {
446+
t.Parallel()
447+
ctx := testutil.Context(t, testutil.WaitShort)
448+
449+
// Prepare user, template, workspace
450+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
451+
owner := coderdtest.CreateFirstUser(t, client)
452+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
453+
version1 := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
454+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version1.ID)
455+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version1.ID)
456+
workspace := coderdtest.CreateWorkspace(t, member, template.ID)
457+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
458+
459+
// Stop the workspace
460+
build := coderdtest.CreateWorkspaceBuild(t, member, workspace, database.WorkspaceTransitionStop)
461+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID)
462+
463+
// Start in no-wait mode
464+
inv, root := clitest.New(t, "start", workspace.Name, "--no-wait")
465+
clitest.SetupConfig(t, member, root)
466+
doneChan := make(chan struct{})
467+
pty := ptytest.New(t).Attach(inv)
468+
go func() {
469+
defer close(doneChan)
470+
err := inv.Run()
471+
assert.NoError(t, err)
472+
}()
473+
474+
pty.ExpectMatch("workspace has been started in no-wait mode")
475+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
476+
}

cli/testdata/coder_start_--help.golden

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ OPTIONS:
2222
Set the value of ephemeral parameters defined in the template. The
2323
format is "name=value".
2424

25+
--no-wait bool
26+
Return immediately after starting the workspace.
27+
2528
--parameter string-array, $CODER_RICH_PARAMETER
2629
Rich parameter value in the format "name=value".
2730

docs/reference/cli/start.md

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)