Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(cli): add coder exp tasks list#19496

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 4 commits intomainfrommafredri/feat-cli-add-exp-tasks-list
Aug 25, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
1 change: 1 addition & 0 deletionscli/exp.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,7 @@ func (r *RootCmd) expCmd() *serpent.Command {
r.mcpCommand(),
r.promptExample(),
r.rptyCommand(),
r.tasksCommand(),
},
}
return cmd
Expand Down
20 changes: 20 additions & 0 deletionscli/exp_task.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
package cli

import (
"github.com/coder/serpent"
)

func (r *RootCmd) tasksCommand() *serpent.Command {
cmd := &serpent.Command{
Use: "task",
Aliases: []string{"tasks"},
Short: "Experimental task commands.",
Handler: func(i *serpent.Invocation) error {
return i.Command.HelpHandler(i)
},
Children: []*serpent.Command{
r.taskList(),
},
}
return cmd
}
142 changes: 142 additions & 0 deletionscli/exp_tasklist.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
package cli

import (
"fmt"
"strings"
"time"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

type taskListRow struct {
Task codersdk.Task `table:"t,recursive_inline"`

StateChangedAgo string `table:"state changed"`
}

func taskListRowFromTask(now time.Time, t codersdk.Task) taskListRow {
var stateAgo string
if t.CurrentState != nil {
stateAgo = now.UTC().Sub(t.CurrentState.Timestamp).Truncate(time.Second).String() + " ago"
}

return taskListRow{
Task: t,

StateChangedAgo: stateAgo,
}
}

func (r *RootCmd) taskList() *serpent.Command {
var (
statusFilter string
all bool
user string

client = new(codersdk.Client)
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]taskListRow{},
[]string{
"id",
"name",
"status",
"state",
"state changed",
"message",
},
),
cliui.ChangeFormatterData(
cliui.JSONFormat(),
func(data any) (any, error) {
rows, ok := data.([]taskListRow)
if !ok {
return nil, xerrors.Errorf("expected []taskListRow, got %T", data)
}
out := make([]codersdk.Task, len(rows))
for i := range rows {
out[i] = rows[i].Task
}
return out, nil
},
),
)
)

cmd := &serpent.Command{
Use: "list",
Short: "List experimental tasks",
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Options: serpent.OptionSet{
{
Name: "status",
Description: "Filter by task status (e.g. running, failed, etc).",
Flag: "status",
Default: "",
Value: serpent.StringOf(&statusFilter),
},
{
Name: "all",
Description: "List tasks for all users you can view.",
Flag: "all",
FlagShorthand: "a",
Default: "false",
Value: serpent.BoolOf(&all),
},
{
Name: "user",
Description: "List tasks for the specified user (username, \"me\").",
Flag: "user",
Default: "",
Value: serpent.StringOf(&user),
},
},
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
exp := codersdk.NewExperimentalClient(client)

targetUser := strings.TrimSpace(user)
if targetUser == "" && !all {
targetUser = codersdk.Me
}

tasks, err := exp.Tasks(ctx, &codersdk.TasksFilter{
Owner: targetUser,
Status: statusFilter,
})
if err != nil {
return xerrors.Errorf("list tasks: %w", err)
}

// 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.")
return nil
}

rows := make([]taskListRow, len(tasks))
now := time.Now()
for i := range tasks {
rows[i] = taskListRowFromTask(now, tasks[i])
}

out, err := formatter.Format(ctx, rows)
if err != nil {
return xerrors.Errorf("format tasks: %w", err)
}
_, _ = fmt.Fprintln(inv.Stdout, out)
return nil
},
}

formatter.AttachOptions(&cmd.Options)
return cmd
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp