summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-26 10:35:59 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-26 10:35:59 +0200
commitef70ea3c783be09501053469c5b4b5bb51ef2df2 (patch)
tree78afa61e303a11d6dd61b1a8c15c8d1ad7906786
parent5998a69ebbb174df4425fec878263d2a5d7cb096 (diff)
FSM: Do graph generation in directory with mermaid-cli
-rw-r--r--internal/fsm.go24
-rw-r--r--state.go2
2 files changed, 20 insertions, 6 deletions
diff --git a/internal/fsm.go b/internal/fsm.go
index 4d2b266..5aa3f50 100644
--- a/internal/fsm.go
+++ b/internal/fsm.go
@@ -3,7 +3,9 @@ package internal
import (
"fmt"
"os"
+ "os/exec"
"sort"
+ "path"
)
type (
@@ -92,12 +94,14 @@ type FSM struct {
Current FSMStateID
// Info to be passed from the parent state
+ Name string
StateCallback func(string, string, string)
Logger *FileLogger
+ Directory string
Debug bool
}
-func (fsm *FSM) Init(callback func(string, string, string), logger *FileLogger, debug bool) {
+func (fsm *FSM) Init(name string, callback func(string, string, string), logger *FileLogger, directory string, debug bool) {
fsm.States = FSMStates{
DEREGISTERED: {{NO_SERVER, "Client registers"}},
NO_SERVER: {{CHOSEN_SERVER, "User chooses a server"}},
@@ -110,8 +114,10 @@ func (fsm *FSM) Init(callback func(string, string, string), logger *FileLogger,
CONNECTED: {{AUTHENTICATED, "OS reports disconnected"}},
}
fsm.Current = DEREGISTERED
+ fsm.Name = name
fsm.StateCallback = callback
fsm.Logger = logger
+ fsm.Directory = directory
fsm.Debug = debug
}
@@ -129,17 +135,25 @@ func (fsm *FSM) HasTransition(check FSMStateID) bool {
return false
}
+func (fsm *FSM) getGraphFilename(extension string) string {
+ debugPath := path.Join(fsm.Directory, fsm.Name)
+ return fmt.Sprintf("%s%s", debugPath, extension)
+}
+
func (fsm *FSM) writeGraph() {
graph := fsm.GenerateGraph()
-
- f, err := os.Create("debug.graph")
+ graphFile := fsm.getGraphFilename(".graph")
+ graphImgFile := fsm.getGraphFilename(".png")
+ f, err := os.Create(graphFile)
if err != nil {
fsm.Logger.Log(LOG_INFO, fmt.Sprintf("Failed to write debug fsm graph with error %v", err))
}
- defer f.Close()
-
f.WriteString(graph)
+ f.Close()
+ cmd := exec.Command("mmdc", "-i", graphFile, "-o", graphImgFile)
+
+ cmd.Start()
}
func (fsm *FSM) GoTransitionWithData(newState FSMStateID, data string, background bool) bool {
diff --git a/state.go b/state.go
index 6b3141b..29f8a1e 100644
--- a/state.go
+++ b/state.go
@@ -51,7 +51,7 @@ func (state *VPNState) Register(name string, directory string, stateCallback fun
}
// Initialize the FSM
- state.FSM.Init(stateCallback, &state.Logger, debug)
+ state.FSM.Init(name, stateCallback, &state.Logger, directory, debug)
state.Debug = debug
// Initialize the Config