This repository was archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathenv.m
171 lines (143 loc) · 4.81 KB
/
env.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function value = env(key, default, useAutoEnvrep)
% ENV - read environment variables
%
% Reads values from three sources in the following order:
% 1) System environment (getenv)
% 2) MATLAB preferences (getpref) in the 'env' group
% 3) ".env" file on the path
%
% Examples:
% dbHost = env('DATABASE_HOST', '127.0.0.1');
% s3Pass = env('S3_PASSWORD');
% env PATH
%
% See also: getenv, getpref
% Copyright 2021 Florian Schwaiger <[email protected]>
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
% associated documentation files (the "Software"), to deal in the Software without restriction,
% including without limitation the rights to use, copy, modify, merge, publish, distribute,
% sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
% is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies
% or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
% BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if nargin > 2 && not(useAutoEnvrep)
eval("envrep = @(x) x;");
end
narginchk(0, 2);
if nargin == 0
value = readfrominifile("*", pwd);
value = structfun(@envrep, value, "Uniform", false);
return
end
value = envrep(readfromsystemenv(key));
if not(strcmp(value, ""))
return
end
value = envrep(readfrommatlabpref(key));
if not(strcmp(value, ""))
return
end
value = envrep(readfrominifile(key, pwd));
if not(strcmp(value, ""))
return
end
if nargout > 0 && nargin > 1
if isstring(key)
value = string(default);
else
value = default;
end
elseif nargout > 0
error('env:missing', ...
'Environment value "%s" undefined, no default given.', key);
end
end
function value = readfromsystemenv(key)
% try to read from system env variables
value = getenv(key);
if isstring(key)
value = string(value);
end
end
function value = readfrommatlabpref(key)
% read from MATLAB preferences "env" group
if ispref('env', key)
value = getpref('env', key);
elseif isstring(key)
value = "";
else
value = '';
end
end
function value = readfrominifile(key, folder)
% get the value from an ".env" file
value = '';
envFile = fullfile(folder, 'keys.env');
if not(exist(envFile, 'file'))
parent = fileparts(folder);
if strcmp(parent, folder)
value = readfrominifile(key, parent);
elseif strcmp(key, "*")
value = struct();
end
return
end
modified = lastmodified(envFile);
persistent envCache
if isempty(envCache) || envCache.modified < modified
envCache = struct( ...
'modified', modified, ...
'content', loadini(envFile) ...
);
end
if strcmp(key, "*")
value = envCache.content;
elseif isfield(envCache.content, key)
value = envCache.content.(key);
end
if isstring(key) && isstruct(value)
value = structfun(@string, value, 'UniformOutput', false);
elseif isstring(key)
value = string(value);
end
end
function modified = lastmodified(filename)
% finds out when a file was modified last
info = dir(filename);
modified = info(1).datenum;
end
function value = loadini(filename)
% read and parse a file in KEY=VALUE format
lines = strsplit(fileread(filename), newline);
value = struct();
for iLine = 1:numel(lines)
line = strtrim(lines{iLine});
if isempty(line) || any(line(1) == '#;[')
% ignore comments and section headers
else
value = appendlinevalue(value, line);
end
end
end
function parent = appendlinevalue(parent, line)
[key, valueWithEqualsChar] = strtok(line, '=');
key = strtrim(key);
text = strtrim(valueWithEqualsChar(2:end));
if isempty(text) || any(text(1) == '#;')
% ignore unset value and comments
elseif any(text(1) == '"''')
% extract until the closing quote character
parent.(key) = extractBefore(text(2:end), regexpPattern("(?<!\\)" + text(1)));
else
% apply
parent.(key) = text;
end
end