-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventGet.go
120 lines (98 loc) · 2.62 KB
/
eventGet.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
package event
import (
"bytes"
"context"
"encoding/json"
"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 cmdEventGet = &cobra.Command{
Use: "get <id>",
Short: "Get details on a specific event",
Example: `$ gofer event get Abdedow8953`,
RunE: eventGet,
Args: cobra.ExactArgs(1),
}
func init() {
CmdEvent.AddCommand(cmdEventGet)
}
func eventGet(_ *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
}
cl.State.Fmt.Print("Retrieving event")
conn, err := cl.State.Connect()
if err != nil {
cl.State.Fmt.PrintErr(err)
cl.State.Fmt.Finish()
return err
}
client := proto.NewGoferClient(conn)
md := metadata.Pairs("Authorization", "Bearer "+cl.State.Config.Token)
ctx := metadata.NewOutgoingContext(context.Background(), md)
resp, err := client.GetEvent(ctx, &proto.GetEventRequest{
Id: int64(id),
})
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not get taskrun: %v", err))
cl.State.Fmt.Finish()
return err
}
output, err := formatEvent(resp, cl.State.Config.Detail)
if err != nil {
cl.State.Fmt.PrintErr(fmt.Sprintf("could not render event: %v", err))
cl.State.Fmt.Finish()
return err
}
cl.State.Fmt.Println(output)
cl.State.Fmt.Finish()
return nil
}
type data struct {
Kind string
Emitted string
ID string
Details map[string]interface{}
}
func formatEvent(event *proto.GetEventResponse, detail bool) (string, error) {
evnt := Event{
Type: event.Event.Type,
ID: event.Event.Id,
Emitted: event.Event.Emitted,
Details: event.Event.Details,
}
details := map[string]interface{}{}
err := json.Unmarshal([]byte(evnt.Details), &details)
if err != nil {
return "", err
}
fmttedDetails := map[string]interface{}{}
for key, value := range details {
fmttedDetails[color.CyanString(formatEventKind(key))] = value
}
data := data{
Kind: color.BlueString(formatEventKind(evnt.Type)),
Emitted: format.UnixMilli(evnt.Emitted, "Unknown", detail),
ID: color.YellowString(fmt.Sprintf("%d", evnt.ID)),
Details: fmttedDetails,
}
const formatTmpl = `[{{.ID}}] {{.Kind}}
🗒 Details:
{{- range $key, $value := .Details }}
• {{ $key }}: {{ $value }}
{{- end }}
Emitted {{.Emitted}}`
var tpl bytes.Buffer
t := template.Must(template.New("tmp").Parse(formatTmpl))
_ = t.Execute(&tpl, data)
return tpl.String(), nil
}