Implement a walkFunc that feeds filenames into a channel

now i can lazily process them, fan them out, whatever
main
Nick Dumas 2 years ago
parent 369ad63f28
commit b91c1ebf6e

@ -3,6 +3,9 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"io/fs"
"os"
"strings"
"go.uber.org/zap" "go.uber.org/zap"
@ -24,12 +27,39 @@ func main() {
flag.Parse() flag.Parse()
m := fsm.NewStateMachine(&fsm.NoteFound) fileNames := make(chan string)
note := "bleep"
ctx := context.WithValue(context.Background(), "note", note) walkFunc := func(path string, d fs.DirEntry, err error) error {
_, err := m.Transition(ctx, "CopyPost") if err != nil {
if err != nil { return err
l.Fatal("could not transition from NoteFound to CopyPost", zap.Error(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))
} }
} }

@ -34,7 +34,11 @@ var NoteFound = Node{
"CopyPost": &Transition{ "CopyPost": &Transition{
Node: &CopyPost, Node: &CopyPost,
Action: func(ctx context.Context) error { 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) note := ctx.Value("note").(string)
l.Info("creating post from note", zap.String("filename", note)) l.Info("creating post from note", zap.String("filename", note))
// scan for attachments here // scan for attachments here

Loading…
Cancel
Save