diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/rclone/init.lua | 385 | 
1 files changed, 192 insertions, 193 deletions
diff --git a/lua/rclone/init.lua b/lua/rclone/init.lua index 2a21076..1c25688 100644 --- a/lua/rclone/init.lua +++ b/lua/rclone/init.lua @@ -6,42 +6,42 @@ local config_dir = ''  --  -- @return string  local function find_config_file() -    if config_file ~= '' then -    	return config_file +  if config_file ~= '' then +    return config_file +  end + +  local scandir = require('plenary.scandir').scan_dir +  local Path = require('plenary.path') + +  local current_dir = Path:new('.') +  while config_file == '' do +    local files = scandir(current_dir.filename, { depth = 1 }) +    for _, file in ipairs(files) do +      if string.find(file, 'rclone.conf') then +        config_file = file +        config_dir = current_dir:absolute() + +        return config_file +      end      end -    local scandir = require('plenary.scandir').scan_dir -    local Path = require('plenary.path') - -    local current_dir = Path:new('.') -    while config_file == '' do -    	local files = scandir(current_dir.filename, { depth = 1 }) -	for _, file in ipairs(files) do -	    if string.find(file, 'rclone.conf') then -		config_file = file -		config_dir = current_dir:absolute() - -		return config_file -	    end -	end - -	--if current_dir:is_root() then -	if current_dir.filename == '/' then -	    -- set to 'rclone config file' output -	    local handle = io.popen('rclone config file', 'r') -	    handle:read() -- advance one line because we want the second line only -	    local output = handle:read(); -	    handle:close() - -	    config_file = output -	    local config_file_path = Path:new(config_file) -	    config_dir = config_file_path:parent().filename - -	    return config_file -	end - -	current_dir = current_dir:parent() +    --if current_dir:is_root() then +    if current_dir.filename == '/' then +      -- set to 'rclone config file' output +      local handle = io.popen('rclone config file', 'r') +      handle:read() -- advance one line because we want the second line only +      local output = handle:read(); +      handle:close() + +      config_file = output +      local config_file_path = Path:new(config_file) +      config_dir = config_file_path:parent().filename + +      return config_file      end + +    current_dir = current_dir:parent() +  end  end  --- @@ -50,11 +50,11 @@ end  --  -- @return string  local function get_config() -    local handle = io.popen('rclone config dump --config=' .. find_config_file(), 'r') -    local config = handle:read('*all'); -    handle:close() +  local handle = io.popen('rclone config dump --config=' .. find_config_file(), 'r') +  local config = handle:read('*all'); +  handle:close() -    return vim.fn.json_decode(config) +  return vim.fn.json_decode(config)  end  --- @@ -66,57 +66,57 @@ end  -- @return table  -- @return nil  local function get_remote_config(remote) -    local config = get_config() -    local target_remote = nil -    local target_config = nil - -    if remote then -	if config[remote] then -	    target_remote = remote -	    target_config = config[remote] -	else -	    print("Couldn't find remote: \"" .. remote .. '"') -	    return -	end +  local config = get_config() +  local target_remote = nil +  local target_config = nil + +  if remote then +    if config[remote] then +      target_remote = remote +      target_config = config[remote]      else -        local config_length = 0 -	for _, _ in pairs(config) do -	    config_length = config_length + 1 -        end +      print("Couldn't find remote: \"" .. remote .. '"') +      return +    end +  else +    local config_length = 0 +    for _, _ in pairs(config) do +      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 -		target_config = remote_config -	    end -	else -	    for remote_name, remote_config in pairs(config) do -		if remote_config.vim_rclone_default then -		    target_remote = remote_name -		    target_config = remote_config -		end -	    end - -	    if target_remote == nil then -		print('No default remote specified') -		return -	    end -	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 +        target_config = remote_config +      end +    else +      for remote_name, remote_config in pairs(config) do +        if remote_config.vim_rclone_default then +          target_remote = remote_name +          target_config = remote_config +        end +      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('No local and or remote path set for remote "' .. target_remote .. '"') -	return +      if target_remote == nil then +        print('No default remote specified') +        return +      end      end +  end -    return target_remote, target_config + +  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('No local and or remote path set for remote "' .. target_remote .. '"') +    return +  end + +  return target_remote, target_config  end  --- @@ -126,18 +126,18 @@ end  --  -- @return string  local function prepare_cmd_local_path(config) -    if (config.vim_rclone_local_path == '.' or config.vim_rclone_local_path == './') then -	return config_dir -    end +  if (config.vim_rclone_local_path == '.' or config.vim_rclone_local_path == './') then +    return config_dir +  end -    local Path = require('plenary.path') -    local local_path = Path:new(config.vim_rclone_local_path) +  local Path = require('plenary.path') +  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_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 -    return local_path +  return local_path  end  --- @@ -148,7 +148,7 @@ end  --  -- @return string  local function build_cmd_remote_path(config, remote) -    return remote .. ':' .. config.vim_rclone_remote_path +  return remote .. ':' .. config.vim_rclone_remote_path  end  --- @@ -173,7 +173,7 @@ end  --  -- @return string  local function build_cmd_config() -    return '--config=' .. find_config_file() +  return '--config=' .. find_config_file()  end  --- @@ -183,36 +183,36 @@ end  --  -- @return string  local function build_cmd_exclude(local_path) -    local Path = require('plenary.path') -    local cmd = '' +  local Path = require('plenary.path') +  local cmd = '' -    -- add configurable default exclude patterns -    local configurable_default_exclude_patterns = vim.g.vim_rclone_configurable_default_exclude_patterns or {'.git/', '.gitignore', '.rcloneignore'} -    for _, pattern in pairs(configurable_default_exclude_patterns) do -    	cmd = cmd .. ' --exclude=' .. pattern -    end +  -- add configurable default exclude patterns +  local configurable_default_exclude_patterns = vim.g.vim_rclone_configurable_default_exclude_patterns or {'.git/', '.gitignore', '.rcloneignore'} +  for _, pattern in pairs(configurable_default_exclude_patterns) do +    cmd = cmd .. ' --exclude=' .. pattern +  end -    -- add .gitignore patterns -    local gitignore = Path:new(local_path .. '/.gitignore') -    if gitignore:exists() then -        cmd = cmd .. ' --exclude-from=' .. gitignore.filename -    end +  -- add .gitignore patterns +  local gitignore = Path:new(local_path .. '/.gitignore') +  if gitignore:exists() then +    cmd = cmd .. ' --exclude-from=' .. gitignore.filename +  end -    local handle = io.popen('git config --get core.excludesfile', 'r') -    local output = handle:read() -    handle:close() -    local gitignore_global = Path:new((Path:new(output)):expand()) -    if output ~= nil and gitignore_global:exists() then -      cmd = cmd .. ' --exclude-from=' .. gitignore_global.filename -    end +  local handle = io.popen('git config --get core.excludesfile', 'r') +  local output = handle:read() +  handle:close() +  local gitignore_global = Path:new((Path:new(output)):expand()) +  if output ~= nil and gitignore_global:exists() then +    cmd = cmd .. ' --exclude-from=' .. gitignore_global.filename +  end -    -- add .rcloneignore patterns -    local rcloneignore = Path:new(local_path .. '/.rcloneignore') -    if rcloneignore:exists() then -    	cmd = cmd .. ' --exclude-from=' .. rcloneignore.filename -    end +  -- add .rcloneignore patterns +  local rcloneignore = Path:new(local_path .. '/.rcloneignore') +  if rcloneignore:exists() then +    cmd = cmd .. ' --exclude-from=' .. rcloneignore.filename +  end -    return cmd:gsub('^%s?', '') -- ltrim +  return cmd:gsub('^%s?', '') -- ltrim  end  --- @@ -220,13 +220,13 @@ end  --  -- @return string  local function build_cmd_logging() -    local log_level = 'INFO' -    local cmd = '' +  local log_level = 'INFO' +  local cmd = '' -    cmd = '--log-level=' .. log_level -    cmd = cmd .. ' --log-file=' .. vim.fn.stdpath('data') .. '/rclone_nvim/rclone.log' +  cmd = '--log-level=' .. log_level +  cmd = cmd .. ' --log-file=' .. vim.fn.stdpath('data') .. '/rclone_nvim/rclone.log' -    return cmd +  return cmd  end  --- @@ -235,30 +235,30 @@ end  --  -- @return any  local function copy(options) -    local remote, config = get_remote_config(options.remote) -    if remote == nil then -	return -    end +  local remote, config = get_remote_config(options.remote) +  if remote == nil then +    return +  end -    local local_path = prepare_cmd_local_path(config) +  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() +  local cmd = +    'rclone copy ' .. +    local_path .. ' ' .. +    build_cmd_remote_path(config, remote) .. ' ' .. +    build_cmd_config() .. ' ' .. +    build_cmd_exclude(local_path) .. ' ' .. +    build_cmd_logging() -    if options['--dry-run'] then -      print(cmd) -    else -      os.execute(cmd) -    end +  if options['--dry-run'] then +    print(cmd) +  else +    os.execute(cmd) +  end -    print("Copied!") +  print("Copied!") -    return config +  return config  end  --- @@ -267,43 +267,43 @@ end  --  -- @return any  local function copyFile(options) -    local remote, config = get_remote_config(options.remote) -    if remote == nil then -	return -    end +  local remote, config = get_remote_config(options.remote) +  if remote == nil then +    return +  end -    local local_path = prepare_cmd_local_path(config) +  local local_path = prepare_cmd_local_path(config) -    -- build relative path to file for local and remote -    local Path = require('plenary.path') -    local local_file_path = Path:new(vim.fn.expand('%')):absolute() -    local local_path_pattern = vim.pesc(local_path) +  -- build relative path to file for local and remote +  local Path = require('plenary.path') +  local local_file_path = Path:new(vim.fn.expand('%')):absolute() +  local local_path_pattern = vim.pesc(local_path) -    if local_file_path:find(local_path_pattern) == nil then -      print('File path not in local path') -      return -    end +  if local_file_path:find(local_path_pattern) == nil then +    print('File path not in local path') +    return +  end -    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 +  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 -    local cmd = -	'rclone copy ' .. -	local_path .. local_file_path_relative .. ' ' .. -	build_cmd_remote_path(config, remote) .. local_file_path_relative_parent .. ' ' .. -	build_cmd_config() .. ' ' .. -	build_cmd_exclude(local_path) .. ' ' .. -	build_cmd_logging() +  local cmd = +    'rclone copy ' .. +    local_path .. local_file_path_relative .. ' ' .. +    build_cmd_remote_path(config, remote) .. local_file_path_relative_parent .. ' ' .. +    build_cmd_config() .. ' ' .. +    build_cmd_exclude(local_path) .. ' ' .. +    build_cmd_logging() -    if options['--dry-run'] then -      print(cmd) -    else -      os.execute(cmd) -    end +  if options['--dry-run'] then +    print(cmd) +  else +    os.execute(cmd) +  end -    print("Copied file!") +  print("Copied file!") -    return config +  return config  end  --- @@ -333,9 +333,9 @@ local function download(options)      if vim.fn.confirm('Download?', '&Yes\n&No') == 1 then        os.execute(cmd)        vim.cmd([[ -	let curBuf=bufnr('%') -	execute 'bufdo execute "confirm e" | update' -	execute 'buffer ' . curBuf +        let curBuf=bufnr('%') +        execute 'bufdo execute "confirm e" | update' +        execute 'buffer ' . curBuf        ]])      else        print('Download canceled!') @@ -419,30 +419,30 @@ end  --  -- @return any  local function sync(options) -    local remote, config = get_remote_config(options.remote) -    if remote == nil then -	return -    end +  local remote, config = get_remote_config(options.remote) +  if remote == nil then +    return +  end -    local local_path = prepare_cmd_local_path(config) +  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() +  local cmd = +    'rclone sync ' .. +    local_path .. ' ' .. +    build_cmd_remote_path(config, remote) .. ' ' .. +    build_cmd_config() .. ' ' .. +    build_cmd_exclude(local_path) .. ' ' .. +    build_cmd_logging() -    if options['--dry-run'] then -      print(cmd) -    else -      os.execute(cmd) -    end +  if options['--dry-run'] then +    print(cmd) +  else +    os.execute(cmd) +  end -    print("Synced!") +  print("Synced!") -    return config +  return config  end  --- @@ -560,4 +560,3 @@ end  return {    run = run,  } -  | 
