summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua')
-rw-r--r--lua/rclone.lua78
1 files changed, 77 insertions, 1 deletions
diff --git a/lua/rclone.lua b/lua/rclone.lua
index fb444d4..e7c84cd 100644
--- a/lua/rclone.lua
+++ b/lua/rclone.lua
@@ -1,17 +1,93 @@
+local config_file = ''
+local config_dir = ''
+
local function find_config_file()
+ if config_file ~= '' then
+ return config_file
+ end
+
-- TODO: find config file
+ 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.filename
+ break
+ end
+ end
+
+ current_dir = current_dir:parent()
+ end
+
+ return config_file
end
local function get_config()
-- TODO: rclone config dump --config=find_config_file()
+ local handle = io.popen('rclone config dump --config=' .. find_config_file(), 'r')
+ local config = handle:read('*all');
+ handle:close()
+
+ return config
end
-local function get_remote_config()
+local function get_remote_config(remote)
-- TODO: read json from get_config() and get default remote
+ local config = vim.fn.json_decode(get_config())
+ local target_remote = nil
+ local target_config = nil
+
+ if remote then
+ for remote_name, remote_config in pairs(config) do
+ if remote_name == remote then
+ target_remote = remote_name
+ target_config = remote_config
+ end
+ end
+ else
+ local config_length = 0
+ for _, _ in pairs(config) do
+ config_length = config_length + 1
+ 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
+ end
+ end
+
+ if target_remote == nil then
+ print("Couldn't find suitable remote")
+ end
+
+ return target_remote, target_config
end
local function copy()
-- TODO: rclone copy $LOCAL_PATH $REMOTE:$REMOTE_PATH --config=find_config_file()
+ 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
+
+ print('rclone copy ' .. local_path:absolute() .. ' ' .. remote .. ':' .. config.vim_rclone_remote_path .. ' --config=' .. find_config_file())
+
+ return config
end
return {