/
show-strobes.go
87 lines (75 loc) · 2.03 KB
/
show-strobes.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
package commands
import (
"encoding/csv"
"os"
"github.com/activecm/rita/datatypes/strobe"
"github.com/activecm/rita/resources"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func init() {
command := cli.Command{
Name: "show-strobes",
Usage: "Print strobe information",
ArgsUsage: "<database>",
Flags: []cli.Flag{
humanFlag,
cli.BoolFlag{
Name: "connection-count, l",
Usage: "Sort the strobes by largest connection count.",
},
configFlag,
},
Action: func(c *cli.Context) error {
db := c.Args().Get(0)
if db == "" {
return cli.NewExitError("Specify a database", -1)
}
res := resources.InitResources(c.String("config"))
var strobes []strobe.Strobe
coll := res.DB.Session.DB(db).C(res.Config.T.Strobe.StrobeTable)
var sortStr string
if c.Bool("connection-count") {
sortStr = "connection_count"
} else {
sortStr = "-connection_count"
}
coll.Find(nil).Sort(sortStr).All(&strobes)
if len(strobes) == 0 {
return cli.NewExitError("No results were found for "+db, -1)
}
if c.Bool("human-readable") {
err := showStrobesHuman(strobes)
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
return nil
}
err := showStrobes(strobes)
if err != nil {
return cli.NewExitError(err.Error(), -1)
}
return nil
},
}
bootstrapCommands(command)
}
func showStrobes(strobes []strobe.Strobe) error {
csvWriter := csv.NewWriter(os.Stdout)
csvWriter.Write([]string{"Source", "Destination", "Connection Count"})
for _, strobe := range strobes {
csvWriter.Write([]string{strobe.Source, strobe.Destination, i(strobe.ConnectionCount)})
}
csvWriter.Flush()
return nil
}
func showStrobesHuman(strobes []strobe.Strobe) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(100)
table.SetHeader([]string{"Source", "Destination", "Connection Count"})
for _, strobe := range strobes {
table.Append([]string{strobe.Source, strobe.Destination, i(strobe.ConnectionCount)})
}
table.Render()
return nil
}