Configure synchronization of files and directories through a plugin.
moved some messages into debug
4 files changed, +46 -22
+15-1lua/syncmap/log.lua
| @@ -9,9 +9,11 @@ end | ||
| 9 | 9 | |
| 10 | 10 | ---@type table<LogLevel, vim.log.levels> |
| 11 | 11 | M.log_table = { |
| 12 | + ["trace"] = vim.log.levels.TRACE, | |
| 13 | + ["debug"] = vim.log.levels.DEBUG, | |
| 12 | 14 | ["info"] = vim.log.levels.INFO, |
| 13 | 15 | ["error"] = vim.log.levels.ERROR, |
| 14 | - ["none"] = vim.log.levels.OFF, | |
| 16 | + ["off"] = vim.log.levels.OFF, | |
| 15 | 17 | } |
| 16 | 18 | |
| 17 | 19 | ---Logs a message based on options log level |
| @@ -31,6 +33,18 @@ function M.info(msg) | ||
| 31 | 33 | M.log(msg, vim.log.levels.INFO) |
| 32 | 34 | end |
| 33 | 35 | |
| 36 | +---Logs message as debug | |
| 37 | +---@param msg any | |
| 38 | +function M.debug(msg) | |
| 39 | + M.log(msg, vim.log.levels.DEBUG) | |
| 40 | +end | |
| 41 | + | |
| 42 | +---Logs message as trace | |
| 43 | +---@param msg any | |
| 44 | +function M.trace(msg) | |
| 45 | + M.log(msg, vim.log.levels.TRACE) | |
| 46 | +end | |
| 47 | + | |
| 34 | 48 | ---Logs message as error |
| 35 | 49 | ---@param msg any |
| 36 | 50 | function M.error(msg) |
+18-13lua/syncmap/rsync.lua
| @@ -10,7 +10,6 @@ function M.run(args) | ||
| 10 | 10 | local cmd = { "rsync", unpack(args.flags), args.src, args.dst } |
| 11 | 11 | local result = vim.fn.system(cmd) |
| 12 | 12 | if vim.v.shell_error ~= 0 then |
| 13 | - log.error(table.concat(cmd, " ") .. " failed:\n" .. result) | |
| 14 | 13 | log.error( |
| 15 | 14 | string.format( |
| 16 | 15 | "Failed to run the the following rsync command.\n%s\nResult: %s", |
| @@ -19,7 +18,7 @@ function M.run(args) | ||
| 19 | 18 | ) |
| 20 | 19 | ) |
| 21 | 20 | else |
| 22 | - log.info("Successfully ran the following rsync command.\n" .. table.concat(cmd, " ")) | |
| 21 | + log.debug("Successfully ran the following rsync command.\n" .. table.concat(cmd, " ")) | |
| 23 | 22 | end |
| 24 | 23 | return result |
| 25 | 24 | end |
| @@ -35,24 +34,30 @@ function M.spawn_watcher(args) | ||
| 35 | 34 | }) |
| 36 | 35 | end |
| 37 | 36 | |
| 38 | - local cmd = string.format( | |
| 39 | - "while inotifywait -r -e modify,create,delete %q; do rsync %s %q %q; done", | |
| 40 | - args.src, | |
| 41 | - table.concat(args.flags, " "), | |
| 42 | - args.src, | |
| 43 | - args.dst | |
| 44 | - ) | |
| 37 | + local cmd = "while inotifywait " | |
| 38 | + if vim.uv.fs_stat(args.src).type == "directory" then | |
| 39 | + log.debug(string.format("Setting up inotify for a file.\n%s", args.src)) | |
| 40 | + cmd = cmd .. string.format("-r -e modify,create,delete %q; ", args.src) | |
| 41 | + else | |
| 42 | + log.debug(string.format("Setting up inotify for a file.\n%s", args.src)) | |
| 43 | + cmd = cmd .. string.format("-e %q; ", args.src) | |
| 44 | + end | |
| 45 | + cmd = cmd .. string.format("do rsync %s %q %q; done", table.concat(args.flags, " "), args.src, args.dst) | |
| 45 | 46 | local cmd_str_for_log = "sh -c '" .. cmd .. "'" |
| 46 | 47 | |
| 47 | - local _, pid = simple_cmd.spawn("sh", { | |
| 48 | + local handle, pid = simple_cmd.spawn("sh", { | |
| 48 | 49 | args = { "-c", cmd }, |
| 49 | 50 | path = "sh", |
| 50 | - cwd = args.src, | |
| 51 | + -- cwd = args.src, | |
| 51 | 52 | callback = function(code, status, _, pid) |
| 52 | - log.info(string.format("%s\n%d completed with %d code and %d status", cmd_str_for_log, pid, code, status)) | |
| 53 | + log.debug(string.format("%s\n%d completed with %d code and %d status", cmd_str_for_log, pid, code, status)) | |
| 53 | 54 | end, |
| 54 | 55 | }) |
| 55 | - log.info(string.format("Started the following command with pid %d\n%s", pid, cmd_str_for_log)) | |
| 56 | + if not handle then | |
| 57 | + log.error(string.format("Failed to start the following command.\n%s\nErr: %s", cmd_str_for_log, pid)) | |
| 58 | + else | |
| 59 | + log.debug(string.format("Started the following command with pid %d\n%s", pid, cmd_str_for_log)) | |
| 60 | + end | |
| 56 | 61 | return pid |
| 57 | 62 | end |
| 58 | 63 |
+10-7lua/syncmap/state.lua
| @@ -63,20 +63,21 @@ function M.sync() | ||
| 63 | 63 | -- Iterate active and sync what exists |
| 64 | 64 | for tag, pid in pairs(M.active) do |
| 65 | 65 | if not lookup[tag] then |
| 66 | - log.info(string.format("Removed from the config.\n%s\nRemoving the process %d.", tag, pid)) | |
| 66 | + log.info(string.format("Removed from the config.\n%s\nRemoving the process %s.", tag, pid)) | |
| 67 | 67 | M.active[tag] = nil |
| 68 | 68 | simple_cmd.kill(pid) |
| 69 | 69 | elseif not simple_cmd.exists(pid) then |
| 70 | + local m = lookup[tag] | |
| 71 | + local r = utils.row_to_rsync_params(m) | |
| 72 | + M.active[tag] = rsync.spawn_watcher(r) | |
| 70 | 73 | log.info( |
| 71 | 74 | string.format( |
| 72 | - "In active table but the process %d is not running.\n%s\nStarting a new process.", | |
| 75 | + "In active table but the process %s is not running.\n%s\nStarting a new process at %s.", | |
| 73 | 76 | pid, |
| 74 | - tag | |
| 77 | + tag, | |
| 78 | + M.active[tag] | |
| 75 | 79 | ) |
| 76 | 80 | ) |
| 77 | - local m = lookup[tag] | |
| 78 | - local r = utils.row_to_rsync_params(m) | |
| 79 | - M.active[tag] = rsync.spawn_watcher(r) | |
| 80 | 81 | end |
| 81 | 82 | end |
| 82 | 83 | |
| @@ -84,9 +85,11 @@ function M.sync() | ||
| 84 | 85 | for _, m in ipairs(opts.map) do |
| 85 | 86 | local tag = vim.fn.expand(m[1]) .. ":" .. vim.fn.expand(m[2]) |
| 86 | 87 | if not M.active[tag] then |
| 87 | - log.info(string.format("%s\nIn the config but not in the active table. Starting a new process.", tag)) | |
| 88 | 88 | local r = utils.row_to_rsync_params(m) |
| 89 | 89 | M.active[tag] = rsync.spawn_watcher(r) |
| 90 | + log.info( | |
| 91 | + string.format("%s\nIn the config but not in the active table.\nNew process is %s.", tag, M.active[tag]) | |
| 92 | + ) | |
| 90 | 93 | end |
| 91 | 94 | end |
| 92 | 95 | M.save() |
+3-1lua/syncmap/types.lua
| @@ -17,9 +17,11 @@ | ||
| 17 | 17 | ---| string # allow custom flags too |
| 18 | 18 | |
| 19 | 19 | ---@alias LogLevel |
| 20 | +---| "trace" show trace logs | |
| 21 | +---| "debug" show debug logs | |
| 20 | 22 | ---| "info" show info logs |
| 21 | 23 | ---| "error" show error logs |
| 22 | ----| "none" show no logs | |
| 24 | +---| "off" turn off logs | |
| 23 | 25 | |
| 24 | 26 | ---Ex: `--exclude-from='.gitignore'` |
| 25 | 27 | ---@alias ExcludeFrom string @[default=".gitignore"] Adds --exclude-from in rsync |