This repository was archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgraph_structure.go
68 lines (61 loc) · 2.22 KB
/
graph_structure.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
package toscaviewer
import (
"bytes"
"fmt"
"github.com/owulveryck/toscalib"
"os"
"os/exec"
)
// ToscaGraph is a type holding the SVG representations of the graph
// the structure is "in memory" by now for debugging purpose
type ToscaGraph struct {
Graph map[string][]byte
ToscaDefinition *toscalib.ToscaDefinition
}
// Initialize the ToscaGraph structure
// This function executes the dot process on the dot representation of the current tosca definition
// It generates a svg representation of the tosca definition and another svg representation of the execution workflow
// according the standard lifecycle
func (toscaGraph *ToscaGraph) Initialize() error {
toscaDefinition := *toscaGraph.ToscaDefinition
var tempGraph map[string][]byte
tempGraph = make(map[string][]byte, 3)
for i, value := range []string{"ToscaDefinition", "ToscaWorkflow"} {
dotProcess := exec.Command("dot", "-Tsvg")
// Set the stdin stdout and stderr of the dot subprocess
stdinOfDotProcess, err := dotProcess.StdinPipe()
if err != nil {
fmt.Println(err) //replace with logger, or anything you want
}
defer stdinOfDotProcess.Close() // the doc says subProcess.Wait will close it, but I'm not sure, so I kept this line
readCloser, err := dotProcess.StdoutPipe()
if err != nil {
fmt.Println(err) //replace with logger, or anything you want
}
dotProcess.Stderr = os.Stderr
// Actually run the dot subprocess
if err = dotProcess.Start(); err != nil { //Use start, not run
fmt.Println("An error occured: ", err) //replace with logger, or anything you want
}
switch i {
case 0:
toscaDefinition.PrintDot(stdinOfDotProcess)
stdinOfDotProcess.Close()
case 1:
toscaDefinition.DotExecutionWorkflow(stdinOfDotProcess)
stdinOfDotProcess.Close()
}
// Read from stdout and store it in the correct structure
var buf bytes.Buffer
buf.ReadFrom(readCloser)
tempGraph[value] = buf.Bytes()
//toscaDefinition.PrintDot(stdinOfDotProcess)
//toscaDefinition.DotExecutionWorkflow(stdinOfDotProcess)
dotProcess.Wait()
}
tempGraph["ToscaYaml"] = toscaDefinition.Bytes()
//*toscaGraph = ToscaGraph(make(map[string][]byte, 2))
//(*toscaGraph).ToscaDefinition = &toscaDefinition
(*toscaGraph).Graph = tempGraph
return nil
}