aboutsummaryrefslogtreecommitdiffstats
path: root/watcher.go
diff options
context:
space:
mode:
Diffstat (limited to 'watcher.go')
-rw-r--r--watcher.go61
1 files changed, 35 insertions, 26 deletions
diff --git a/watcher.go b/watcher.go
index 7df290d..8c8177e 100644
--- a/watcher.go
+++ b/watcher.go
@@ -2,7 +2,6 @@ package main
import (
"context"
- "log"
"os"
"path/filepath"
"sync"
@@ -11,7 +10,7 @@ import (
"github.com/fsnotify/fsnotify"
)
-func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, stack *GitignoreStack, dryRun, verbose bool) error {
+func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, stack *GitignoreStack, dryRun bool) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
@@ -48,9 +47,7 @@ func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, sta
continue
}
- if verbose {
- log.Printf("event: %s %s", event.Op, relPath)
- }
+ logDebug("event: %s %s", event.Op, relPath)
mu.Lock()
if t, exists := debounce[relPath]; exists {
@@ -60,7 +57,7 @@ func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, sta
mu.Lock()
delete(debounce, relPath)
mu.Unlock()
- handleEvent(event.Op, hostDir, dataDir, relPath, manifest, stack, dryRun, verbose, watcher)
+ handleEvent(event.Op, hostDir, dataDir, relPath, manifest, stack, dryRun, watcher)
})
mu.Unlock()
@@ -68,37 +65,37 @@ func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, sta
if !ok {
return nil
}
- log.Printf("watcher error: %v", err)
+ logErrorf("watcher: %v", err)
}
}
}
-func handleEvent(op fsnotify.Op, hostDir, dataDir, relPath string, manifest *Manifest, stack *GitignoreStack, dryRun, verbose bool, watcher *fsnotify.Watcher) {
+func handleEvent(op fsnotify.Op, hostDir, dataDir, relPath string, manifest *Manifest, stack *GitignoreStack, dryRun bool, watcher *fsnotify.Watcher) {
destPath := filepath.Join(dataDir, relPath)
srcPath := filepath.Join(hostDir, relPath)
switch {
case op&fsnotify.Create != 0:
- syncCreate(srcPath, destPath, relPath, manifest, stack, dryRun, verbose, watcher)
+ syncCreate(srcPath, destPath, relPath, manifest, stack, dryRun, watcher)
case op&fsnotify.Write != 0:
- syncWrite(srcPath, destPath, relPath, manifest, dryRun, verbose)
+ syncWrite(srcPath, destPath, relPath, manifest, dryRun)
case op&fsnotify.Remove != 0:
- syncRemove(destPath, relPath, manifest, dryRun, verbose)
+ syncRemove(destPath, relPath, manifest, dryRun)
case op&fsnotify.Rename != 0:
- syncRemove(destPath, relPath, manifest, dryRun, verbose)
+ syncRemove(destPath, relPath, manifest, dryRun)
}
}
-func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *GitignoreStack, dryRun, verbose bool, watcher *fsnotify.Watcher) {
+func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *GitignoreStack, dryRun bool, watcher *fsnotify.Watcher) {
info, err := os.Lstat(srcPath)
if err != nil {
if os.IsNotExist(err) {
return
}
- logError("stat", relPath, err)
+ logErrorf("stat %s: %v", relPath, err)
return
}
@@ -107,13 +104,14 @@ func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *Gi
stack.Push(srcPath)
}
if err := watcher.Add(srcPath); err != nil {
- logError("watch", relPath, err)
+ logErrorf("watch %s: %v", relPath, err)
}
if dryRun {
+ logInfo("[dry-run] would create dir: %s", relPath)
return
}
if err := os.MkdirAll(destPath, info.Mode()); err != nil {
- logError("mkdir", relPath, err)
+ logErrorf("mkdir %s: %v", relPath, err)
}
return
}
@@ -122,7 +120,7 @@ func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *Gi
if info.Mode()&os.ModeSymlink != 0 {
linkTarget, err = os.Readlink(srcPath)
if err != nil {
- logError("readlink", relPath, err)
+ logErrorf("readlink %s: %v", relPath, err)
return
}
}
@@ -136,6 +134,7 @@ func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *Gi
manifest.Files[relPath] = meta
if dryRun {
+ logInfo("[dry-run] would sync: %s", relPath)
return
}
@@ -144,23 +143,26 @@ func syncCreate(srcPath, destPath, relPath string, manifest *Manifest, stack *Gi
if linkTarget != "" {
if err := os.Symlink(linkTarget, destPath); err != nil {
- logError("symlink", relPath, err)
+ logErrorf("symlink %s: %v", relPath, err)
}
+ logInfo("sync: %s -> %s (symlink)", relPath, linkTarget)
return
}
if err := copyFileWithRetry(srcPath, destPath, info.Mode()); err != nil {
- logError("copy", relPath, err)
+ logErrorf("copy %s: %v", relPath, err)
+ } else {
+ logInfo("sync: %s", relPath)
}
}
-func syncWrite(srcPath, destPath, relPath string, manifest *Manifest, dryRun, verbose bool) {
+func syncWrite(srcPath, destPath, relPath string, manifest *Manifest, dryRun bool) {
info, err := os.Lstat(srcPath)
if err != nil {
if os.IsNotExist(err) {
return
}
- logError("stat", relPath, err)
+ logErrorf("stat %s: %v", relPath, err)
return
}
@@ -173,7 +175,7 @@ func syncWrite(srcPath, destPath, relPath string, manifest *Manifest, dryRun, ve
if info.Mode()&os.ModeSymlink != 0 {
linkTarget, err = os.Readlink(srcPath)
if err != nil {
- logError("readlink", relPath, err)
+ logErrorf("readlink %s: %v", relPath, err)
return
}
}
@@ -186,6 +188,7 @@ func syncWrite(srcPath, destPath, relPath string, manifest *Manifest, dryRun, ve
}
if dryRun {
+ logInfo("[dry-run] would update: %s", relPath)
return
}
@@ -193,25 +196,31 @@ func syncWrite(srcPath, destPath, relPath string, manifest *Manifest, dryRun, ve
if linkTarget != "" {
if err := os.Symlink(linkTarget, destPath); err != nil {
- logError("symlink", relPath, err)
+ logErrorf("symlink %s: %v", relPath, err)
}
+ logInfo("sync: %s -> %s (symlink)", relPath, linkTarget)
return
}
if err := copyFileWithRetry(srcPath, destPath, info.Mode()); err != nil {
- logError("copy", relPath, err)
+ logErrorf("copy %s: %v", relPath, err)
+ } else {
+ logInfo("sync: %s", relPath)
}
}
-func syncRemove(destPath, relPath string, manifest *Manifest, dryRun, verbose bool) {
+func syncRemove(destPath, relPath string, manifest *Manifest, dryRun bool) {
delete(manifest.Files, relPath)
if dryRun {
+ logInfo("[dry-run] would remove: %s", relPath)
return
}
if err := os.Remove(destPath); err != nil && !os.IsNotExist(err) {
- logError("remove", relPath, err)
+ logErrorf("remove %s: %v", relPath, err)
+ } else {
+ logInfo("remove: %s", relPath)
}
}