|
30 | 30 | -- with the env variable value |
31 | 31 | -- @param str Where replace the placers for the env variables |
32 | 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 |
| 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( |
| 46 | + string.format( |
| 47 | + "Environment variable '%s' was not found.", |
| 48 | + var |
| 49 | + ) |
| 50 | + ) |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + return str |
50 | 55 | end |
51 | 56 |
|
52 | 57 | -- has_key checks if the provided table contains the provided key using a regex |
53 | 58 | -- @param tbl Table to iterate over |
54 | 59 | -- @param key The key to be searched in the table |
55 | 60 | 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 |
| 61 | + for tbl_key, _ in pairs(tbl) do |
| 62 | + if string.find(key, tbl_key) then |
| 63 | + return true |
| 64 | + end |
| 65 | + end |
| 66 | + return false |
62 | 67 | end |
63 | 68 |
|
64 | 69 | -- has_value checks if the provided table contains the provided string using a regex |
|
0 commit comments