From 936cffb13eba79eaed22fc6e7341abf2ad003ea3 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 19 Jul 2024 12:36:57 +0200 Subject: add "directory" option and change option handling --- lua/rclone/init.lua | 112 +++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 53 deletions(-) (limited to 'lua/rclone') diff --git a/lua/rclone/init.lua b/lua/rclone/init.lua index 1c25688..07eec24 100644 --- a/lua/rclone/init.lua +++ b/lua/rclone/init.lua @@ -229,6 +229,29 @@ local function build_cmd_logging() return cmd end +--- +-- build path to directory for local and remote +-- +-- @return any +local function build_cmd_option_directory(local_path, options) + local Path = require('plenary.path') + local local_directory_path = '' + + local_directory_path = Path:new(vim.fn.expand(options['directory'])) + + if local_directory_path:exists() ~= true then + print('Directory "' .. local_directory_path:absolute() .. '" to download to missing') + return false + end + + local_directory_path = local_directory_path:absolute() + + local local_path_pattern = vim.pesc(local_path) + local local_directory_path_relative = local_directory_path:gsub(local_path_pattern, '') + + return local_directory_path_relative +end + --- -- Call the 'rclone copy' command with config values -- 'rclone copy ' @@ -242,15 +265,21 @@ local function copy(options) local local_path = prepare_cmd_local_path(config) + -- build path to directory for local and remote + local local_directory_path_relative = '' + if options['directory'] then + local_directory_path_relative = build_cmd_option_directory(local_path, options) + end + local cmd = 'rclone copy ' .. - local_path .. ' ' .. - build_cmd_remote_path(config, remote) .. ' ' .. + local_path .. local_directory_path_relative .. ' ' .. + build_cmd_remote_path(config, remote) .. local_directory_path_relative .. ' ' .. build_cmd_config() .. ' ' .. build_cmd_exclude(local_path) .. ' ' .. build_cmd_logging() - if options['--dry-run'] then + if options['dry-run'] then print(cmd) else os.execute(cmd) @@ -295,7 +324,7 @@ local function copyFile(options) build_cmd_exclude(local_path) .. ' ' .. build_cmd_logging() - if options['--dry-run'] then + if options['dry-run'] then print(cmd) else os.execute(cmd) @@ -319,15 +348,21 @@ local function download(options) local local_path = prepare_cmd_local_path(config) + -- build path to directory for local and remote + local local_directory_path_relative = '' + if options['directory'] then + local_directory_path_relative = build_cmd_option_directory(local_path, options) + end + local cmd = 'rclone copy ' .. - build_cmd_remote_path(config, remote) .. ' ' .. - local_path .. ' ' .. + build_cmd_remote_path(config, remote) .. local_directory_path_relative .. ' ' .. + local_path .. local_directory_path_relative .. ' ' .. build_cmd_config() .. ' ' .. build_cmd_exclude(local_path) .. ' ' .. build_cmd_logging() - if options['--dry-run'] then + if options['dry-run'] then print(cmd) else if vim.fn.confirm('Download?', '&Yes\n&No') == 1 then @@ -361,20 +396,7 @@ local function downloadFile(options) -- build path to file for local and remote local Path = require('plenary.path') - local local_file_path = '' - - if options['directory'] then - local_file_path = Path:new(vim.fn.expand(options['directory'])) - - if local_file_path:exists() ~= true then - print('Directory "' .. local_file_path:absolute() .. '" to download to missing') - return - end - - local_file_path = local_file_path:absolute() - else - local_file_path = Path:new(vim.fn.expand('%')):absolute() - end + local_file_path = Path:new(vim.fn.expand('%')):absolute() local local_path_pattern = vim.pesc(local_path) @@ -386,10 +408,6 @@ local function downloadFile(options) local local_file_path_relative = local_file_path:gsub(local_path_pattern, '') local local_file_path_relative_parent = Path:new(local_file_path_relative):parent().filename - if options['directory'] then - local_file_path_relative_parent = local_file_path_relative - end - local cmd = 'rclone copy ' .. build_cmd_remote_path(config, remote) .. local_file_path_relative .. ' ' .. @@ -398,7 +416,7 @@ local function downloadFile(options) build_cmd_exclude(local_path) .. ' ' .. build_cmd_logging() - if options['--dry-run'] then + if options['dry-run'] then print(cmd) else if vim.fn.confirm('Download file?', '&Yes\n&No') == 1 then @@ -434,7 +452,7 @@ local function sync(options) build_cmd_exclude(local_path) .. ' ' .. build_cmd_logging() - if options['--dry-run'] then + if options['dry-run'] then print(cmd) else os.execute(cmd) @@ -469,11 +487,7 @@ local function mount(options) '--read-only --no-checksum' .. ' ' .. '--daemon' - if options['--dry-run'] then - if options['--return'] then - return cmd - end - + if options['dry-run'] then print(cmd) else os.execute('mkdir -p ' .. local_mount_path) @@ -486,30 +500,22 @@ end -- -- @return any local function umount(options) - local mount_options = vim.deepcopy(options) - mount_options['--dry-run'] = true - mount_options['--return'] = true - local mount_cmd = mount(mount_options) - - local pid_cmd = "ps aux | grep -i '" .. mount_cmd .. "' | grep -v grep | awk '{print $2}'" - local cmd = function (pid) - return 'kill ' .. pid + local remote, config = get_remote_config(options.remote) + if remote == nil then + return end - if options['--dry-run'] then - print(pid_cmd) - print(cmd('$PID_RETURNED_FROM_CMD_ABOVE')) - else - local pid_handle = io.popen(pid_cmd) - local pid = pid_handle:read() - pid_handle:close() + local local_path = prepare_cmd_local_path(config) + local local_mount_path = build_cmd_local_mount_path(local_path, options, config) - if pid == nil then - print('No pid found for mount process') - return - end + local cmd = + 'fusermount -u ' .. local_mount_path .. + '&& umount ' .. local_mount_path - os.execute(cmd(pid)) + if options['dry-run'] then + print(cmd) + else + os.execute(cmd) end end @@ -542,7 +548,7 @@ local function run(cmd, ...) local options = {} for _, arg in ipairs(args) do if arg:find('%-%-') == 1 then - options[arg] = true + options[string.sub(arg, 3)] = true elseif arg:find('=', 1) == nil then options['remote'] = arg else -- cgit v1.2.3