First step: inventory posts in provided directory

main
Nick Dumas 2 years ago
parent 94701a01b2
commit 4f76a1d571

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

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

Loading…
Cancel
Save