Skip to content

Commit 5d71932

Browse files
committed
Merge branch 'feature/env-vars'
2 parents 72b61e0 + 1e9d6bd commit 5d71932

File tree

7 files changed

+129
-14
lines changed

7 files changed

+129
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ have to leave Neovim!
2727
- Fast execution time
2828
- Run request under cursor
2929
- Syntax highlight for http files and output
30+
- Possibility of using environment variables in http files
3031

3132
# Install
3233

doc/rest-nvim.txt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
===============================================================================
1414
CONTENTS *rest-nvim-contents*
1515
1. Introduction...........................................|rest-nvim-intro|
16-
2. Quick start......................................|rest-nvim-quick-start|
17-
3. Usage..................................................|rest-nvim-usage|
16+
2. Features............................................|rest-nvim-features|
17+
3. Quick start......................................|rest-nvim-quick-start|
18+
4. Usage..................................................|rest-nvim-usage|
1819
1. Commands..................................|rest-nvim-usage-commands|
19-
1. Requests..................................|rest-nvim-usage-requests|
20-
2. Fields......................................|rest-nvim-usage-fields|
21-
4. Known issues..........................................|rest-nvim-issues|
22-
5. License..............................................|rest-nvim-license|
23-
6. Contributing....................................|rest-nvim-contributing|
20+
2. Requests..................................|rest-nvim-usage-requests|
21+
3. Environment Variables........|rest-nvim-usage-environment-variables|
22+
5. Known issues..........................................|rest-nvim-issues|
23+
6. License..............................................|rest-nvim-license|
24+
7. Contributing....................................|rest-nvim-contributing|
2425

2526

2627
===============================================================================
@@ -32,6 +33,16 @@ plugin `plenary.nvim` so, in other words, `rest.nvim` is a curl wrapper so you
3233
don't have to leave Neovim!
3334

3435

36+
===============================================================================
37+
FEATURES *rest-nvim-features*
38+
39+
- Easy to use
40+
- Fast execution time
41+
- Run request under cursor
42+
- Syntax highlight for http files and output
43+
- Possibility of using environment variables in http files
44+
45+
3546
===============================================================================
3647
QUICK START *rest-nvim-quick-start*
3748

@@ -94,6 +105,18 @@ Currently `rest.nvim` supports the following request methods:
94105
- PUT
95106

96107

108+
===============================================================================
109+
ENVIRONMENT VARIABLES *rest-nvim-usage-environment-variables*
110+
111+
`rest.nvim` allows the use of environment variables in requests.
112+
113+
To use environment variables, the following syntax is used: `{{VARIABLE_NAME}}`
114+
115+
These environment variables can be obtained from:
116+
- System
117+
- `.env` file in the current working directory
118+
119+
97120
===============================================================================
98121
KNOWN ISSUES *rest-nvim-issues*
99122

