-
Notifications
You must be signed in to change notification settings - Fork 3
/
format.go
76 lines (62 loc) · 1.37 KB
/
format.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
package dash
import (
"encoding/json"
"fmt"
"strings"
"github.com/itchio/headway/united"
)
func (v *Verdict) String() string {
var lines []string
lines = append(lines, fmt.Sprintf(`| %s %s`,
united.FormatBytes(v.TotalSize),
v.BasePath))
for _, candidate := range v.Candidates {
lines = append(lines, candidate.String())
}
return strings.Join(lines, "\n")
}
func (c *Candidate) String() string {
line := fmt.Sprintf(`|-- %s %s %s-%s`,
united.FormatBytes(c.Size),
c.Path,
c.Flavor,
c.Arch,
)
append := func(label string, input interface{}) {
var values []string
if input == nil {
return
}
switch node := input.(type) {
case map[string]interface{}:
for k, v := range node {
if b, ok := v.(bool); ok {
if b {
values = append(values, k)
}
} else {
values = append(values, fmt.Sprintf("%s=%s", k, v))
}
}
default:
return
}
var valueString = ""
if len(values) > 0 {
valueString = fmt.Sprintf("(%s)", strings.Join(values, " "))
}
line += fmt.Sprintf(" %s%s", label, valueString)
}
marshalled, err := json.Marshal(c)
if err != nil {
return err.Error()
}
intermediate := make(map[string]interface{})
err = json.Unmarshal(marshalled, &intermediate)
if err != nil {
return err.Error()
}
append("win", intermediate["windowsInfo"])
append("sh", intermediate["scriptInfo"])
return line
}