From 4f76a1d57198d59a9ad5851717a6ee2a3fb76273 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Mon, 19 Jun 2023 23:05:40 -0400 Subject: [PATCH] First step: inventory posts in provided directory --- cmd/obp/cmd/hugo.go | 4 ++-- hugo.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cmd/obp/cmd/hugo.go b/cmd/obp/cmd/hugo.go index cc1e5ec..1c375fe 100644 --- a/cmd/obp/cmd/hugo.go +++ b/cmd/obp/cmd/hugo.go @@ -27,7 +27,7 @@ var hugoCmd = &cobra.Command{ target := viper.GetString("target") source := viper.GetString("source") - err := obp.CopyPosts(target, source) + err := obp.CopyPosts(source, target) if err != nil { return fmt.Errorf("error copying posts in %q: %w", source, err) } @@ -50,7 +50,7 @@ func init() { hugoCmd.Flags().StringP("target", "t", "", "hugo content/ directory") hugoCmd.MarkFlagsRequiredTogether("source", "target") - err := viper.BindPFlag("source", hugoCmd.Flags().Lookup("schema")) + err := viper.BindPFlag("source", hugoCmd.Flags().Lookup("source")) if err != nil { log.Panicln("error binding viper to source flag:", err) } diff --git a/hugo.go b/hugo.go index 7c19731..10cb9ff 100644 --- a/hugo.go +++ b/hugo.go @@ -1,6 +1,46 @@ package obp +import ( + "fmt" + "io/fs" + "log" + "os" + "path/filepath" + "strings" +) + func CopyPosts(src, dst string) error { + /* + err := os.MkdirAll(dst, 0777) + if err != nil && !os.IsExist(err) { + return fmt.Errorf("error creating target directory %q: %w", dst, err) + } + */ + + posts := make([]string, 0) + + srcRoot := os.DirFS(src) + err := fs.WalkDir(srcRoot, ".", func(path string, d fs.DirEntry, err error) error { + // here's where I walk through the source directory and collect all the markdown notes + if err != nil { + return fmt.Errorf("could not walk %q: %w", path, err) + } + + if d.IsDir() { + return nil + } + + if strings.HasSuffix(path, ".md") { + posts = append(posts, filepath.Join(src, path)) + } + + return nil + }) + if err != nil { + return fmt.Errorf("walkfunc failed: %w", err) + } + log.Printf("%#+v\n", posts) + return nil }