Skip to content

Commit

Permalink
feat: config option for custom environment variables file (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrserafin committed Nov 16, 2021
1 parent 759bf5b commit 2542929
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use {
},
-- Jump to request line on run
jump_to_request = false,
env_file = '.env'
})
end
}
Expand Down Expand Up @@ -105,12 +106,13 @@ To run `rest.nvim` you should map the following commands:

## Settings

- `result_split_horizontal` opens result on a horizontal split (default opens
- `result_split_horizontal` opens result on a horizontal split (default opens
on vertical)
- `skip_ssl_verification` passes the `-k` flag to cURL in order to skip SSL verification,
useful when using unknown certificates
- `highlight` allows to enable and configure the highlighting of the selected request when send,
- `jump_to_request` moves the cursor to the selected request line when send,
- `env_file` specifies file name that consist environment variables (default: .env)

## Usage

Expand Down
3 changes: 2 additions & 1 deletion doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function, it looks like this by default:
` },`
` -- Jump to request line on run`
` jump_to_request = false,`
` env_file = '.env'`
`})`

In this section we will be using `https://reqres.in/` for requests.
Expand Down Expand Up @@ -144,8 +145,8 @@ ENVIRONMENT VARIABLES *rest-nvim-usage-environment-variables*
To use environment variables, the following syntax is used: `{{VARIABLE_NAME}}`

These environment variables can be obtained from:
- File in the current working directory (env_file in config or '.env')
- System
- `.env` file in the current working directory


===============================================================================
Expand Down
2 changes: 2 additions & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ rest-nvim-license rest-nvim.txt /*rest-nvim-license*
rest-nvim-quick-start rest-nvim.txt /*rest-nvim-quick-start*
rest-nvim-usage rest-nvim.txt /*rest-nvim-usage*
rest-nvim-usage-commands rest-nvim.txt /*rest-nvim-usage-commands*
rest-nvim-usage-dynamic-variables rest-nvim.txt /*rest-nvim-usage-dynamic-variables*
rest-nvim-usage-environment-variables rest-nvim.txt /*rest-nvim-usage-environment-variables*
rest-nvim-usage-external-files rest-nvim.txt /*rest-nvim-usage-external-files*
rest-nvim-usage-requests rest-nvim.txt /*rest-nvim-usage-requests*
rest-nvim.txt rest-nvim.txt /*rest-nvim.txt*
1 change: 1 addition & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local config = {
show_headers = true,
},
jump_to_request = false,
env_file = ".env",
}

--- Get a configuration value
Expand Down
12 changes: 8 additions & 4 deletions lua/rest-nvim/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local config = require("rest-nvim.config")

local random = math.random
math.randomseed(os.time())

Expand Down Expand Up @@ -45,17 +47,19 @@ M.read_file = function(file)
return lines
end

-- read_env_file Reads the environment variables found in the `.env` file and
-- returns a table with the variables
-- read_env_file Reads the environment variables found in the env_file option
-- (defualt: .env) specified in configuration and returns a table with the
-- variables
M.read_env_file = function()
local variables = {}
local env_file = "/" .. (config.get("env_file") or ".env")

-- Directories to search for env files
local env_file_paths = {
-- current working directory
vim.fn.getcwd() .. "/.env",
vim.fn.getcwd() .. env_file,
-- directory of the currently opened file
vim.fn.expand("%:p:h") .. "/.env",
vim.fn.expand("%:p:h") .. env_file,
}

-- If there's an env file in the current working dir
Expand Down

0 comments on commit 2542929

Please sign in to comment.