summaryrefslogtreecommitdiff
path: root/lua/rclone.lua
blob: 6df5efb77a0144f6ccb40cb36a1227f8b50672ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local config_file = ''
local config_dir = ''

---
-- Find config file and set global variables
--
-- @return string
local function find_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.filename

		return config_file
	    end
	end

	--if current_dir:is_root() then
	if current_dir.filename == '/' then
	    return ''
	end

	current_dir = current_dir:parent()
    end
end

---
-- Get rclone config as JSON
-- and parse to table
--
-- @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()

    return vim.fn.json_decode(config)
end

---
-- Get the desired remote config from the config file
--
-- @param remote string
--
-- @return string
-- @return table
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)
	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")
	-- TODO: check how to do error handling in lua/vim plugins
    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()'
--
-- @return any
local function copy()
    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()

    -- TODO: add configurable default exclude patterns
    local configurable_default_exclude_patterns = {'.git/'}
    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 .rcloneignore patterns
    local rcloneignore = Path:new(local_path .. '/.rcloneignore')
    if rcloneignore:exists() then
    	cmd = cmd .. ' --exclude-from=' .. rcloneignore.filename
    end

    -- TODO: add --log-level=INFO --log-file=.local/share/nvim/rclone_nvim/rclone.log

    os.execute(cmd)
    print(cmd)
    print("Copied!")

    return config
end

return {
    copy = copy,
}