terraform/digraph/graphviz.go

29 lines
568 B
Go
Raw Normal View History

2014-05-24 21:47:04 +02:00
package digraph
import (
"fmt"
"io"
)
2014-07-14 21:10:26 +02:00
// WriteDot is used to emit a GraphViz compatible definition
2014-05-24 21:47:04 +02:00
// for a directed graph. It can be used to dump a .dot file.
2014-07-14 18:00:01 +02:00
func WriteDot(w io.Writer, nodes []Node) error {
2014-05-24 21:47:04 +02:00
w.Write([]byte("digraph {\n"))
defer w.Write([]byte("}\n"))
2014-07-14 18:00:01 +02:00
2014-05-24 21:47:04 +02:00
for _, n := range nodes {
2014-07-14 18:00:01 +02:00
nodeLine := fmt.Sprintf("\t\"%s\";\n", n)
w.Write([]byte(nodeLine))
2014-05-24 21:47:04 +02:00
for _, edge := range n.Edges() {
target := edge.Tail()
line := fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n",
2014-05-24 21:47:04 +02:00
n, target, edge)
w.Write([]byte(line))
}
}
2014-07-14 18:00:01 +02:00
return nil
2014-05-24 21:47:04 +02:00
}