-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathjson.go
75 lines (61 loc) · 1.6 KB
/
json.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
package outputs
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/util"
"github.com/fatih/color"
)
type Json struct{}
func (r Json) Output(w io.Writer, results <-chan []resource.TestResult,
startTime time.Time, outConfig util.OutputConfig) (exitCode int) {
var pretty bool
pretty = util.IsValueInList("pretty", outConfig.FormatOptions)
color.NoColor = true
testCount := 0
failed := 0
var resultsOut []map[string]interface{}
for resultGroup := range results {
for _, testResult := range resultGroup {
if !testResult.Successful {
failed++
}
m := struct2map(testResult)
m["summary-line"] = humanizeResult(testResult)
m["duration"] = int64(m["duration"].(float64))
resultsOut = append(resultsOut, m)
testCount++
}
}
summary := make(map[string]interface{})
duration := time.Since(startTime)
summary["test-count"] = testCount
summary["failed-count"] = failed
summary["total-duration"] = duration
summary["summary-line"] = fmt.Sprintf("Count: %d, Failed: %d, Duration: %.3fs", testCount, failed, duration.Seconds())
out := make(map[string]interface{})
out["results"] = resultsOut
out["summary"] = summary
var j []byte
if pretty {
j, _ = json.MarshalIndent(out, "", " ")
} else {
j, _ = json.Marshal(out)
}
fmt.Fprintln(w, string(j))
if failed > 0 {
return 1
}
return 0
}
func init() {
RegisterOutputer("json", &Json{}, []string{"pretty"})
}
func struct2map(i interface{}) map[string]interface{} {
out := make(map[string]interface{})
j, _ := json.Marshal(i)
json.Unmarshal(j, &out)
return out
}