forked from itchyny/rexdep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
75 lines (68 loc) · 1.83 KB
/
output.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 main
import (
"fmt"
"io"
"regexp"
"sort"
"strconv"
)
func outputDefault(writer io.Writer, dependency *Dependency) {
for _, module := range dependency.modules {
for _, to := range keys(dependency.relation[module]) {
fmt.Fprintf(writer, "%s %s\n", module, to)
}
}
}
func outputDot(writer io.Writer, dependency *Dependency) {
fmt.Fprintf(writer, "digraph \"graph\" {\n")
for _, module := range dependency.modules {
for _, to := range keys(dependency.relation[module]) {
fmt.Fprintf(writer, " %s -> %s;\n", strconv.Quote(module), strconv.Quote(to))
}
}
fmt.Fprintf(writer, "}\n")
}
func outputCsv(writer io.Writer, dependency *Dependency) {
for _, module := range dependency.modules {
for _, to := range keys(dependency.relation[module]) {
fmt.Fprintf(writer, "%s,%s\n", strconv.Quote(module), strconv.Quote(to))
}
}
}
func outputTsv(writer io.Writer, dependency *Dependency) {
escape := func(str string) string {
unescape := regexp.MustCompile(`\\([\"'\\])`)
ret := unescape.ReplaceAllString(strconv.Quote(str), "$1")
return ret[1 : len(ret)-1]
}
for _, module := range dependency.modules {
for _, to := range keys(dependency.relation[module]) {
fmt.Fprintf(writer, "%s\t%s\n", escape(module), escape(to))
}
}
}
func outputJSON(writer io.Writer, dependency *Dependency) {
fmt.Fprintf(writer, "{")
for i, module := range dependency.modules {
if i > 0 {
fmt.Fprintf(writer, ",")
}
fmt.Fprintf(writer, "\n %s: [", strconv.Quote(module))
for j, to := range keys(dependency.relation[module]) {
if j > 0 {
fmt.Fprintf(writer, ",")
}
fmt.Fprintf(writer, "\n %s", strconv.Quote(to))
}
fmt.Fprintf(writer, "\n ]")
}
fmt.Fprintf(writer, "\n}\n")
}
func keys(m map[string]bool) []string {
var xs []string
for x := range m {
xs = append(xs, x)
}
sort.Strings(xs)
return xs
}