-
Notifications
You must be signed in to change notification settings - Fork 12
/
writer.go
125 lines (112 loc) · 3.48 KB
/
writer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cytoscape
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/pcasteran/terraform-graph-beautifier/tfgraph"
"github.com/rs/zerolog/log"
"io"
"io/fs"
"text/template"
)
// RenderingOptions contains all the options used during graph rendering.
type RenderingOptions struct {
GraphName string
EmbedModules bool
HTMLTemplate fs.File
}
// WriteGraphJSON writes the specified Terraform graph in Cytoscape.js JSON format.
func WriteGraphJSON(writer io.Writer, graph *tfgraph.Graph, options *RenderingOptions) {
// Get the graph elements.
graphElements := getGraphElements(graph, options)
// Encode the result to JSON.
enc := json.NewEncoder(writer)
if err := enc.Encode(&graphElements); err != nil {
log.Fatal().Err(err).Msg("Cannot encode Cytoscape.js graph to JSON")
}
}
// WriteGraphHTML writes the specified Terraform graph to an HTML file using the given template.
func WriteGraphHTML(writer io.Writer, graph *tfgraph.Graph, options *RenderingOptions) {
// Get the graph elements JSON.
var buf bytes.Buffer
graphWriter := bufio.NewWriter(&buf)
WriteGraphJSON(graphWriter, graph, options)
graphWriter.Flush()
// Load and execute the template.
b, err := io.ReadAll(options.HTMLTemplate)
if err != nil {
log.Fatal().Err(err).Msg("Cannot read HTML template")
}
tmpl := template.Must(template.New("cyto-html").Parse(string(b)))
err = tmpl.Execute(writer, &map[string]interface{}{
"PageTitle": options.GraphName,
"GraphElementsJSON": buf.String(),
})
if err != nil {
log.Fatal().Err(err).Msg("Cannot render HTML template")
}
}
func getGraphElements(graph *tfgraph.Graph, options *RenderingOptions) *Elements {
// First, copy the graph dependencies as we may need to had some for the module -> module relations.
deps := make([]*tfgraph.Dependency, len(graph.Dependencies))
copy(deps, graph.Dependencies)
// Get the graph nodes.
var nodes []*Node
var addElement func(parent *tfgraph.Module, element tfgraph.ConfigElement)
addElement = func(parent *tfgraph.Module, element tfgraph.ConfigElement) {
// Add a node for the element.
node := &Node{
Data: NodeData{
ID: element.GetQualifiedName(),
Label: element.GetName(),
Type: element.GetTfType(),
},
Classes: []string{element.GetTfType()},
}
if parent != nil {
parentName := parent.GetQualifiedName()
node.Data.Parent = &parentName
}
nodes = append(nodes, node)
// If the element is a module, recursively add its children.
module, ok := element.(*tfgraph.Module)
if ok {
// Set the parent module ID.
node.Data.ParentModuleID = node.Data.Parent
if !options.EmbedModules && parent != nil {
// Remove the parent and create a dependency for the module -> module relation.
node.Data.Parent = nil
deps = append(deps, &tfgraph.Dependency{
Source: parent,
Destination: element,
})
}
for _, child := range module.Children {
addElement(module, child)
}
}
}
addElement(nil, graph.Root)
// Get the graph edges.
var edges []*Edge
for _, dep := range deps {
src := dep.Source.GetQualifiedName()
dst := dep.Destination.GetQualifiedName()
edge := &Edge{
Data: EdgeData{
ID: fmt.Sprintf("%s-%s", src, dst),
Source: src,
SourceType: dep.Source.GetTfType(),
Target: dst,
TargetType: dep.Destination.GetTfType(),
},
Classes: []string{fmt.Sprintf("%s-%s", dep.Source.GetTfType(), dep.Destination.GetTfType())},
}
edges = append(edges, edge)
}
return &Elements{
Nodes: nodes,
Edges: edges,
}
}