-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.go
117 lines (101 loc) · 2.94 KB
/
reports.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
package main
import (
"fmt"
"net/url"
"os"
"strings"
"github.com/fatih/color"
"github.com/jakewarren/trustar-golang"
"github.com/rodaine/table"
"github.com/spf13/cobra"
)
var reportCmd = &cobra.Command{
Use: "reports",
Short: "Interact with reports",
}
// https://docs.trustar.co/api/v13/reports/search_reports.html
var reportSearchCmd = &cobra.Command{
Use: "search <search term>",
Short: "Search reports",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var (
reports trustar.ReportResponse
err error
numOfReports int
)
headerFmt := color.New(color.FgCyan, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("id", "title", "created", "updated", "enclave")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
formatTime := func(e int64) string {
t, _ := trustar.MsEpochToTime(e)
return t.Format("2006-01-02 15:04:05 MST")
}
// search the reports
for _, a := range args {
s.Suffix = " Searching..."
s.Start()
query := url.Values{}
query.Add("searchTerm", a)
reports, err = c.SearchReports(query)
s.Stop()
if err != nil {
fmt.Println(err)
return
}
if len(reports.Reports) == 0 {
fmt.Println("0 reports found.")
continue
}
dedupedReports := deduplicateReports(reports.Reports)
for _, r := range dedupedReports {
associatedEnclaves := make([]string, 0)
for _, e := range r.EnclaveIds {
associatedEnclaves = append(associatedEnclaves, lookupEnclave(e))
}
tbl.AddRow(r.ID, r.Title, formatTime(r.Created), formatTime(r.Updated), strings.Join(associatedEnclaves, ", "))
numOfReports++
}
}
if numOfReports > 0 {
tbl.Print()
fmt.Printf("\n%d report(s) found.\n", numOfReports)
}
},
}
// the reports we get from the API are not de-duplicated so we have to do it ourselves :(
func deduplicateReports(reports []trustar.ReportDetails) []trustar.ReportDetails {
reportsSeen := make(map[string]struct{})
dedupedReports := make([]trustar.ReportDetails, 0)
for _, r := range reports {
// if the report has already been processed, update the associated enclaves and de-duplicate it
_, exists := reportsSeen[r.ID]
if exists {
// append the enclave
for j := range dedupedReports {
if dedupedReports[j].ID == r.ID {
dedupedReports[j].EnclaveIds = append(dedupedReports[j].EnclaveIds, r.EnclaveIds...)
}
}
} else {
reportsSeen[r.ID] = struct{}{}
dedupedReports = append(dedupedReports, r)
}
}
return dedupedReports
}
var reportOpenCmd = &cobra.Command{
Use: "open <report id>...",
Short: "Open the specified report(s) in your browser",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, a := range args {
err := openBrowser(fmt.Sprintf("https://station.trustar.co/constellation/reports/%s", a))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
},
}