From b91c1ebf6e70da7c6aa57c123095a9322f9a164b Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Mon, 1 May 2023 16:38:27 -0400 Subject: [PATCH] Implement a walkFunc that feeds filenames into a channel now i can lazily process them, fan them out, whatever --- cmd/fsm_demo/main.go | 42 ++++++++++++++++++++++++++++++++++++------ fsm/nodes.go | 6 +++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/cmd/fsm_demo/main.go b/cmd/fsm_demo/main.go index 552e422..21e7e23 100644 --- a/cmd/fsm_demo/main.go +++ b/cmd/fsm_demo/main.go @@ -3,6 +3,9 @@ package main import ( "context" "flag" + "io/fs" + "os" + "strings" "go.uber.org/zap" @@ -24,12 +27,39 @@ func main() { 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)) + fileNames := make(chan string) + + walkFunc := func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil + } + fileNames <- path + + return nil + } + + go func() { + for fn := range fileNames { + if !strings.HasSuffix(fn, ".md") { + continue + } + m := fsm.NewStateMachine(&fsm.NoteFound) + ctx := context.WithValue(context.Background(), "note", fn) + _, err := m.Transition(ctx, "CopyPost") + if err != nil { + l.Fatal("could not transition from NoteFound to CopyPost", zap.Error(err)) + } + } + }() + root := os.DirFS(source) + err := fs.WalkDir(root, ".", walkFunc) + if err != nil { + l.Fatal("error walking for files", zap.Error(err)) } + } diff --git a/fsm/nodes.go b/fsm/nodes.go index 27e425d..4a12df5 100644 --- a/fsm/nodes.go +++ b/fsm/nodes.go @@ -34,7 +34,11 @@ var NoteFound = Node{ "CopyPost": &Transition{ Node: &CopyPost, Action: func(ctx context.Context) error { - l = l.Named("NoteFound") + //TODO: Here's the place to do validation + // I had an idea about parsing frontmatter into go structs, + // and then writing them out to a NullWriter via protobuf encoding. + // this would do schema checking which is nice + l := l.Named("NoteFound") note := ctx.Value("note").(string) l.Info("creating post from note", zap.String("filename", note)) // scan for attachments here