Skip to content

Commit

Permalink
cmd/export: Update statemachine docs generation to generate dot and J…
Browse files Browse the repository at this point in the history
…SON formats
  • Loading branch information
joelrebel committed Feb 6, 2023
1 parent 902cba4 commit aa13270
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
67 changes: 60 additions & 7 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/metal-toolbox/flasher/internal/outofband"
sm "github.com/metal-toolbox/flasher/internal/statemachine"
"github.com/spf13/cobra"

"github.com/emicklei/dot"
)

var cmdExport = &cobra.Command{
Expand All @@ -24,6 +27,7 @@ var cmdExport = &cobra.Command{
type exportFlags struct {
action bool
task bool
json bool
}

var (
Expand All @@ -38,8 +42,36 @@ var cmdExportStatemachine = &cobra.Command{
},
}

func exportStatemachine(ctx context.Context) {
func exportAsDot(data []byte) (string, error) {
smJSON := &sw.StateMachineJSON{}

if err := json.Unmarshal(data, smJSON); err != nil {
return "", err
}

g := dot.NewGraph(dot.Directed)
nodes := map[string]dot.Node{}

for _, transition := range smJSON.TransitionRules {
_, exists := nodes[transition.DestinationState]
if !exists {
nodes[transition.DestinationState] = g.Node(transition.DestinationState)
}

for _, sourceState := range transition.SourceStates {
_, exists := nodes[sourceState]
if !exists {
nodes[sourceState] = g.Node(sourceState)
}

g.Edge(nodes[sourceState], nodes[transition.DestinationState], transition.Name)
}
}

return g.String(), nil
}

func exportStatemachine(ctx context.Context) {
if exportFlagSet.task {
exportTaskStatemachine(ctx)
os.Exit(0)
Expand All @@ -61,12 +93,22 @@ func exportTaskStatemachine(ctx context.Context) {
log.Fatal(err)
}

json, err := m.DescribeAsJSON()
data, err := m.DescribeAsJSON()
if err != nil {
log.Fatal(err)
}

if exportFlagSet.json {
fmt.Println(string(data))
os.Exit(0)
}

val, err := exportAsDot(data)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(json))
fmt.Println(val)
}

func exportOutofbandActionStatemachine(ctx context.Context) {
Expand All @@ -75,17 +117,28 @@ func exportOutofbandActionStatemachine(ctx context.Context) {
log.Fatal(err)
}

json, err := m.DescribeAsJSON()
data, err := m.DescribeAsJSON()
if err != nil {
log.Fatal(err)
}

if exportFlagSet.json {
fmt.Println(string(data))
os.Exit(0)
}

val, err := exportAsDot(data)
if err != nil {
log.Fatal(err)
}

fmt.Println(string(json))
fmt.Println(val)
}

func init() {
cmdExportStatemachine.PersistentFlags().BoolVarP(&exportFlagSet.action, "action", "", false, "export action statemachine as JSON")
cmdExportStatemachine.PersistentFlags().BoolVarP(&exportFlagSet.task, "task", "", false, "export task statemachine as JSON")
cmdExportStatemachine.PersistentFlags().BoolVarP(&exportFlagSet.action, "action", "", false, "export action statemachine in the Graphviz Dot format")
cmdExportStatemachine.PersistentFlags().BoolVarP(&exportFlagSet.task, "task", "", false, "export task statemachine in the Graphviz Dot format")
cmdExportStatemachine.PersistentFlags().BoolVarP(&exportFlagSet.json, "json", "", false, "export task statemachine in the JSON format")

cmdExport.AddCommand(cmdExportStatemachine)
rootCmd.AddCommand(cmdExport)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
require (
github.com/cockroachdb/cockroach-go/v2 v2.2.16 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/dot v1.1.0 // indirect
github.com/ericlagergren/decimal v0.0.0-20211103172832-aca2edc11f73 // indirect
github.com/friendsofgo/errors v0.9.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/emicklei/dot v1.1.0 h1:lv5Fthv7E2bcY2ZqA3nYWl9QFCuY8fzX8OtWCIqiuZo=
github.com/emicklei/dot v1.1.0/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down

0 comments on commit aa13270

Please sign in to comment.