working demo of FSM implementation

main
Nick Dumas 2 years ago
parent 110753ef52
commit 369ad63f28

@ -0,0 +1,35 @@
package main
import (
"context"
"flag"
"go.uber.org/zap"
"code.ndumas.com/ndumas/obsidian-pipeline/fsm"
)
func main() {
var (
source, target, attachmentsDir, blogDir string
dev bool
)
l, _ := zap.NewProduction()
flag.BoolVar(&dev, "dev", false, "developer mode")
flag.StringVar(&source, "source", "", "source directory containing your vault")
flag.StringVar(&target, "target", "", "target directory containing your hugo site")
flag.StringVar(&attachmentsDir, "attachments", "", "directory containing your vault's attachments")
flag.StringVar(&blogDir, "blog", "", "vault directory containing blog posts to-be-published")
flag.Parse()
m := fsm.NewStateMachine(&fsm.NoteFound)
note := "bleep"
ctx := context.WithValue(context.Background(), "note", note)
_, err := m.Transition(ctx, "CopyPost")
if err != nil {
l.Fatal("could not transition from NoteFound to CopyPost", zap.Error(err))
}
}

@ -1,4 +1,4 @@
package obspipeline
package fsm
import (
"context"
@ -6,6 +6,7 @@ import (
)
type State string
type Event string
type Action func(ctx context.Context) error
@ -57,3 +58,14 @@ func (m *StateMachine) Transition(ctx context.Context, event Event) (*Node, erro
return m.CurrentNode, nil
}
func NewStateMachine(initialNode *Node) *StateMachine {
if initialNode == nil {
return &StateMachine{}
}
return &StateMachine{
initialNode: initialNode,
CurrentNode: initialNode,
}
}

@ -1,7 +1,13 @@
package obspipeline
package fsm
import (
"context"
"go.uber.org/zap"
)
var (
l, _ = zap.NewProduction()
)
var CopyPost = Node{
@ -28,6 +34,9 @@ var NoteFound = Node{
"CopyPost": &Transition{
Node: &CopyPost,
Action: func(ctx context.Context) error {
l = l.Named("NoteFound")
note := ctx.Value("note").(string)
l.Info("creating post from note", zap.String("filename", note))
// scan for attachments here
// if len() attachments > 0
//
@ -86,6 +95,6 @@ var SanitizeLinks = Node{
}
var Terminate = Node{
State: "SanitizeLinks",
State: "Terminate",
Transitions: map[Event]*Transition{},
}

Loading…
Cancel
Save