From 260ff045defa582340c3cce77f0a394c6e072781 Mon Sep 17 00:00:00 2001 From: Lucas Miguel Date: Mon, 2 Dec 2024 00:56:15 -0300 Subject: [PATCH] Creating file config and read --- .gitignore | 1 + README.md | 1 + lua/nvim-sync-ftp.lua | 23 +++++++++ lua/nvim-sync-ftp/cli.lua | 88 +++++++++++++++++++++++++++++++++++ lua/nvim-sync-ftp/message.lua | 24 ++++++++++ 5 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 lua/nvim-sync-ftp.lua create mode 100644 lua/nvim-sync-ftp/cli.lua create mode 100644 lua/nvim-sync-ftp/message.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1273927 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.sync-ftp-config diff --git a/README.md b/README.md new file mode 100644 index 0000000..d716e61 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Plugins for syncronization in FTP servers # diff --git a/lua/nvim-sync-ftp.lua b/lua/nvim-sync-ftp.lua new file mode 100644 index 0000000..4c9a403 --- /dev/null +++ b/lua/nvim-sync-ftp.lua @@ -0,0 +1,23 @@ +local cli = require("nvim-sync-ftp.cli") + +local M = {} + +function M.setup(opts) + vim.api.nvim_create_user_command("SyncFtpMapToRemote", function (params) + cli.MapToRemote(params) + end,{ + force = true, + nargs = '*', + range = true, + }) + + vim.api.nvim_create_user_command("SyncFtpUpload", function(params) + cli.Upload() + end,{ + force = true, + nargs = '*', + range = true, + }) +end + +return M diff --git a/lua/nvim-sync-ftp/cli.lua b/lua/nvim-sync-ftp/cli.lua new file mode 100644 index 0000000..b0c6130 --- /dev/null +++ b/lua/nvim-sync-ftp/cli.lua @@ -0,0 +1,88 @@ +local message = require("nvim-sync-ftp.message") +local directory = vim.fn.getcwd(); +local filePath = directory .. "/.sync-ftp-config" +local config = {} + +local M = {} + +local function file_exists() + return vim.loop.fs_stat(filePath) ~= nil +end + +local function create_file() + + local buf = vim.api.nvim_create_buf(true, true) + + vim.api.nvim_buf_set_name(buf, filePath) + + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { + "host hostName" , + "user userName", + "password password", + "port 21", + "remote_path /remote/", + "upload_to_save false", + }) + + vim.api.nvim_buf_set_option(buf, "buftype", "") + + vim.api.nvim_buf_call(buf, function() + vim.cmd("write") + end) + + vim.api.nvim_win_set_buf(0, buf) + + message.success("File created successfully!!!") + +end + +function M.MapToRemote () + + if file_exists() then + message.warn("File already exists!!!"); + else + create_file() + end + +end + +local function read_file(path) + local file = io.open(path, "r") + + if not file then + return nil, "Config file not found!" + end + + local lines = {} + for line in file:lines() do + table.insert(lines, line) + end + file:close() + return lines +end + +local function getConfig() + 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 + end + else + message(err); + end + else + message.error("Config file not found!") + end +end + +function M.Upload() + getConfig() + + + +end + +return M diff --git a/lua/nvim-sync-ftp/message.lua b/lua/nvim-sync-ftp/message.lua new file mode 100644 index 0000000..be8a5dd --- /dev/null +++ b/lua/nvim-sync-ftp/message.lua @@ -0,0 +1,24 @@ +local levels = vim.log.levels + +local M = {} + +--- @type fun(fmt: string, ...: string) +M.warn = vim.schedule_wrap(function(fmt, ...) + vim.notify(fmt:format(...), levels.WARN, { 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' }) +end) + +--- @type fun(fmt: string, ...: string) +M.error_once = vim.schedule_wrap(function(fmt, ...) + vim.notify_once(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' }) +end) + +M.success = vim.schedule_wrap(function(fmt, ...) + vim.notify_once(fmt:format(...), vim.log.levels.SUCCESS, { title = 'Sync FTP'}) +end) + +return M