Feat: Add support for multiple correlated directories

Added support for specifying multiple correlated directories for both the watcher and the
destination. The `watcher_dirs` and `hugo_dirs` variables now accept a list of directories
separated by `:` in the configuration file. Each directory in `watcher_dirs` corresponds
to the directory in the same position in `hugo_dirs`.

This enhancement allows for greater flexibility and customization when monitoring and
processing files in different directories.
This commit is contained in:
Óscar M. Lage 2024-04-10 10:59:53 +02:00
parent ad9b786f45
commit bfecae723a
3 changed files with 62 additions and 20 deletions

View File

@ -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: Configuration runs in `~/.config/obs2hugo/obs2hugo.ini` or `~/.config/obs2hugo.ini`, something like this:
```ini ```ini
watcher_dir = /Users/johndoe/vaults/obsidian/posts-hugo watcher_dirs = /Users/johndoe/vaults/obsidian/microposts-hugo:/Users/johndoe/vaults/obsidian/posts-hugo;
hugo_dir = /Users/johndoe/code/hugo/src/content/posts 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 ## Usage
1. Clone the repository to your local machine. 1. Clone the repository to your local machine.

52
main.go
View File

@ -32,11 +32,19 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
} }
// Watcher directory // Watcher directories
directory := config.Section("").Key("watcher_dir").String() watcherDirs := config.Section("").Key("watcher_dirs").String()
// Hugo content directory // 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 // Creating watcher
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
@ -46,8 +54,9 @@ func main() {
} }
defer watcher.Close() defer watcher.Close()
// Add directory to the watcher // Add directories to the watcher
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { for _, dir := range watcherDirsList {
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
fmt.Println("Error accessing to the directory:", err) fmt.Println("Error accessing to the directory:", err)
return err return err
@ -61,6 +70,7 @@ func main() {
fmt.Println("Error adding directory to the watcher:", err) fmt.Println("Error adding directory to the watcher:", err)
return return
} }
}
// Watcher event loop // Watcher event loop
for { for {
@ -72,7 +82,17 @@ func main() {
// TBD // TBD
case event.Op&fsnotify.Write == fsnotify.Write: case event.Op&fsnotify.Write == fsnotify.Write:
fmt.Println("File modified:", event.Name) 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: case event.Op&fsnotify.Remove == fsnotify.Remove:
fmt.Println("File deleted:", event.Name) fmt.Println("File deleted:", event.Name)
// TBD // 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) { func processModifiedFile(filePath, directory, destDir string) {
// Slugified file name without extension // Slugified file name without extension
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)) fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))

View File

@ -2,5 +2,5 @@
; ~/.config/obs2hugo/obs2hugo.ini or ; ~/.config/obs2hugo/obs2hugo.ini or
; ~/.config/obs2hugo.ini ; ~/.config/obs2hugo.ini
watcher_dir = /Users/johndoe/vaults/obsidian/posts-hugo watcher_dirs = /Users/johndoe/vaults/obsidian/microposts-hugo:/Users/johndoe/vaults/obsidian/posts-hugo;
hugo_dir = /Users/johndoe/code/hugo/src/content/posts hugo_dirs = /Users/johndoe/code/hugo/src/content/posts:/Users/johndoe/code/hugo/src/content/microposts