-
Notifications
You must be signed in to change notification settings - Fork 1
/
runGet.go
174 lines (143 loc) · 4.61 KB
/
runGet.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
package run
import (
"bytes"
"context"
"fmt"
"strconv"
"text/template"
"github.com/clintjedwards/gofer/internal/cli/cl"
"github.com/clintjedwards/gofer/internal/cli/format"
proto "github.com/clintjedwards/gofer/proto/go"
"github.com/fatih/color"
"github.com/spf13/cobra"
"google.golang.org/grpc/metadata"
)
var cmdRunGet = &cobra.Command{
Use: "get <pipeline> <id>",
Short: "Get details on a specific run",
Example: `$ gofer run get simple_test_pipeline 23`,
RunE: runGet,
Args: cobra.ExactArgs(2),
}
func init() {
CmdRun.AddCommand(cmdRunGet)
}
func runGet(_ *cobra.Command, args []string) error {
pipelineID := args[0]
idRaw := args[1]
id, err := strconv.Atoi(idRaw)
if err != nil {
return err
}
cl.State.Fmt.Print("Retrieving run")
conn, err := cl.State.Connect()
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
return err
}
client := proto.NewGoferClient(conn)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
md := metadata.Pairs("Authorization", "Bearer "+cl.State.Config.Token)
ctx = metadata.NewOutgoingContext(ctx, md)
run, err := client.GetRun(ctx, &proto.GetRunRequest{
NamespaceId: cl.State.Config.Namespace,
PipelineId: pipelineID,
Id: int64(id),
})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not get run: %v", err))
cl.State.Fmt.Finish()
return err
}
taskRuns, err := client.ListTaskRuns(ctx, &proto.ListTaskRunsRequest{
NamespaceId: cl.State.Config.Namespace,
PipelineId: run.Run.Pipeline,
RunId: run.Run.Id,
})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not get task run data: %v", err))
cl.State.Fmt.Finish()
return err
}
cl.State.Fmt.Println(formatRunInfo(run.Run, taskRuns.TaskRuns, cl.State.Config.Detail))
cl.State.Fmt.Finish()
return nil
}
type data struct {
ID string
State string
Status string
Started string
Duration string
PipelineID string
InitiatorType string
InitiatorName string
ObjectsExpired bool
TaskRuns []taskRunData
}
type taskRunData struct {
Duration string
Started string
ID string
Status string
State string
StatePrefix string
DependsOn []string
}
func formatTaskRunStatePrefix(state proto.TaskRun_TaskRunState) string {
if state == proto.TaskRun_RUNNING {
return "Running for"
}
if state == proto.TaskRun_WAITING || state == proto.TaskRun_PROCESSING {
return "Waiting for"
}
return "Lasted"
}
func formatRunInfo(run *proto.Run, taskRuns []*proto.TaskRun, detail bool) string {
taskRunList := []taskRunData{}
for _, task := range taskRuns {
data := taskRunData{
Duration: format.Duration(task.Started, task.Ended),
Started: format.UnixMilli(task.Started, "Not yet", detail),
ID: color.BlueString(task.Id),
Status: format.ColorizeTaskRunStatus(format.NormalizeEnumValue(task.Status.String(), "Unknown")),
State: format.ColorizeTaskRunState(format.NormalizeEnumValue(task.State.String(), "Unknown")),
StatePrefix: formatTaskRunStatePrefix(task.State),
}
data.DependsOn = format.Dependencies(task.Task.DependsOn)
taskRunList = append(taskRunList, data)
}
faint := color.New(color.Faint).SprintfFunc()
data := data{
ID: color.BlueString("#" + strconv.Itoa(int(run.Id))),
Status: format.ColorizeRunStatus(format.NormalizeEnumValue(run.Status.String(), "Unknown")),
State: format.ColorizeRunState(format.NormalizeEnumValue(run.State.String(), "Unknown")),
Started: format.UnixMilli(run.Started, "Not yet", detail),
Duration: format.Duration(run.Started, run.Ended),
PipelineID: color.BlueString(run.Pipeline),
InitiatorType: faint("[" + format.NormalizeEnumValue(run.Initiator.Type.String(), "Unknown") + "]"),
InitiatorName: color.CyanString(run.Initiator.Name),
ObjectsExpired: run.StoreObjectsExpired,
TaskRuns: taskRunList,
}
const formatTmpl = `Run {{.ID}} for Pipeline {{.PipelineID}} :: {{.State}} :: {{.Status}}
Initiated by {{.InitiatorName}} {{.InitiatorType}} {{.Started}} and ran for {{.Duration}}
{{- if .TaskRuns}}
🗒 Task Runs
{{- range $run := .TaskRuns}}
• {{$run.ID}} :: Started {{ $run.Started }} :: {{ $run.StatePrefix }} {{ $run.Duration }} :: {{ $run.State }} :: {{ $run.Status }}
{{- if $run.DependsOn -}}
{{- range $dependant := $run.DependsOn }}
- {{ $dependant }}
{{- end -}}
{{- end -}}
{{- end}}
{{- end}}
Objects Expired: {{.ObjectsExpired}}`
var tpl bytes.Buffer
t := template.Must(template.New("tmp").Parse(formatTmpl))
_ = t.Execute(&tpl, data)
return tpl.String()
}