diff --git a/README.md b/README.md index 097ffa5..dbab1eb 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,12 @@ This repository contains a Go program that allows converting and copying content Configuration runs in `~/.config/obs2hugo/obs2hugo.ini` or `~/.config/obs2hugo.ini`, something like this: ```ini -watcher_dir = /Users/johndoe/vaults/obsidian/posts-hugo -hugo_dir = /Users/johndoe/code/hugo/src/content/posts +watcher_dirs = /Users/johndoe/vaults/obsidian/microposts-hugo:/Users/johndoe/vaults/obsidian/posts-hugo; +hugo_dirs = /Users/johndoe/code/hugo/src/content/posts:/Users/johndoe/code/hugo/src/content/microposts ``` +Note: The `watcher_dirs` and `hugo_dirs` variables must have the same number of elements, taking into account that `:` acts as a separator. They are correlated so the first element in `watcher_dirs` corresponds to the first element in `hugo_dirs`, the second element in `watcher_dirs` corresponds to the second element in `hugo_dirs`, and so forth. + ## Usage 1. Clone the repository to your local machine. diff --git a/main.go b/main.go index 4d972cf..d029d3a 100644 --- a/main.go +++ b/main.go @@ -32,11 +32,19 @@ func main() { log.Fatalln(err) } } - // Watcher directory - directory := config.Section("").Key("watcher_dir").String() + // Watcher directories + watcherDirs := config.Section("").Key("watcher_dirs").String() // Hugo content directory - destDir := config.Section("").Key("hugo_dir").String() + destDirs := config.Section("").Key("hugo_dirs").String() + + watcherDirsList := strings.Split(watcherDirs, ":") + destDirsList := strings.Split(destDirs, ":") + + if len(watcherDirsList) != len(destDirsList) { + fmt.Println("Error: watcher_dirs and hugo_dirs must have the same number of elements") + return + } // Creating watcher watcher, err := fsnotify.NewWatcher() @@ -46,20 +54,22 @@ func main() { } defer watcher.Close() - // Add directory to the watcher - err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { + // Add directories to the watcher + for _, dir := range watcherDirsList { + err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Println("Error accessing to the directory:", err) + return err + } + if info.IsDir() { + return watcher.Add(path) + } + return nil + }) if err != nil { - fmt.Println("Error accessing to the directory:", err) - return err + fmt.Println("Error adding directory to the watcher:", err) + return } - if info.IsDir() { - return watcher.Add(path) - } - return nil - }) - if err != nil { - fmt.Println("Error adding directory to the watcher:", err) - return } // Watcher event loop @@ -72,7 +82,17 @@ func main() { // TBD case event.Op&fsnotify.Write == fsnotify.Write: fmt.Println("File modified:", event.Name) - processModifiedFile(event.Name, directory, destDir) + triggerDir := getTriggerDirectory(event.Name, watcherDirsList) + if triggerDir == "" { + fmt.Println("Error: Unable to determine trigger directory for event:", event.Name) + continue + } + newDestDir := getDestinationDirectory(triggerDir, watcherDirsList, destDirsList) + if newDestDir == "" { + fmt.Println("Error: Unable to determine destination directory for trigger directory:", triggerDir) + continue + } + processModifiedFile(event.Name, triggerDir, newDestDir) case event.Op&fsnotify.Remove == fsnotify.Remove: fmt.Println("File deleted:", event.Name) // TBD @@ -86,6 +106,26 @@ func main() { } } +// Function to determine which watcher directory triggered the event +func getTriggerDirectory(eventPath string, dirs []string) string { + for _, dir := range dirs { + if strings.HasPrefix(eventPath, dir) { + return dir + } + } + return "" +} + +// Function to determine the destination directory for the trigger directory +func getDestinationDirectory(triggerDir string, watcherDirsList, destDirsList []string) string { + for i, dir := range watcherDirsList { + if dir == triggerDir { + return destDirsList[i] + } + } + return "" +} + func processModifiedFile(filePath, directory, destDir string) { // Slugified file name without extension fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)) diff --git a/obs2hugo.ini.sample b/obs2hugo.ini.sample index ae56569..9a8eed2 100644 --- a/obs2hugo.ini.sample +++ b/obs2hugo.ini.sample @@ -2,5 +2,5 @@ ; ~/.config/obs2hugo/obs2hugo.ini or ; ~/.config/obs2hugo.ini -watcher_dir = /Users/johndoe/vaults/obsidian/posts-hugo -hugo_dir = /Users/johndoe/code/hugo/src/content/posts +watcher_dirs = /Users/johndoe/vaults/obsidian/microposts-hugo:/Users/johndoe/vaults/obsidian/posts-hugo; +hugo_dirs = /Users/johndoe/code/hugo/src/content/posts:/Users/johndoe/code/hugo/src/content/microposts