Configure synchronization of files and directories through a plugin.
rock solid working with files now too
2 files changed, +20 -9
+8-5lua/syncmap/rsync.lua
| @@ -35,12 +35,16 @@ function M.spawn_watcher(args) | ||
| 35 | 35 | end |
| 36 | 36 | |
| 37 | 37 | local cmd = "while inotifywait " |
| 38 | - if vim.uv.fs_stat(args.src).type == "directory" then | |
| 38 | + local stat = vim.uv.fs_stat(args.src) | |
| 39 | + if stat and stat.type == "directory" then | |
| 39 | 40 | log.debug(string.format("Setting up inotify for a file.\n%s", args.src)) |
| 40 | 41 | cmd = cmd .. string.format("-r -e modify,create,delete %q; ", args.src) |
| 41 | - else | |
| 42 | + elseif stat and stat.type == "file" then | |
| 42 | 43 | log.debug(string.format("Setting up inotify for a file.\n%s", args.src)) |
| 43 | - cmd = cmd .. string.format("-e %q; ", args.src) | |
| 44 | + cmd = cmd .. string.format("-e modify %q; ", args.src) | |
| 45 | + else | |
| 46 | + log.error(string.format("Could not stat %s", args.src)) | |
| 47 | + return "NO STAT" | |
| 44 | 48 | end |
| 45 | 49 | cmd = cmd .. string.format("do rsync %s %q %q; done", table.concat(args.flags, " "), args.src, args.dst) |
| 46 | 50 | local cmd_str_for_log = "sh -c '" .. cmd .. "'" |
| @@ -48,7 +52,6 @@ function M.spawn_watcher(args) | ||
| 48 | 52 | local handle, pid = simple_cmd.spawn("sh", { |
| 49 | 53 | args = { "-c", cmd }, |
| 50 | 54 | path = "sh", |
| 51 | - -- cwd = args.src, | |
| 52 | 55 | callback = function(code, status, _, pid) |
| 53 | 56 | log.debug(string.format("%s\n%d completed with %d code and %d status", cmd_str_for_log, pid, code, status)) |
| 54 | 57 | end, |
| @@ -58,7 +61,7 @@ function M.spawn_watcher(args) | ||
| 58 | 61 | else |
| 59 | 62 | log.debug(string.format("Started the following command with pid %d\n%s", pid, cmd_str_for_log)) |
| 60 | 63 | end |
| 61 | - return pid | |
| 64 | + return pid, cmd_str_for_log | |
| 62 | 65 | end |
| 63 | 66 | |
| 64 | 67 | return M |
+12-4lua/syncmap/state.lua
| @@ -69,11 +69,13 @@ function M.sync() | ||
| 69 | 69 | elseif not simple_cmd.exists(pid) then |
| 70 | 70 | local m = lookup[tag] |
| 71 | 71 | local r = utils.row_to_rsync_params(m) |
| 72 | - M.active[tag] = rsync.spawn_watcher(r) | |
| 72 | + local cmd_str | |
| 73 | + M.active[tag], cmd_str = rsync.spawn_watcher(r) | |
| 73 | 74 | log.info( |
| 74 | 75 | string.format( |
| 75 | - "In active table but the process %s is not running.\n%s\nStarting a new process at %s.", | |
| 76 | + "In active table but the process %s is not running.\n%s\n%s\nStarting a new process at %s.", | |
| 76 | 77 | pid, |
| 78 | + cmd_str, | |
| 77 | 79 | tag, |
| 78 | 80 | M.active[tag] |
| 79 | 81 | ) |
| @@ -86,9 +88,15 @@ function M.sync() | ||
| 86 | 88 | local tag = vim.fn.expand(m[1]) .. ":" .. vim.fn.expand(m[2]) |
| 87 | 89 | if not M.active[tag] then |
| 88 | 90 | local r = utils.row_to_rsync_params(m) |
| 89 | - M.active[tag] = rsync.spawn_watcher(r) | |
| 91 | + local cmd_str | |
| 92 | + M.active[tag], cmd_str = rsync.spawn_watcher(r) | |
| 90 | 93 | log.info( |
| 91 | - string.format("%s\nIn the config but not in the active table.\nNew process is %s.", tag, M.active[tag]) | |
| 94 | + string.format( | |
| 95 | + "%s\nIn the config but not in the active table.\n%s\nNew process is %s.", | |
| 96 | + tag, | |
| 97 | + cmd_str, | |
| 98 | + M.active[tag] | |
| 99 | + ) | |
| 92 | 100 | ) |
| 93 | 101 | end |
| 94 | 102 | end |