Adicionado configuração para enviar ao salvar

This commit is contained in:
Lucas Miguel
2024-12-03 23:21:56 -03:00
parent 0f7e9a02cf
commit 891609fd52
2 changed files with 43 additions and 16 deletions

View File

@@ -3,6 +3,9 @@ local cli = require("nvim-sync-ftp.cli")
local M = {} local M = {}
function M.setup(opts) function M.setup(opts)
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,{
@@ -18,6 +21,25 @@ function M.setup(opts)
nargs = '*', nargs = '*',
range = true, range = true,
}) })
if next(config) ~= nil then
-- Config for on save
if config.upload_to_save == true then
vim.api.nvim_create_augroup("SyncFtpUploadGroup", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
group = "SyncFtpUploadGroup",
pattern = "*.*",
callback = function(args)
cli.Upload()
end
})
end
end
end end
return M return M

View File

@@ -61,14 +61,17 @@ local function read_file(path)
return lines return lines
end end
local function getConfig() function M.getConfig()
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 i, 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+)$")
config[first_word] = second_word configTemp[first_word] = second_word
end end
else else
message(err); message(err);
@@ -76,11 +79,13 @@ local function getConfig()
else else
message.error("Config file not found!") message.error("Config file not found!")
end end
return configTemp
end end
function M.Upload() function M.Upload()
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
@@ -102,19 +107,19 @@ function M.Upload()
'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1', 'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1',
current_buffer_path, config.host, remotePath, config.user, config.password) current_buffer_path, config.host, remotePath, config.user, config.password)
vim.loop.spawn("sh", { vim.loop.spawn("sh", {
args = { "-c", command }, args = { "-c", command },
}, function(code, signal) }, function(code, signal)
if code == 0 then if code == 0 then
vim.schedule(function() vim.schedule(function()
message.info("File uploaded successfully!") message.info("File uploaded successfully!")
end) end)
else else
vim.schedule(function() vim.schedule(function()
message.error("File upload failed with exit code: " .. code) message.error("File upload failed with exit code: " .. code)
end) end)
end end
end) end)
end end