Skip to content

Commit

Permalink
Add support for --drawio-args flag in graph command (#2143)
Browse files Browse the repository at this point in the history
support for drawio-args
  • Loading branch information
FloSch62 authored Jul 18, 2024
1 parent a3b76a5 commit 3789b73
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
26 changes: 20 additions & 6 deletions clab/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,34 @@ func (c *CLab) ServeTopoGraph(tmpl, staticDir, srv string, topoD TopoData) error
return http.ListenAndServe(srv, nil)
}

func (c *CLab) GenerateDrawioDiagram(version string) error {
func (c *CLab) GenerateDrawioDiagram(version string, additionalFlags []string) error {
topoFile := c.TopoPaths.TopologyFilenameBase()

cmd := exec.Command("sudo", "docker", "run", "-v",
fmt.Sprintf("%s:/data", c.TopoPaths.TopologyFileDir()),
cmdArgs := []string{
"docker", "run",
"-v", fmt.Sprintf("%s:/data", c.TopoPaths.TopologyFileDir()),
fmt.Sprintf("ghcr.io/srl-labs/clab-io-draw:%s", version),
"-i", topoFile)
"-i", topoFile,
}

log.Infof("Generating draw.io diagram with version: %s", version)

// Process additional flags
for _, flag := range additionalFlags {
parts := strings.Fields(flag)
cmdArgs = append(cmdArgs, parts...)
}

cmd := exec.Command("sudo", cmdArgs...)

out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.CombinedOutput() failed with %s\n", err)
log.Errorf("Command execution failed: %v", err)
log.Errorf("Command output: %s", string(out))
return fmt.Errorf("failed to generate diagram: %w\nOutput: %s", err, string(out))
}

log.Infof("Diagram created. %s", out)
log.Infof("Diagram created successfully. Output: %s", string(out))

return nil
}
5 changes: 4 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
mermaidDirection string
drawio bool
drawioVersion string
drawioArgs []string
staticDir string
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func graphFn(_ *cobra.Command, _ []string) error {
}

if drawio {
return c.GenerateDrawioDiagram(drawioVersion)
return c.GenerateDrawioDiagram(drawioVersion, drawioArgs)
}

gtopo := clab.GraphTopo{
Expand Down Expand Up @@ -153,6 +154,8 @@ func init() {
graphCmd.Flags().BoolVarP(&dot, "dot", "", false, "generate dot file")
graphCmd.Flags().BoolVarP(&mermaid, "mermaid", "", false, "print mermaid flowchart to stdout")
graphCmd.Flags().StringVarP(&mermaidDirection, "mermaid-direction", "", "TD", "specify direction of mermaid dirgram")
graphCmd.Flags().StringSliceVar(&drawioArgs, "drawio-args", []string{},
"Additional flags to pass to the drawio diagram generation tool (can be specified multiple times)")
graphCmd.Flags().BoolVarP(&drawio, "drawio", "", false, "generate drawio diagram file")
graphCmd.Flags().StringVarP(&drawioVersion, "drawio-version", "", "latest",
"version of the clab-io-draw container to use for generating drawio diagram file")
Expand Down
6 changes: 6 additions & 0 deletions docs/cmd/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ The `group` property set to the predefined value will automatically auto-align t

When `graph` command is called with the `--drawio` flag, containerlab will leverage the [`clab-io-draw`](https://github.com/srl-labs/clab-io-draw) project to generate the drawio file that represents the topology in a graphical form and can be imported into [draw.io](https://draw.io).

You can pass additional arguments to the `clab-io-draw` tool using the `--drawio-args=` flag. For example:

```
containerlab graph --drawio --drawio-args="--theme nokia_dark --layout horizontal" -t topo.yaml
```

### Mermaid

When `graph` is called with the `--mermaid` flag containerlab generates a graph description file in [Mermaid graph format](https://mermaid.js.org/syntax/flowchart.html). Several [Markdown renders](https://mermaid.js.org/ecosystem/integrations-community.html) such as Github, Gitlab, and Notion support rendering embeded mermaid graphs in code blocks. If the results of the render are not satisfying the result can be imported into [draw.io](https://draw.io) and further edited.
Expand Down

0 comments on commit 3789b73

Please sign in to comment.