doc/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
rest-nvim rest-nvim.txt /*rest-nvim*
22
rest-nvim-contents rest-nvim.txt /*rest-nvim-contents*
33
rest-nvim-contributing rest-nvim.txt /*rest-nvim-contributing*
4+
rest-nvim-features rest-nvim.txt /*rest-nvim-features*
45
rest-nvim-intro rest-nvim.txt /*rest-nvim-intro*
56
rest-nvim-issues rest-nvim.txt /*rest-nvim-issues*
67
rest-nvim-license rest-nvim.txt /*rest-nvim-license*
78
rest-nvim-quick-start rest-nvim.txt /*rest-nvim-quick-start*
89
rest-nvim-usage rest-nvim.txt /*rest-nvim-usage*
910
rest-nvim-usage-commands rest-nvim.txt /*rest-nvim-usage-commands*
11+
rest-nvim-usage-environment-variables rest-nvim.txt /*rest-nvim-usage-environment-variables*
1012
rest-nvim-usage-requests rest-nvim.txt /*rest-nvim-usage-requests*
1113
rest-nvim.txt rest-nvim.txt /*rest-nvim.txt*

lua/rest-nvim/init.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ local function parse_url(stmt)
4444
return {
4545
method = parsed[1],
4646
-- Encode URL
47-
url = utils.encode_url(parsed[2]),
47+
url = utils.encode_url(utils.replace_env_vars(parsed[2])),
4848
}
4949
end
5050

@@ -81,7 +81,7 @@ local function get_body(bufnr, stop_line, query_line, json_body)
8181
api.nvim_buf_get_lines(bufnr, start_line, end_line - 1, false)
8282

8383
for _, v in ipairs(json_lines) do
84-
json_string = json_string .. v
84+
json_string = json_string .. utils.replace_env_vars(v)
8585
end
8686

8787
json_string = '{' .. json_string .. '}'
@@ -201,8 +201,7 @@ local function get_auth(bufnr, query_line)
201201
-- Split by spaces, e.g. {'Authorization:', 'user:pass'}
202202
auth_data = utils.split(auth_data, '%s+')
203203
-- {'user', 'pass'}
204-
auth = utils.split(auth_data[2], ':')
205-
auth_not_empty = true
204+
auth = utils.split(utils.replace_env_vars(auth_data[2]), ':')
206205
end
207206
end
208207

lua/rest-nvim/utils/init.lua

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
local M = {}
22

3+
-- file_exists checks if the provided file exists and returns a boolean
4+
-- @param file File to check
5+
M.file_exists = function(file)
6+
file = io.open(file, 'rb')
7+
if file then
8+
file:close()
9+
end
10+
return file ~= nil
11+
end
12+
13+
-- read_env_file Reads the environment variables found in the `.env` file and
14+
-- returns a table with the variables
15+
M.read_env_file = function()
16+
local variables = {}
17+
local env_file_path = vim.fn.getcwd() .. '/.env'
18+
-- If there's an env file in the current working dir
19+
if M.file_exists(env_file_path) then
20+
for line in io.lines(env_file_path) do
21+
local vars = M.split(line, '=', 1)
22+
variables[vars[1]] = vars[2]
23+
end
24+
end
25+
26+
return variables
27+
end
28+
29+
-- replace_env_vars replaces the env variables fields in the provided string
30+
-- with the env variable value
31+
-- @param str Where replace the placers for the env variables
32+
M.replace_env_vars = function(str)
33+
local env_vars = M.read_env_file()
34+
35+
for var in string.gmatch(str, '{{%w+}}') do
36+
var = var:gsub('{', ''):gsub('}', '')
37+
-- If the env variable wasn't found in the `.env` file then search it
38+
-- in the OS environment variables
39+
if M.has_key(env_vars, var) then
40+
str = str:gsub('{{' .. var .. '}}', env_vars[var])
41+
else
42+
if os.getenv(var) ~= nil then
43+
str = str:gsub('{{' .. var .. '}}', os.getenv(var))
44+
else
45+
error(string.format("Environment variable '%s' was not found.", var))
46+
end
47+
end
48+
end
49+
return str
50+
end
51+
52+
-- has_key checks if the provided table contains the provided key using a regex
53+
-- @param tbl Table to iterate over
54+
-- @param key The key to be searched in the table
55+
M.has_key = function(tbl, key)
56+
for tbl_key, _ in pairs(tbl) do
57+
if string.find(key, tbl_key) then
58+
return true
59+
end
60+
end
61+
return false
62+
end
63+
364
-- has_value checks if the provided table contains the provided string using a regex
465
-- @param tbl Table to iterate over
566
-- @param str String to search in the table
@@ -49,15 +110,26 @@ M.tbl_to_str = function(tbl, json)
49110
end
50111

51112
-- Just a split function because Lua does not have this, nothing more
52-
M.split = function(str, sep)
113+
-- @param str String to split
114+
-- @param sep Separator
115+
-- @param max_splits Number of times to split the string (optional)
116+
M.split = function(str, sep, max_splits)
53117
if sep == nil then
54118
sep = '%s'
55119
end
120+
max_splits = max_splits or -1
56121

57122
local str_tbl = {}
58-
for match in string.gmatch(str, '([^' .. sep .. ']+)') do
59-
table.insert(str_tbl, match)
123+
local nField, nStart = 1, 1
124+
local nFirst, nLast = str:find(sep, nStart)
125+
while nFirst and max_splits ~= 0 do
126+
str_tbl[nField] = str:sub(nStart, nFirst - 1)
127+
nField = nField + 1
128+
nStart = nLast + 1
129+
nFirst, nLast = str:find(sep, nStart)
130+
max_splits = max_splits - 1
60131
end
132+
str_tbl[nField] = str:sub(nStart)
61133

62134
return str_tbl
63135
end

tests/using_env_vars/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
URL=https://reqres.in/api/users
2+
USERNAME=morpheus
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Environment variables are readed from an `.env` file in the current working
2+
# directory and if they are not found, they will fallback to be searched in
3+
# the system environment variables.
4+
#
5+
# Environment variables placeholders can be used in the following fields:
6+
# - URL
7+
# - Authorization
8+
# - Body
9+
POST {{URL}}
10+
11+
# Authorization: {{USER}}:{{PASSWORD}}
12+
13+
{
14+
"name": "{{USERNAME}}",
15+
"job": "leader"
16+
}

0 commit comments

Comments
 (0)