|
1 | 1 | local M = {} |
2 | 2 |
|
| 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 | + |
3 | 64 | -- has_value checks if the provided table contains the provided string using a regex |
4 | 65 | -- @param tbl Table to iterate over |
5 | 66 | -- @param str String to search in the table |
@@ -49,15 +110,26 @@ M.tbl_to_str = function(tbl, json) |
49 | 110 | end |
50 | 111 |
|
51 | 112 | -- 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) |
53 | 117 | if sep == nil then |
54 | 118 | sep = '%s' |
55 | 119 | end |
| 120 | + max_splits = max_splits or -1 |
56 | 121 |
|
57 | 122 | 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 |
60 | 131 | end |
| 132 | + str_tbl[nField] = str:sub(nStart) |
61 | 133 |
|
62 | 134 | return str_tbl |
63 | 135 | end |
|
0 commit comments