From 0f7e9a02cfc391fe5bdbd3e53ebfe6f46341c7e5 Mon Sep 17 00:00:00 2001 From: Lucas Miguel Date: Tue, 3 Dec 2024 22:49:40 -0300 Subject: [PATCH] Ajustado para subir arquivos --- lua/nvim-sync-ftp/cli.lua | 52 ++++++++++++++--------------------- lua/nvim-sync-ftp/message.lua | 7 ++++- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/lua/nvim-sync-ftp/cli.lua b/lua/nvim-sync-ftp/cli.lua index 6dca4a9..4e426a4 100644 --- a/lua/nvim-sync-ftp/cli.lua +++ b/lua/nvim-sync-ftp/cli.lua @@ -79,54 +79,42 @@ local function getConfig() end function M.Upload() + getConfig() local current_buffer_path = vim.api.nvim_buf_get_name(0) local directoryTemp = directory - - -- message.warn("Send file ... Wait") + + message.warn("Send file ... 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 ftp_script = string.format([[ - open %s - user %s %s - put %s %s - bye - ]], config.host, config.user, config.password, current_buffer_path, remotePath) - - local script_file = "ftp_commands.txt" - local file = io.open(script_file, "w") - file:write(ftp_script) - file:close() + local command = string.format( + 'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1', + current_buffer_path, config.host, remotePath, config.user, config.password) - local log_file = "/tmp/ftp_log.txt" - - local exit_code = os.execute(string.format("ftp -n < %s > %s 2>&1", script_file, log_file)) - - - local log = io.open(log_file, "r") - local log_content = log:read("*a") - log:close() - - if log_content:match("Error") or log_content:match("fail") then - message.error("Fail to upload file!" .. log_content) - else - message.success("File send successfully!") - end - - os.remove(script_file) - os.remove(log_file) + vim.loop.spawn("sh", { + args = { "-c", command }, + }, function(code, signal) + if code == 0 then + vim.schedule(function() + message.info("File uploaded successfully!") + end) + else + vim.schedule(function() + message.error("File upload failed with exit code: " .. code) + end) + end + end) end diff --git a/lua/nvim-sync-ftp/message.lua b/lua/nvim-sync-ftp/message.lua index d36730f..d00a515 100644 --- a/lua/nvim-sync-ftp/message.lua +++ b/lua/nvim-sync-ftp/message.lua @@ -7,6 +7,11 @@ M.warn = vim.schedule_wrap(function(fmt, ...) vim.notify(fmt:format(...), levels.WARN, { title = 'Sync FTP' }) end) +--- @type fun(fmt: string, ...: string) +M.info = vim.schedule_wrap(function(fmt, ...) + vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP' }) +end) + --- @type fun(fmt: string, ...: string) M.error = vim.schedule_wrap(function(fmt, ...) vim.notify(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' }) @@ -18,7 +23,7 @@ M.error_once = vim.schedule_wrap(function(fmt, ...) end) M.success = vim.schedule_wrap(function(fmt, ...) - vim.notify(fmt:format(...), levels.SUCCESS, { title = 'Sync FTP'}) + vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP'}) end) return M