/
cmd_admin_history.go
98 lines (81 loc) · 1.81 KB
/
cmd_admin_history.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
package main
import (
"fmt"
"os"
"sort"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"github.com/nsec/askgod/api"
"github.com/nsec/askgod/internal/utils"
)
// Sorting
type byFlagID []api.AdminScore
func (a byFlagID) Len() int {
return len(a)
}
func (a byFlagID) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byFlagID) Less(i, j int) bool {
return a[i].FlagID < a[j].FlagID
}
func (c *client) cmdAdminHistory(ctx *cli.Context) error {
// Get the scores
scores := []api.AdminScore{}
err := c.queryStruct("GET", "/scores", nil, &scores)
if err != nil {
return err
}
sort.Sort(byFlagID(scores))
// Get the teams
teams := []api.AdminTeam{}
err = c.queryStruct("GET", "/teams", nil, &teams)
if err != nil {
return err
}
// Get the flags
flags := []api.AdminFlag{}
err = c.queryStruct("GET", "/flags", nil, &flags)
if err != nil {
return err
}
const layout = "2006/01/02 15:04"
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Flag ID", "Flag Description", "Flag Tags", "Team ID", "Team Name", "Team Tags", "Value", "Submit time"})
table.SetBorder(false)
table.SetAutoWrapText(false)
for _, entry := range scores {
// Get the team
team := api.AdminTeam{}
for _, t := range teams {
if t.ID == entry.TeamID {
team = t
break
}
}
// Get the team
flag := api.AdminFlag{}
for _, t := range flags {
if t.ID == entry.FlagID {
flag = t
break
}
}
teamid := fmt.Sprintf("%d", team.ID)
if team.Tags["infra"] != "" {
teamid = team.Tags["infra"]
}
table.Append([]string{
fmt.Sprintf("%d", flag.ID),
flag.Description,
utils.PackTags(flag.Tags),
teamid,
team.Name,
utils.PackTags(team.Tags),
fmt.Sprintf("%d", entry.Value),
entry.SubmitTime.Local().Format(layout),
})
}
table.Render()
return nil
}