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 (
"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))
}
}

@ -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

Loading…
Cancel
Save