Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118---@mode rest-nvim.dotenv dotenv management module
local M = {}
local dotenv_parser = require("rest-nvim.parser.dotenv")local config = require("rest-nvim.config")local logger = require("rest-nvim.logger")
---load dotenv file---This function will set environment variables in current editor session.---@see vim.env---@param path string file path of dotenv file---@param setter? fun(key:string, value:string)function M.load_file(path, setter) vim.validate({ path = { path, "string" }, settter = { setter, { "function", "nil" } }, }) if not setter then setter = function(key, value) vim.env[key] = value end end local ok = dotenv_parser.parse(path, setter) if not ok then vim.notify("failed to load file '" .. path .. "'", vim.log.levels.WARN, { title = "rest.nvim" }) endend
---register the dotenv file.---this file will be sourced right before each requests (it won't be sourced---multiple times when running all requests in current http file)---@param path string file path of dotenv file---@param bufnr number? buffer identifier, default to current bufferfunction M.register_file(path, bufnr) bufnr = bufnr or 0 vim.b[bufnr]._rest_nvim_env_file = path vim.notify("Env file '" .. path .. "' has been registered", vim.log.levels.INFO, { title = "rest.nvim" })end
---show registered dotenv file for current buffer---@param bufnr number? buffer identifier, default to current bufferfunction M.show_registered_file(bufnr) bufnr = bufnr or 0 if not vim.b[bufnr]._rest_nvim_env_file then vim.notify("No env file is used in current buffer", vim.log.levels.WARN, { title = "rest.nvim" }) else vim.notify( "Current env file in use: " .. vim.b._rest_nvim_env_file, vim.log.levels.INFO, { title = "rest.nvim" } ) endend
---Find a list of environment files---@see rest.Opts.Env.find---@return string[] files Environment variable files pathfunction M.find_env_files() return config.env.find()end
---@return string? dotenv filefunction M.find_relevent_env_file() local filename = vim.fn.expand("%:t:r") if filename == "" then return nil end filename = filename:gsub("%.http$", "") ---@type string? local env_file -- search for `/same/path/filename.env` logger.debug("searching for " .. filename .. ".env file") env_file = vim.fs.find(filename .. ".env", { path = vim.fn.expand("%:h"), upward = true, stop = vim.fn.getcwd(), type = "file", limit = math.huge, })[1] if env_file then logger.debug("found .env file:", env_file) return env_file end -- search upward for `.env` file logger.debug("searching for .env file") env_file = vim.fs.find(function(name, _) return name == ".env" end, { path = vim.fn.expand("%:h"), upward = true, stop = vim.fn.getcwd(), type = "file", limit = math.huge, })[1] if env_file then logger.debug("found .env file:", env_file) return env_file else logger.debug("no .env file found") endend
---@param bufnr number? buffer identifier, default to current buffer---@return string? pathfunction M.select_file(bufnr) bufnr = bufnr or vim.api.nvim_get_current_buf() vim.ui.select(M.find_env_files(), { prompt = "Select env files", }, function(item, _idx) if item then M.register_file(item, bufnr) end end)end
return M