opentofu/digraph/graphviz.go

29 lines
568 B
Go
Raw Normal View History

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