-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtask_list.go
More file actions
181 lines (162 loc) · 3.96 KB
/
task_list.go
File metadata and controls
181 lines (162 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cli
import (
"fmt"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/util/slice"
"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
quiet bool
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]taskListRow{},
[]string{
"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 tasks",
Long: FormatExamples(
Example{
Description: "List tasks for the current user.",
Command: "coder task list",
},
Example{
Description: "List tasks for a specific user.",
Command: "coder task list --user someone-else",
},
Example{
Description: "List all tasks you can view.",
Command: "coder task list --all",
},
Example{
Description: "List all your running tasks.",
Command: "coder task list --status running",
},
Example{
Description: "As above, but only show IDs.",
Command: "coder task list --status running --quiet",
},
),
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
),
Options: serpent.OptionSet{
{
Name: "status",
Description: "Filter by task status.",
Flag: "status",
Default: "",
Value: serpent.EnumOf(&statusFilter, slice.ToStrings(codersdk.AllTaskStatuses())...),
},
{
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),
},
{
Name: "quiet",
Description: "Only display task IDs.",
Flag: "quiet",
FlagShorthand: "q",
Default: "false",
Value: serpent.BoolOf(&quiet),
},
},
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
ctx := inv.Context()
targetUser := strings.TrimSpace(user)
if targetUser == "" && !all {
targetUser = codersdk.Me
}
tasks, err := client.Tasks(ctx, &codersdk.TasksFilter{
Owner: targetUser,
Status: codersdk.TaskStatus(statusFilter),
})
if err != nil {
return xerrors.Errorf("list tasks: %w", err)
}
if quiet {
for _, task := range tasks {
_, _ = fmt.Fprintln(inv.Stdout, task.ID.String())
}
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)
}
if out == "" {
cliui.Infof(inv.Stderr, "No tasks found.")
return nil
}
_, _ = fmt.Fprintln(inv.Stdout, out)
return nil
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}