upload dir
This commit is contained in:
@@ -3,20 +3,27 @@ local cli = require("nvim-sync-ftp.cli")
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
|
|
||||||
local config = cli.getConfig()
|
local config = cli.getConfig()
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("SyncFtpMapToRemote", function (params)
|
vim.api.nvim_create_user_command("SyncFtpMapToRemote", function(params)
|
||||||
cli.MapToRemote(params)
|
cli.MapToRemote(params)
|
||||||
end,{
|
end, {
|
||||||
force = true,
|
force = true,
|
||||||
nargs = '*',
|
nargs = '*',
|
||||||
range = true,
|
range = true,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("SyncFtpUpload", function(params)
|
vim.api.nvim_create_user_command("SyncFtpUpload", function(params)
|
||||||
cli.Upload()
|
cli.UploadFile()
|
||||||
end,{
|
end, {
|
||||||
|
force = true,
|
||||||
|
nargs = '*',
|
||||||
|
range = true,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("SyncFtpUploadDir", function(params)
|
||||||
|
cli.UploadDir()
|
||||||
|
end, {
|
||||||
force = true,
|
force = true,
|
||||||
nargs = '*',
|
nargs = '*',
|
||||||
range = true,
|
range = true,
|
||||||
@@ -35,10 +42,7 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -5,18 +5,56 @@ local config = {}
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local function path_components(path)
|
||||||
|
-- Remove trailing slashes
|
||||||
|
path = path:gsub("/+$", "")
|
||||||
|
|
||||||
|
-- Extract the directory (dirname)
|
||||||
|
local dir = path:match("^(.*)/[^/]*$") or "."
|
||||||
|
if dir == "" then dir = "/" end -- Handle edge case for root-level paths like "/file.txt"
|
||||||
|
|
||||||
|
-- Extract the basename
|
||||||
|
local base = path:match("[^/]+$") or path
|
||||||
|
if base == "" then base = "/" end -- Handle edge case for "/" or empty path
|
||||||
|
|
||||||
|
-- Extract the parent directory (one level up from dir)
|
||||||
|
local parent
|
||||||
|
if dir == "." then
|
||||||
|
parent = ".." -- Parent of current directory
|
||||||
|
elseif dir == "/" then
|
||||||
|
parent = "/" -- Root's parent is itself
|
||||||
|
else
|
||||||
|
parent = dir:match("^(.*)/[^/]*$") or "/"
|
||||||
|
if parent == "" then parent = "/" end -- Handle edge case for "/dir"
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
dir = dir, -- Directory containing the file (dirname)
|
||||||
|
base = base, -- Filename without directory (basename)
|
||||||
|
parent = parent -- Parent directory one level up from dir
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dirname(str)
|
||||||
|
if str:match(".-/.-") then
|
||||||
|
local name = string.gsub(str, "(.*/)(.*)", "%1")
|
||||||
|
return name
|
||||||
|
else
|
||||||
|
return ''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function file_exists()
|
local function file_exists()
|
||||||
return vim.loop.fs_stat(filePath) ~= nil
|
return vim.loop.fs_stat(filePath) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local function create_file()
|
local function create_file()
|
||||||
|
|
||||||
local buf = vim.api.nvim_create_buf(true, true)
|
local buf = vim.api.nvim_create_buf(true, true)
|
||||||
|
|
||||||
vim.api.nvim_buf_set_name(buf, filePath)
|
vim.api.nvim_buf_set_name(buf, filePath)
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
|
||||||
"host hostName" ,
|
"host hostName",
|
||||||
"user userName",
|
"user userName",
|
||||||
"password password",
|
"password password",
|
||||||
"port 21",
|
"port 21",
|
||||||
@@ -24,7 +62,7 @@ local function create_file()
|
|||||||
"upload_to_save false",
|
"upload_to_save false",
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_buf_set_option(buf, "buftype", "")
|
vim.api.nvim_set_option_value("buftype", "", { buf = buf })
|
||||||
|
|
||||||
vim.api.nvim_buf_call(buf, function()
|
vim.api.nvim_buf_call(buf, function()
|
||||||
vim.cmd("write")
|
vim.cmd("write")
|
||||||
@@ -33,17 +71,14 @@ local function create_file()
|
|||||||
vim.api.nvim_win_set_buf(0, buf)
|
vim.api.nvim_win_set_buf(0, buf)
|
||||||
|
|
||||||
message.success("File created successfully!!!")
|
message.success("File created successfully!!!")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.MapToRemote ()
|
function M.MapToRemote()
|
||||||
|
|
||||||
if file_exists() then
|
if file_exists() then
|
||||||
message.warn("File already exists!!!");
|
message.warn("File already exists!!!");
|
||||||
else
|
else
|
||||||
create_file()
|
create_file()
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_file(path)
|
local function read_file(path)
|
||||||
@@ -62,14 +97,13 @@ local function read_file(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.getConfig()
|
function M.getConfig()
|
||||||
|
|
||||||
local configTemp = {};
|
local configTemp = {};
|
||||||
|
|
||||||
if file_exists() then
|
if file_exists() then
|
||||||
local content, err = read_file(filePath)
|
local content, err = read_file(filePath)
|
||||||
|
|
||||||
if content then
|
if content then
|
||||||
for i, line in ipairs(content) do
|
for _, line in ipairs(content) do
|
||||||
local first_word, second_word = line:match("^(%S+)%s+(%S+)$")
|
local first_word, second_word = line:match("^(%S+)%s+(%S+)$")
|
||||||
configTemp[first_word] = second_word
|
configTemp[first_word] = second_word
|
||||||
end
|
end
|
||||||
@@ -81,8 +115,7 @@ function M.getConfig()
|
|||||||
return configTemp
|
return configTemp
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.Upload()
|
function M.UploadFile()
|
||||||
|
|
||||||
config = M.getConfig()
|
config = M.getConfig()
|
||||||
local current_buffer_path = vim.api.nvim_buf_get_name(0)
|
local current_buffer_path = vim.api.nvim_buf_get_name(0)
|
||||||
local directoryTemp = directory
|
local directoryTemp = directory
|
||||||
@@ -101,11 +134,16 @@ function M.Upload()
|
|||||||
|
|
||||||
local remotePath = config.remote_path .. relative_path
|
local remotePath = config.remote_path .. relative_path
|
||||||
|
|
||||||
|
print("remotePath:", remotePath)
|
||||||
|
print("current_buffer_path:", current_buffer_path)
|
||||||
|
local dir = dirname(remotePath)
|
||||||
local command = string.format(
|
local command = string.format(
|
||||||
'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1',
|
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %s; cd %s;put %s; quit' > /dev/null 2>&1",
|
||||||
current_buffer_path, config.host, remotePath, config.user, config.password)
|
config.user, config.password, config.host, dir, dir, current_buffer_path
|
||||||
|
)
|
||||||
|
print("Generated command:", command)
|
||||||
|
|
||||||
vim.loop.spawn("sh", {
|
vim.uv.spawn("sh", {
|
||||||
args = { "-c", command },
|
args = { "-c", command },
|
||||||
}, function(code, signal)
|
}, function(code, signal)
|
||||||
if code == 0 then
|
if code == 0 then
|
||||||
@@ -118,7 +156,48 @@ function M.Upload()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.UploadDir()
|
||||||
|
config = M.getConfig()
|
||||||
|
local current_buffer_path = vim.api.nvim_buf_get_name(0)
|
||||||
|
local directoryTemp = directory
|
||||||
|
|
||||||
|
message.warn("Send directory... Wait")
|
||||||
|
|
||||||
|
if not directoryTemp:match("/$") then
|
||||||
|
directoryTemp = directoryTemp .. "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
local relative_path = current_buffer_path:sub(#directoryTemp + 1)
|
||||||
|
|
||||||
|
if not config.remote_path:match("/$") then
|
||||||
|
config.remote_path = config.remote_path .. "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
local remotePath = config.remote_path .. relative_path
|
||||||
|
|
||||||
|
local remote = path_components(remotePath)
|
||||||
|
local localDir = dirname(current_buffer_path)
|
||||||
|
local command = string.format(
|
||||||
|
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %q;cd %q;mirror -R %q; quit'",
|
||||||
|
config.user, config.password, config.host, remote.parent, remote.parent, localDir
|
||||||
|
)
|
||||||
|
print("Generated command:", command)
|
||||||
|
|
||||||
|
vim.uv.spawn("sh", {
|
||||||
|
args = { "-c", command },
|
||||||
|
}, function(code, signal)
|
||||||
|
if code == 0 then
|
||||||
|
vim.schedule(function()
|
||||||
|
message.info("Directory uploaded successfully!")
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
vim.schedule(function()
|
||||||
|
message.error("Directory upload failed with exit code: " .. code)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ M.error_once = vim.schedule_wrap(function(fmt, ...)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
M.success = vim.schedule_wrap(function(fmt, ...)
|
M.success = vim.schedule_wrap(function(fmt, ...)
|
||||||
vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP'})
|
vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP' })
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user