This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
task_list.go
249 lines (214 loc) · 5.64 KB
/
task_list.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cli
import (
"fmt"
"sort"
"strconv"
"github.com/dustin/go-humanize"
"github.com/golang/protobuf/jsonpb"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/hashicorp/waypoint/internal/clierrors"
"github.com/hashicorp/waypoint/internal/pkg/flag"
pb "github.com/hashicorp/waypoint/pkg/server/gen"
)
type TaskListCommand struct {
*baseCommand
flagJson bool
flagLimit int
flagDesc bool
flagTaskStates []string
}
func (c *TaskListCommand) Run(args []string) int {
// Initialize. If we fail, we just exit since Init handles the UI.
if err := c.Init(
WithArgs(args),
WithFlags(c.Flags()),
); err != nil {
return 1
}
ctx := c.Ctx
listTaskReq := &pb.ListTaskRequest{}
if len(c.flagTaskStates) > 0 {
var ts []pb.Task_State
// convert to int32 const from string value
for _, state := range c.flagTaskStates {
s, ok := pb.Task_State_value[state]
if !ok {
// this shouldn't happen given the State flag is an enum var, but protect
// against it anyway
c.ui.Output("Undefined task job state value: "+state, terminal.WithErrorStyle())
return 1
} else {
ts = append(ts, pb.Task_State(s))
}
}
listTaskReq.TaskState = ts
}
resp, err := c.project.Client().ListTask(ctx, listTaskReq)
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return 1
}
if len(resp.Tasks) == 0 {
return 0
}
tasks := resp.Tasks
// Reverse task list if requested
if c.flagDesc {
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].StartJob.QueueTime.AsTime().Before(tasks[j].StartJob.QueueTime.AsTime())
})
} else {
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].StartJob.QueueTime.AsTime().After(tasks[j].StartJob.QueueTime.AsTime())
})
}
// limit to the first n jobs
if c.flagLimit > 0 && c.flagLimit <= len(tasks) {
tasks = tasks[:c.flagLimit]
}
if c.flagJson {
var m jsonpb.Marshaler
m.Indent = "\t"
for _, t := range resp.Tasks {
str, err := m.MarshalToString(t)
if err != nil {
c.ui.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return 1
}
fmt.Println(str)
}
return 0
}
c.ui.Output("Waypoint On-Demand Runner Tasks", terminal.WithHeaderStyle())
tblHeaders := []string{"ID", "Run Job Operation", "Pipeline", "Task State", "Project", "Time Created", "Time Completed"}
tbl := terminal.NewTable(tblHeaders...)
for _, t := range tasks {
var op string
switch t.TaskJob.Operation.(type) {
case *pb.Job_Noop_:
op = "Noop"
case *pb.Job_Build:
op = "Build"
case *pb.Job_Push:
op = "Push"
case *pb.Job_Deploy:
op = "Deploy"
case *pb.Job_Destroy:
op = "Destroy"
case *pb.Job_Release:
op = "Release"
case *pb.Job_Validate:
op = "Validate"
case *pb.Job_Auth:
op = "Auth"
case *pb.Job_Docs:
op = "Docs"
case *pb.Job_ConfigSync:
op = "ConfigSync"
case *pb.Job_Exec:
op = "Exec"
case *pb.Job_Up:
op = "Up"
case *pb.Job_Logs:
op = "Logs"
case *pb.Job_QueueProject:
op = "QueueProject"
case *pb.Job_Poll:
op = "Poll"
case *pb.Job_StatusReport:
op = "StatusReport"
case *pb.Job_StartTask:
op = "StartTask"
case *pb.Job_StopTask:
op = "StopTask"
case *pb.Job_WatchTask:
op = "WatchTask"
case *pb.Job_Init:
op = "Init"
case *pb.Job_PipelineStep:
op = "PipelineStep"
case *pb.Job_DestroyProject:
op = "DestroyProject"
default:
op = "Unknown"
c.Log.Debug("encountered unsupported task operation", "op", t.TaskJob.Operation)
}
var project string
if t.TaskJob.Application != nil {
project = t.TaskJob.Application.Project
}
var queueTime string
if t.StartJob.QueueTime != nil {
queueTime = humanize.Time(t.StartJob.QueueTime.AsTime())
}
var completeTime string
if t.StopJob.CompleteTime != nil {
completeTime = humanize.Time(t.StopJob.CompleteTime.AsTime())
}
pipeline := ""
if t.TaskJob.Pipeline != nil {
pipeline = "name: " + t.TaskJob.Pipeline.PipelineName + ", run: " + strconv.FormatUint(t.TaskJob.Pipeline.RunSequence, 10) + ", step: " + t.TaskJob.Pipeline.Step
}
tblColumn := []string{
t.Task.Id,
op,
pipeline,
pb.Task_State_name[int32(t.Task.JobState)],
project,
queueTime,
completeTime,
}
tbl.Rich(tblColumn, nil)
}
c.ui.Table(tbl)
return 0
}
var taskStateValues = []string{pb.Task_State_name[0],
pb.Task_State_name[1],
pb.Task_State_name[2],
pb.Task_State_name[3],
pb.Task_State_name[4],
pb.Task_State_name[5],
pb.Task_State_name[6],
pb.Task_State_name[7],
pb.Task_State_name[8],
}
func (c *TaskListCommand) Flags() *flag.Sets {
return c.flagSet(flagSetOperation, func(set *flag.Sets) {
f := set.NewSet("Command Options")
f.BoolVar(&flag.BoolVar{
Name: "desc",
Target: &c.flagDesc,
Default: false,
Usage: "Output the list of Tasks from newest to oldest.",
})
f.BoolVar(&flag.BoolVar{
Name: "json",
Target: &c.flagJson,
Default: false,
Usage: "Output the list of Tasks as json.",
})
f.IntVar(&flag.IntVar{
Name: "limit",
Target: &c.flagLimit,
Default: 0,
Usage: "If set, will limit the number of Tasks to list.",
})
f.EnumVar(&flag.EnumVar{
Name: "state",
Target: &c.flagTaskStates,
Values: taskStateValues,
Usage: "List Tasks that only match the requested state. Can be repeated multiple times.",
})
})
}
func (c *TaskListCommand) Synopsis() string {
return "List all On-Demand Runner Tasks in Waypoint"
}
func (c *TaskListCommand) Help() string {
return formatHelp(`
Usage: waypoint task list [options]
List all known On-Demand Runner Tasks from Waypoint server. Each task is a
Waypoint Job tuple made up of a StartTask, RunTask, and StopTask.
` + c.Flags().Help())
}