-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdfd_render.go
More file actions
54 lines (45 loc) · 965 Bytes
/
Copy pathdfd_render.go
File metadata and controls
54 lines (45 loc) · 965 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
48
49
50
51
52
53
54
package spec
import (
"bytes"
"context"
"os"
"github.com/goccy/go-graphviz"
)
func render(raw []byte, format graphviz.Format) ([]byte, error) {
ctx := context.Background()
g, err := graphviz.New(ctx)
if err != nil {
return nil, err
}
defer g.Close()
graph, err := graphviz.ParseBytes(raw)
if err != nil {
return nil, err
}
defer graph.Close()
var buf bytes.Buffer
if err := g.Render(ctx, graph, format, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func dotToPngBytes(raw []byte) ([]byte, error) {
return render(raw, graphviz.PNG)
}
func dotToSvgBytes(raw []byte) ([]byte, error) {
return render(raw, graphviz.SVG)
}
func dotToPng(raw []byte, file string) error {
b, err := dotToPngBytes(raw)
if err != nil {
return err
}
return os.WriteFile(file, b, 0644)
}
func dotToSvg(raw []byte, file string) error {
b, err := dotToSvgBytes(raw)
if err != nil {
return err
}
return os.WriteFile(file, b, 0644)
}