From 6e4f330b0fd260aab28a743d04169ccacaf80d0a Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Thu, 4 Sep 2025 08:32:01 +0000 Subject: [PATCH] feat(cli): add quiet flag to task create Allows passing `--quiet` (or `-q`) to the task create command so that it only prints the ID of the task. --- cli/exp_task_create.go | 24 ++++++++++++++++++------ cli/exp_task_create_test.go | 15 ++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cli/exp_task_create.go b/cli/exp_task_create.go index e2c4f35a5d308..ad5d51a3a42c3 100644 --- a/cli/exp_task_create.go +++ b/cli/exp_task_create.go @@ -22,6 +22,7 @@ func (r *RootCmd) taskCreate() *serpent.Command { templateVersionName string presetName string stdin bool + quiet bool ) cmd := &serpent.Command{ @@ -57,6 +58,13 @@ func (r *RootCmd) taskCreate() *serpent.Command { Description: "Reads from stdin for the task input.", Value: serpent.BoolOf(&stdin), }, + { + Name: "quiet", + Flag: "quiet", + FlagShorthand: "q", + Description: "Only display the created task's ID.", + Value: serpent.BoolOf(&quiet), + }, }, Handler: func(inv *serpent.Invocation) error { var ( @@ -166,12 +174,16 @@ func (r *RootCmd) taskCreate() *serpent.Command { return xerrors.Errorf("create task: %w", err) } - _, _ = fmt.Fprintf( - inv.Stdout, - "The task %s has been created at %s!\n", - cliui.Keyword(task.Name), - cliui.Timestamp(task.CreatedAt), - ) + if quiet { + _, _ = fmt.Fprintln(inv.Stdout, task.ID) + } else { + _, _ = fmt.Fprintf( + inv.Stdout, + "The task %s has been created at %s!\n", + cliui.Keyword(task.Name), + cliui.Timestamp(task.CreatedAt), + ) + } return nil }, diff --git a/cli/exp_task_create_test.go b/cli/exp_task_create_test.go index 26f22c254dcc1..655dfad29344d 100644 --- a/cli/exp_task_create_test.go +++ b/cli/exp_task_create_test.go @@ -31,6 +31,7 @@ func TestTaskCreate(t *testing.T) { templateID = uuid.New() templateVersionID = uuid.New() templateVersionPresetID = uuid.New() + taskID = uuid.New() ) templateAndVersionFoundHandler := func(t *testing.T, ctx context.Context, orgID uuid.UUID, templateName, templateVersionName, presetName, prompt string) http.HandlerFunc { @@ -44,11 +45,11 @@ func TestTaskCreate(t *testing.T) { ID: orgID, }}, }) - case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template/versions/my-template-version", orgID): + case fmt.Sprintf("/api/v2/organizations/%s/templates/%s/versions/%s", orgID, templateName, templateVersionName): httpapi.Write(ctx, w, http.StatusOK, codersdk.TemplateVersion{ ID: templateVersionID, }) - case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template", orgID): + case fmt.Sprintf("/api/v2/organizations/%s/templates/%s", orgID, templateName): httpapi.Write(ctx, w, http.StatusOK, codersdk.Template{ ID: templateID, ActiveVersionID: templateVersionID, @@ -83,7 +84,8 @@ func TestTaskCreate(t *testing.T) { assert.Equal(t, templateVersionPresetID, req.TemplateVersionPresetID, "template version preset id mismatch") } - httpapi.Write(ctx, w, http.StatusCreated, codersdk.Workspace{ + httpapi.Write(ctx, w, http.StatusCreated, codersdk.Task{ + ID: taskID, Name: "task-wild-goldfish-27", CreatedAt: taskCreatedAt, }) @@ -161,6 +163,13 @@ func TestTaskCreate(t *testing.T) { return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "", "my-preset", "my custom prompt") }, }, + { + args: []string{"my custom prompt", "-q"}, + expectOutput: taskID.String(), + handler: func(t *testing.T, ctx context.Context) http.HandlerFunc { + return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "my-template-version", "", "my custom prompt") + }, + }, { args: []string{"my custom prompt", "--template", "my-template", "--preset", "not-real-preset"}, expectError: `preset "not-real-preset" not found`,