From ca44f39a27073c708857d15d363fe880165028e8 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 30 Nov 2021 21:46:50 +0100 Subject: Improve error handling, add command arguments and add sync command --- lua/rclone.lua | 140 +++++++++++++++++++++++++++++++++++++++++++++--------- plugin/rclone.vim | 3 +- 2 files changed, 119 insertions(+), 24 deletions(-) diff --git a/lua/rclone.lua b/lua/rclone.lua index 98935a7..693d8ef 100644 --- a/lua/rclone.lua +++ b/lua/rclone.lua @@ -64,6 +64,7 @@ end -- -- @return string -- @return table +-- @return nil local function get_remote_config(remote) local config = get_config() local target_remote = nil @@ -74,8 +75,8 @@ local function get_remote_config(remote) target_remote = remote target_config = config[remote] else - print("Couldn't find remote: " .. remote) - os.exit() + print("Couldn't find remote: \"" .. remote .. '"') + return end else local config_length = 0 @@ -83,6 +84,11 @@ local function get_remote_config(remote) config_length = config_length + 1 end + if config_length == 0 then + print('No remotes defined in config file at "' .. find_config_file() .. '"') + return + end + if config_length == 1 then for remote_name, remote_config in pairs(config) do target_remote = remote_name @@ -95,43 +101,69 @@ local function get_remote_config(remote) target_config = remote_config end end + + if target_remote == nil then + print('No default remote specified') + return + end end end - if target_remote == nil then - print("Couldn't find suitable remote") - os.exit() - -- TODO: check how to do error handling in lua/vim plugins - end if target_config.vim_rclone_local_path == nil or target_config.vim_rclone_local_path == '' or target_config.vim_rclone_remote_path == nil or target_config.vim_rclone_remote_path == '' then - print("Couldn't find suitable remote") - os.exit() + print('No local and or remote path set for remote "' .. target_remote .. '"') + return end return target_remote, target_config end --- --- Call the 'rclone copy' command with config values --- 'rclone copy $LOCAL_PATH $REMOTE:$REMOTE_PATH --config=find_config_file()' +-- Build the local path -- --- @return any -local function copy() +-- @param config table +-- +-- @return string +local function prepare_cmd_local_path(config) local Path = require('plenary.path') - local remote, config = get_remote_config() - local local_path = Path:new(config.vim_rclone_local_path) + local_path._cwd = config_dir -- set cwd to config dir to resolve path correctly local_path = local_path:absolute() local_path = local_path:gsub('%./', '') -- remove wrongly kept ./ for relative paths - local cmd = - 'rclone copy ' .. - local_path .. ' ' .. - remote .. ':' .. config.vim_rclone_remote_path .. - ' --config=' .. find_config_file() + return local_path +end + +--- +-- Add remote path +-- +-- @param config table +-- @param remote string +-- +-- @return string +local function build_cmd_remote_path(config, remote) + return remote .. ':' .. config.vim_rclone_remote_path +end + +--- +-- Add config location path +-- +-- @return string +local function build_cmd_config() + return '--config=' .. find_config_file() +end + +--- +-- Add exclude patterns +-- +-- @param local_path string +-- +-- @return string +local function build_cmd_exclude(local_path) + local Path = require('plenary.path') + local cmd = '' -- TODO: add configurable default exclude patterns local configurable_default_exclude_patterns = {'.git/', '.gitignore', '.rcloneignore'} @@ -151,10 +183,43 @@ local function copy() cmd = cmd .. ' --exclude-from=' .. rcloneignore.filename end - -- add logging + return cmd:gsub('^%s?', '') -- ltrim +end + +--- +-- Add logging +-- +-- @return string +local function build_cmd_logging() local log_level = 'INFO' - cmd = cmd .. ' --log-level=' .. log_level .. - ' --log-file=' .. vim.fn.stdpath('data') .. '/rclone_nvim/rclone.log' + local cmd = '' + + cmd = '--log-level=' .. log_level + cmd = cmd .. ' --log-file=' .. vim.fn.stdpath('data') .. '/rclone_nvim/rclone.log' + + return cmd +end + +--- +-- Call the 'rclone copy' command with config values +-- 'rclone copy ' +-- +-- @return any +local function copy(remote_section) + local remote, config = get_remote_config(remote_section) + if remote == nil then + return + end + + local local_path = prepare_cmd_local_path(config) + + local cmd = + 'rclone copy ' .. + local_path .. ' ' .. + build_cmd_remote_path(config, remote) .. ' ' .. + build_cmd_config() .. ' ' .. + build_cmd_exclude(local_path) .. ' ' .. + build_cmd_logging() os.execute(cmd) print(cmd) @@ -163,7 +228,36 @@ local function copy() return config end +--- +-- Call the 'rclone sync' command with config values +-- 'rclone sync ' +-- +-- @return any +local function sync(remote_section) + local remote, config = get_remote_config(remote_section) + if remote == nil then + return + end + + local local_path = prepare_cmd_local_path(config) + + local cmd = + 'rclone sync ' .. + local_path .. ' ' .. + build_cmd_remote_path(config, remote) .. ' ' .. + build_cmd_config() .. ' ' .. + build_cmd_exclude(local_path) .. ' ' .. + build_cmd_logging() + + os.execute(cmd) + print(cmd) + print("Synced!") + + return config +end + return { copy = copy, + sync = sync, } diff --git a/plugin/rclone.vim b/plugin/rclone.vim index b3a4623..3173b6a 100644 --- a/plugin/rclone.vim +++ b/plugin/rclone.vim @@ -5,7 +5,8 @@ endif let s:old_cpo = &cpo set cpo&vim -command! RcloneCopy lua require('rclone').copy() +command! -nargs=? RcloneCopy lua require('rclone').copy() +command! -nargs=? RcloneSync lua require('rclone').sync() let &cpo = s:old_cpo unlet s:old_cpo -- cgit v1.2.3