-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot.go
More file actions
47 lines (38 loc) · 880 Bytes
/
Copy pathdot.go
File metadata and controls
47 lines (38 loc) · 880 Bytes
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
package godot
import (
"bytes"
"io/ioutil"
"text/template"
)
// dotTemplate dotTemplate
const dotTemplate = `
digraph g {
{{ .Attr.String }}
{{ range .NodeGlobalAttr }}
{{ if .Len }}
{{ printf "node [ %s ];" .String }}
{{ end }}
{{ end }}
{{ if .EdgeGlobalAttr.Len }}
{{ printf "edge [ %s ];" .EdgeGlobalAttr.String }}
{{ end }}
{{ range .Nodes.Sort }}
{{ if .Attr.Len }}
{{ printf "%s [ %s ];" .Name .Attr.String }}
{{ else }}
{{ printf "%s;" .Name }}
{{ end }}
{{ end }}
{{ range .Edges.Sort }}
{{ printf "%s%s -> %s%s;" .Src .SrcPort .Dst .DstPort }}
{{ end }}
}
`
// GenerateDotFile 生成dot文件
func GenerateDotFile(fileName string, g *Graph) error {
var buf bytes.Buffer
if err := template.Must(template.New("dot").Parse(dotTemplate)).Execute(&buf, g); err != nil {
return err
}
return ioutil.WriteFile(fileName, buf.Bytes(), 0666)
}