working, live version

main
Nick Dumas 2 years ago
parent f8f69a7187
commit ad1c6ae0b2

@ -34,6 +34,7 @@ func scanReader(r io.Reader, path string, matchChan matches) {
for _, match := range matches { for _, match := range matches {
dirs := strings.Split(path, "/") dirs := strings.Split(path, "/")
noteFilename := dirs[len(dirs)-2] noteFilename := dirs[len(dirs)-2]
log.Println("noteFilename:", noteFilename)
matchChan <- Attachment{Filename: match[1], Note: noteFilename} matchChan <- Attachment{Filename: match[1], Note: noteFilename}
} }
} }
@ -62,7 +63,7 @@ func walkFunc(matchChan matches) filepath.WalkFunc {
func moveAttachment(att Attachment, dest string) error { func moveAttachment(att Attachment, dest string) error {
destPath := filepath.Join(dest, strings.Split(att.Note, ".")[0]) destPath := filepath.Join(dest, strings.Split(att.Note, ".")[0])
log.Println("moving files:", destPath) log.Println("moving files into:", destPath)
_, err := copy(att.Filename, filepath.Join(destPath, filepath.Base(att.Filename))) _, err := copy(att.Filename, filepath.Join(destPath, filepath.Base(att.Filename)))
return err return err
} }
@ -94,11 +95,9 @@ func copy(src, dst string) (int64, error) {
func main() { func main() {
var ( var (
origin string target string
destination string
) )
flag.StringVar(&origin, "origin", "./content/", "directory containing Obsidian Markdown posts") flag.StringVar(&target, "target", "/tmp/blog/content/posts/", "target for posts and their attachments")
flag.StringVar(&destination, "destination", "/tmp/blog/content/posts/", "destination for posts and their attachments")
flag.Parse() flag.Parse()
@ -107,7 +106,7 @@ func main() {
// for i := 0; i < 5; i++ { // for i := 0; i < 5; i++ {
go func() { go func() {
for match := range attachments { for match := range attachments {
err := moveAttachment(match, destination) err := moveAttachment(match, target)
if err != nil { if err != nil {
log.Fatalf("error moving attachment: %s\n", err) log.Fatalf("error moving attachment: %s\n", err)
} }
@ -115,7 +114,7 @@ func main() {
}() }()
// } // }
err := filepath.Walk(origin, walkFunc(attachments)) err := filepath.Walk(target, walkFunc(attachments))
if err != nil { if err != nil {
log.Fatalf("error walking %s", err) log.Fatalf("error walking %s", err)
} }

Loading…
Cancel
Save