diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-04-04 13:28:44 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-04-04 13:28:44 +0200 |
| commit | 93bb4bccdf2b1b201451e0130fbfa90f310dba7b (patch) | |
| tree | ce8c0f808be851f8767bd96c643d4ebb177ab0f0 | |
| parent | 0d860b20a8b6b61d937124ee1955074b12c3f8e6 (diff) | |
Add graph visualisation tool to the cli
| -rw-r--r-- | cli/main.go | 84 |
1 files changed, 72 insertions, 12 deletions
diff --git a/cli/main.go b/cli/main.go index 942aef2..d0b2165 100644 --- a/cli/main.go +++ b/cli/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "os" "os/exec" "strings" @@ -24,29 +25,88 @@ func logState(oldState string, newState string, data string) { } } +func getGraphviz(fsm *eduvpn.FSM, graph string) string { + if fsm == nil { + return graph + } + + for name, state := range fsm.States { + for _, transition := range state.Transition { + graph += "\n" + "cluster_" + name.String() + "-> cluster_" + transition.String() + } + + graph += "\nsubgraph cluster_" + name.String() + "{\n" + if (state.Locked) { + graph += "bgcolor=\"red\"\n" + } + if (fsm.Current == name) { + graph += "color=\"blue\"\n" + } else { + graph += "color=\"\"\n" + } + graph += "label=" + name.String() + graph = getGraphviz(state.Sub, graph) + graph += "\n}" + } + return graph +} + +func generateGraph() string { + state := eduvpn.GetVPNState() + + state.InitializeFSM() + + + graph := "digraph fsm {\n" + graph += "nodesep=2" + graph = getGraphviz(state.FSM, graph) + graph += "\n}" + + return graph +} + func main() { + generateGraph() + fileGraph := flag.String("dumpgraph", "", "Dump the FSM to a graphviz fdp file") urlArg := flag.String("url", "", "The url of the vpn") flag.Parse() - urlString := *urlArg + fileGraphString := *fileGraph + if fileGraphString != "" { + f, err := os.Create(fileGraphString) - if urlString == "" { - log.Fatal("Error: -url is required") - } + if err != nil { + log.Fatalf("Failed to create file %s with error %v", fileGraphString, err) + } - if !strings.HasPrefix(urlString, "https://") { - urlString = "https://" + urlString + defer f.Close() + + f.WriteString(generateGraph()) + + log.Printf("Graph written to file: %s, use 'fdp %s -Tsvg > graph.svg' from graphviz to save to a svg file called graph.svg\n", fileGraphString, fileGraphString) + return } + urlString := *urlArg - state := eduvpn.GetVPNState() + if urlString != "" { + if !strings.HasPrefix(urlString, "https://") { + urlString = "https://" + urlString + } + + state := eduvpn.GetVPNState() + + state.Register("org.eduvpn.app.linux", "configs", logState) + config, configErr := state.Connect(urlString) + + if configErr != nil { + fmt.Printf("Config error %v", configErr) + return + } - state.Register("org.eduvpn.app.linux", "configs", logState) - config, configErr := state.Connect(urlString) + log.Println(config) - if configErr != nil { - fmt.Printf("Config error %v", configErr) return } - print(config) + flag.PrintDefaults() } |
