Skip to content

Commit ebab2e6

Browse files
jbarrattmatiaskorhonen
authored andcommitted
Added optional JSON output of results (-o flag) (goadapp#31)
1 parent c514240 commit ebab2e6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

cli/cli.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
7+
"io/ioutil"
68
"os"
79
"os/signal"
810
"sort"
@@ -29,6 +31,7 @@ var (
2931
body string
3032
headers helpers.StringsliceFlag
3133
awsProfile string
34+
outputFile string
3235
)
3336

3437
const coldef = termbox.ColorDefault
@@ -45,6 +48,7 @@ func main() {
4548
flag.UintVar(&timeout, "t", 15, "request timeout in seconds")
4649
flag.StringVar(&regions, "r", "us-east-1,eu-west-1,ap-northeast-1", "AWS regions to run in (comma separated, no spaces)")
4750
flag.StringVar(&awsProfile, "p", "", "AWS named profile to use")
51+
flag.StringVar(&outputFile, "o", "", "Optional path to JSON file for result storage")
4852
flag.Var(&headers, "H", "List of headers")
4953
flag.BoolVar(&printVersion, "version", false, "print the current Goad version")
5054
flag.Parse()
@@ -78,6 +82,10 @@ func main() {
7882
var finalResult queue.RegionsAggData
7983
defer printSummary(&finalResult)
8084

85+
if outputFile != "" {
86+
defer saveJSONSummary(outputFile, &finalResult)
87+
}
88+
8189
sigChan := make(chan os.Signal, 1)
8290
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) // but interrupts from kbd are blocked by termbox
8391

@@ -272,3 +280,28 @@ func printSummary(result *queue.RegionsAggData) {
272280
}
273281
fmt.Println("")
274282
}
283+
284+
func saveJSONSummary(path string, result *queue.RegionsAggData) {
285+
if len(result.Regions) == 0 {
286+
return
287+
}
288+
results := make(map[string]queue.AggData)
289+
290+
for region, data := range result.Regions {
291+
results[region] = data
292+
}
293+
294+
overall := queue.SumRegionResults(result)
295+
296+
results["overall"] = *overall
297+
b, err := json.MarshalIndent(results, "", " ")
298+
if err != nil {
299+
fmt.Println(err)
300+
return
301+
}
302+
err = ioutil.WriteFile(path, b, 0644)
303+
if err != nil {
304+
fmt.Println(err)
305+
return
306+
}
307+
}

0 commit comments

Comments
 (0)