Skip to content

SoenkeD/sc-go-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SC Golang Templates

This repository contains the templates to use the sc CLI tool to generate Golang state machines.

Install sc CLI

Install sc by following the documentation.

Ensure ~/go/bin is in your path.

Setup

ℹ️ Requirements

A container runtime such as Docker is required.

Install Ginkgo to execute the tests

Getting started

To get started read the guide which goes through the features and intended usage of this tool on an example.

Setup a new project

  1. Navigate to the directory where the project should be created in
  2. 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

  1. Navigate into the project
  2. Modify the Print action to print the first argument
  3. Run make run to see the first running state machine

Add to existing code

ℹ️ The directory "{ROOT}/sc" is required to not exist
  1. Navigate to the go root directory in your project (where go.mod is located)
  2. 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

  1. Consider adding the Makefile. Then run make sc to generate the code.

  2. 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.

About

Golang templates for the sc state chart tool

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published