diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/rclone.lua | 140 | 
1 files changed, 117 insertions, 23 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 <args>' +-- +-- @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 <args>' +-- +-- @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,  } | 
