forked from seamia/protodot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphviz.go
77 lines (63 loc) · 1.78 KB
/
graphviz.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
// Copyright 2017 Seamia Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"os/exec"
"github.com/seamia/tools/support"
)
// (optionally) running 'graphviz' on the given .dot file
func graphviz(src string, svg, png bool) {
svgPath := ""
pngPath := ""
action := ""
if tmp, err := support.GetLocation(g_config, "action"); err == nil {
action = tmp
}
if png || svg {
svgPath = src + ".svg"
pngPath = src + ".png"
if graphviz, err := support.GetLocation(g_config, "graphviz"); err == nil && len(graphviz) > 0 {
if svg {
status("generating .svg file")
if output, e := exec.Command(graphviz, "-Tsvg", src).Output(); e == nil {
if err := os.WriteFile(svgPath, output, 0755); err != nil {
status("error on write", err)
svgPath = ""
}
} else {
status("error on exec", e)
svgPath = ""
}
}
if png {
status("generating .png file")
if output, e := exec.Command(graphviz, "-Tpng", src).Output(); e == nil {
if err := os.WriteFile(pngPath, output, 0755); err != nil {
status("error on write", err)
pngPath = ""
}
} else {
status("error on exec", e)
pngPath = ""
}
}
} else {
status("failed to get 'graphviz' location from config file")
}
}
if len(action) > 0 {
envs := os.Environ()
envs = append(envs, "PROTODOT_DOT=\""+src+"\"")
envs = append(envs, "PROTODOT_SVG=\""+svgPath+"\"")
envs = append(envs, "PROTODOT_PNG=\""+pngPath+"\"")
cmd := exec.Command(action)
cmd.Env = envs
if output, err := cmd.Output(); err == nil {
status("custom action said:", string(output))
} else {
status("Failed to execute custom action [", action, "] due to", err)
}
}
}