This repository contains the templates to use the sc CLI tool to generate Golang state machines.
Install sc
by following the documentation.
Ensure ~/go/bin
is in your path.
A container runtime such as Docker or Podman is required.
Install Ginkgo to execute the tests
To get started read the guide which goes through the features and intended usage of this tool on an example.
- Navigate to the directory where the project should be created in
- Set the desired parameters and execute the command below
sc init --setup https://github.com/SoenkeD/sc-go-templates/main/sc/setup \
--name myctl \
--root $PWD/demo \
--module demo
See further information for the command here
- Navigate into the project
- Modify the
Print
action to print the first argument - Run
make run
to see the first running state machine
- Navigate to the go root directory in your project
(where
go.mod
is located) - Set the desired parameters and execute the command below
sc init --setup https://github.com/SoenkeD/sc-go-templates/main/sc/add \
--name myctl \
--root $PWD/demo \
--module demo \
--ctl src/controller
Change --ctl
to the directory the state machine code should be located.
See further information for the command
here
-
Consider adding the Makefile. Then run
make sc
to generate the code. -
To use the generated code access it e.g. by
package main
import (
"log"
"demo/src/controller/myctl"
"demo/src/controller/myctl/controller"
"demo/src/controller/myctl/state"
)
func main() {
ctl := myctl.InitCtl(
&state.Ctx{},
controller.ControllerSettingsInput{
AfterInit: &controller.DefaultAfterInitHandler{
State: state.ExtendedState{
Hello: "world",
},
},
},
)
res, err := ctl.Run()
if err != nil {
log.Fatalln(err.PanicErr, err.StateErr)
}
log.Println(res)
}
and add to Hello string
to ExtendedState
in src/controller/myctl/state/ExtendedState.go
.