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:
parent
ad9b786f45
commit
bfecae723a
@ -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.
|
||||
|
72
main.go
72
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))
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user