From 87c95c0898f8b5b944232fbf5ad1aeee77125a23 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 8 Sep 2025 10:48:52 +0000 Subject: [PATCH] chore(cli): reduce clutter from `exp tasks list` command - Stop showing the long ID of each task by default. - Add new `--quiet` flag to _only_ show IDs. --- cli/{exp_tasklist.go => exp_task_list.go} | 18 ++++++++- ...tasklist_test.go => exp_task_list_test.go} | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) rename cli/{exp_tasklist.go => exp_task_list.go} (89%) rename cli/{exp_tasklist_test.go => exp_task_list_test.go} (88%) diff --git a/cli/exp_tasklist.go b/cli/exp_task_list.go similarity index 89% rename from cli/exp_tasklist.go rename to cli/exp_task_list.go index 7f2b44d25aa4c..70d68f8121c9a 100644 --- a/cli/exp_tasklist.go +++ b/cli/exp_task_list.go @@ -36,13 +36,13 @@ func (r *RootCmd) taskList() *serpent.Command { statusFilter string all bool user string + quiet bool client = new(codersdk.Client) formatter = cliui.NewOutputFormatter( cliui.TableFormat( []taskListRow{}, []string{ - "id", "name", "status", "state", @@ -98,6 +98,14 @@ func (r *RootCmd) taskList() *serpent.Command { Default: "", Value: serpent.StringOf(&user), }, + { + Name: "quiet", + Description: "Only display task IDs.", + Flag: "quiet", + FlagShorthand: "q", + Default: "false", + Value: serpent.BoolOf(&quiet), + }, }, Handler: func(inv *serpent.Invocation) error { ctx := inv.Context() @@ -116,6 +124,14 @@ func (r *RootCmd) taskList() *serpent.Command { return xerrors.Errorf("list tasks: %w", err) } + if quiet { + for _, task := range tasks { + _, _ = fmt.Fprintln(inv.Stdout, task.ID.String()) + } + + return nil + } + // If no rows and not JSON, show a friendly message. if len(tasks) == 0 && formatter.FormatID() != cliui.JSONFormat().ID() { _, _ = fmt.Fprintln(inv.Stderr, "No tasks found.") diff --git a/cli/exp_tasklist_test.go b/cli/exp_task_list_test.go similarity index 88% rename from cli/exp_tasklist_test.go rename to cli/exp_task_list_test.go index 1120a11c69e3c..2761588a3859e 100644 --- a/cli/exp_tasklist_test.go +++ b/cli/exp_task_list_test.go @@ -6,6 +6,8 @@ import ( "database/sql" "encoding/json" "io" + "slices" + "strings" "testing" "github.com/google/uuid" @@ -200,6 +202,43 @@ func TestExpTaskList(t *testing.T) { pty.ExpectMatch(ws.Name) }) + + t.Run("Quiet", func(t *testing.T) { + t.Parallel() + + // Quiet logger to reduce noise. + quiet := slog.Make(sloghuman.Sink(io.Discard)) + client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{Logger: &quiet}) + owner := coderdtest.CreateFirstUser(t, client) + memberClient, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) + + // Given: We have two tasks + task1 := makeAITask(t, db, owner.OrganizationID, owner.UserID, memberUser.ID, database.WorkspaceTransitionStart, "keep me running") + task2 := makeAITask(t, db, owner.OrganizationID, owner.UserID, memberUser.ID, database.WorkspaceTransitionStop, "stop me please") + + // Given: We add the `--quiet` flag + inv, root := clitest.New(t, "exp", "task", "list", "--quiet") + clitest.SetupConfig(t, memberClient, root) + + ctx := testutil.Context(t, testutil.WaitShort) + var stdout bytes.Buffer + inv.Stdout = &stdout + inv.Stderr = &stdout + + // When: We run the command + err := inv.WithContext(ctx).Run() + require.NoError(t, err) + + want := []string{task1.ID.String(), task2.ID.String()} + got := slice.Filter(strings.Split(stdout.String(), "\n"), func(s string) bool { + return len(s) != 0 + }) + + slices.Sort(want) + slices.Sort(got) + + require.Equal(t, want, got) + }) } func TestExpTaskList_OwnerCanListOthers(t *testing.T) {