From 18eacbf3307f8daea3fbe3f81fcf73c6bb8a0d06 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Tue, 15 Oct 2024 09:17:08 +1030 Subject: [PATCH] feat: add nvim command to sync to neto --- .config/nvim/.stylelua.toml | 2 +- .config/nvim/lua/core/commands.lua | 57 ++++++++++++++++++++++++++++++ .config/zsh/.zshrc | 6 ++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/.config/nvim/.stylelua.toml b/.config/nvim/.stylelua.toml index 6ce0431..0aa4d33 100644 --- a/.config/nvim/.stylelua.toml +++ b/.config/nvim/.stylelua.toml @@ -4,4 +4,4 @@ indent_type = "Spaces" indent_width = 2 quote_style = "ForceDouble" no_call_parentheses = false -collapse_simple_statement = 'Always' +collapse_simple_statement = "Always" diff --git a/.config/nvim/lua/core/commands.lua b/.config/nvim/lua/core/commands.lua index b18e81d..ca153e3 100644 --- a/.config/nvim/lua/core/commands.lua +++ b/.config/nvim/lua/core/commands.lua @@ -23,3 +23,60 @@ end, {}) vim.api.nvim_create_user_command("ClearBuffers", function() vim.cmd('%bd|e#|bd#') end, {}) + + +local function load_env_file() + local env_file_path = vim.fn.getcwd() .. "/.env" -- Construct the path relative to the current working directory + local env_file = io.open(env_file_path, "r") -- Open the .env file + + if not env_file then + print("Could not open .env file") + return + end + + for line in env_file:lines() do + local key, value = line:match("^(%S+)=(%S+)") + if key and value then + vim.env[key] = value + end + end + env_file:close() +end + +local function sync_file_to_sftp() + load_env_file() + + -- Get SFTP credentials from environment variables + local sftp_user = os.getenv("SFTP_USER") + local sftp_host = os.getenv("SFTP_HOST") + local sftp_port = os.getenv("SFTP_PORT") + local sftp_password = os.getenv("SFTP_PASSWORD") + local sftp_theme_directory = os.getenv("SFTP_THEME_DIRECTORY") + + local current_file = vim.fn.expand("%:p") -- Get the full path of the current file + + -- Define your project root path + local project_root = vim.fn.getcwd() -- Current working directory where Neovim was launched + local relative_path = current_file:sub(#project_root + 1) + + -- Construct the full remote path + local remote_path = sftp_theme_directory .. relative_path -- Append the relative path to the remote base path + + -- Command to sync the file using lftp + local lftp_cmd = string.format( + "lftp -u %s,%s sftp://%s:%s -e 'put %s -o %s; bye' > /dev/null 2>&1", + sftp_user, + sftp_password, + sftp_host, + sftp_port, + current_file, + remote_path + ) + + -- Execute the command + os.execute(lftp_cmd) + print("File synced to SFTP server: " .. remote_path) +end + +-- Map the function to a command in Neovim +vim.api.nvim_create_user_command("SyncToNeto", sync_file_to_sftp, {}) diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index da1b779..f06f92e 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -112,5 +112,11 @@ if [ -f "$HOME/.cargo/env" ]; then . "$HOME/.cargo/env" fi +if type "pyenv" > /dev/null; then + export PYENV_ROOT="$HOME/.pyenv" + [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH" + eval "$(pyenv init -)" +fi + # Speed debugging # zprof