Skip to content

feat(cli): start workspace in no-wait mode #17087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func (r *RootCmd) start() *serpent.Command {
var (
parameterFlags workspaceParameterFlags
bflags buildFlags

noWait bool
)

client := new(codersdk.Client)
Expand All @@ -28,7 +30,15 @@ func (r *RootCmd) start() *serpent.Command {
serpent.RequireNArgs(1),
r.InitClient(client),
),
Options: serpent.OptionSet{cliui.SkipPromptOption()},
Options: serpent.OptionSet{
{
Flag: "no-wait",
Description: "Return immediately after starting the workspace.",
Value: serpent.BoolOf(&noWait),
Hidden: false,
},
cliui.SkipPromptOption(),
},
Handler: func(inv *serpent.Invocation) error {
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
if err != nil {
Expand Down Expand Up @@ -80,6 +90,11 @@ func (r *RootCmd) start() *serpent.Command {
}
}

if noWait {
_, _ = 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))
return nil
}

err = cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID)
if err != nil {
return err
Expand Down
33 changes: 33 additions & 0 deletions cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,36 @@ func TestStart_Starting(t *testing.T) {

_ = testutil.RequireRecvCtx(ctx, t, doneChan)
}

func TestStart_NoWait(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)

// Prepare user, template, workspace
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, client)
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
version1 := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version1.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version1.ID)
workspace := coderdtest.CreateWorkspace(t, member, template.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)

// Stop the workspace
build := coderdtest.CreateWorkspaceBuild(t, member, workspace, database.WorkspaceTransitionStop)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build.ID)

// Start in no-wait mode
inv, root := clitest.New(t, "start", workspace.Name, "--no-wait")
clitest.SetupConfig(t, member, root)
doneChan := make(chan struct{})
pty := ptytest.New(t).Attach(inv)
go func() {
defer close(doneChan)
err := inv.Run()
assert.NoError(t, err)
}()

pty.ExpectMatch("workspace has been started in no-wait mode")
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
}
3 changes: 3 additions & 0 deletions cli/testdata/coder_start_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ OPTIONS:
Set the value of ephemeral parameters defined in the template. The
format is "name=value".

--no-wait bool
Return immediately after starting the workspace.

--parameter string-array, $CODER_RICH_PARAMETER
Rich parameter value in the format "name=value".

Expand Down
8 changes: 8 additions & 0 deletions docs/reference/cli/start.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading