From 891609fd52a01b1067918546fad16fd2514c3231 Mon Sep 17 00:00:00 2001 From: Lucas Miguel Date: Tue, 3 Dec 2024 23:21:56 -0300 Subject: [PATCH] =?UTF-8?q?Adicionado=20configura=C3=A7=C3=A3o=20para=20en?= =?UTF-8?q?viar=20ao=20salvar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/nvim-sync-ftp.lua | 22 ++++++++++++++++++++++ lua/nvim-sync-ftp/cli.lua | 37 +++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lua/nvim-sync-ftp.lua b/lua/nvim-sync-ftp.lua index 4c9a403..fc61723 100644 --- a/lua/nvim-sync-ftp.lua +++ b/lua/nvim-sync-ftp.lua @@ -3,6 +3,9 @@ local cli = require("nvim-sync-ftp.cli") local M = {} function M.setup(opts) + + local config = cli.getConfig() + vim.api.nvim_create_user_command("SyncFtpMapToRemote", function (params) cli.MapToRemote(params) end,{ @@ -18,6 +21,25 @@ function M.setup(opts) nargs = '*', 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 return M diff --git a/lua/nvim-sync-ftp/cli.lua b/lua/nvim-sync-ftp/cli.lua index 4e426a4..e4f00b4 100644 --- a/lua/nvim-sync-ftp/cli.lua +++ b/lua/nvim-sync-ftp/cli.lua @@ -61,14 +61,17 @@ local function read_file(path) return lines end -local function getConfig() +function M.getConfig() + + local configTemp = {}; + if file_exists() then local content, err = read_file(filePath) if content then for i, line in ipairs(content) do local first_word, second_word = line:match("^(%S+)%s+(%S+)$") - config[first_word] = second_word + configTemp[first_word] = second_word end else message(err); @@ -76,11 +79,13 @@ local function getConfig() else message.error("Config file not found!") end + + return configTemp end function M.Upload() - getConfig() + config = M.getConfig() local current_buffer_path = vim.api.nvim_buf_get_name(0) local directoryTemp = directory @@ -102,19 +107,19 @@ function M.Upload() 'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1', current_buffer_path, config.host, remotePath, config.user, config.password) - 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) + 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