Skip to content

Commit 0a83144

Browse files
shulutkovdekhtyarev
authored andcommitted
added ability to concatenate several environment variables into one line (#73)
* added ability to concatenate several environment variables into one line
1 parent 801d703 commit 0a83144

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ Dockerfile*
66
.gitignore
77
.idea/
88
.tox/
9+
.travis.yml
10+
tox.ini
911
__pychache__
1012
htmlcov/
13+
tests/
14+
*.png
15+

k8s_handle/config.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
log = logging.getLogger(__name__)
1414

1515
INCLUDE_RE = re.compile(r'{{\s?file\s?=\s?\'(?P<file>[^\']*)\'\s?}}')
16-
CUSTOM_ENV_RE = re.compile(r'^(?P<prefix>.*){{\s*env\s*=\s*\'(?P<env>[^\']*)\'\s*}}(?P<postfix>.*)$') # noqa
16+
CUSTOM_ENV_RE = r'{{\s*env\s*=\s*\'([^\']*)\'\s*}}'
1717

1818
KEY_USE_KUBECONFIG = 'use_kubeconfig'
1919
KEY_K8S_MASTER_URI = 'k8s_master_uri'
@@ -113,19 +113,15 @@ def _process_variable(variable):
113113
if matches:
114114
return load_yaml(matches.groupdict().get('file'))
115115

116-
matches = CUSTOM_ENV_RE.match(variable)
116+
try:
117+
return re.sub(CUSTOM_ENV_RE, lambda m: os.environ[m.group(1)], variable)
117118

118-
if matches:
119-
prefix = matches.groupdict().get('prefix')
120-
env_var_name = matches.groupdict().get('env')
121-
postfix = matches.groupdict().get('postfix')
122-
123-
if os.environ.get(env_var_name) is None and settings.GET_ENVIRON_STRICT:
124-
raise RuntimeError('Environment variable "{}" is not set'.format(env_var_name))
125-
126-
return prefix + os.environ.get(env_var_name, '') + postfix
119+
except KeyError as err:
120+
log.debug('Environment variable "{}" is not set'.format(err.args[0]))
121+
if settings.GET_ENVIRON_STRICT:
122+
raise RuntimeError('Environment variable "{}" is not set'.format(err.args[0]))
127123

128-
return variable
124+
return re.sub(CUSTOM_ENV_RE, lambda m: os.environ.get(m.group(1), ''), variable)
129125

130126

131127
def _update_single_variable(value, include_history):

tests/test_config.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,17 @@ def test_context_update_recursion(self):
118118
'section1-key4': [0, 1, 2, 3],
119119
'section1-key5': "{{ env='CUSTOM_ENV' }}",
120120
'section1-key6': "{{ file='tests/fixtures/include.yaml' }}",
121+
'section1-key7': "{{ env='CUSTOM_ENV'}} = {{ env='CUSTOM_ENV' }}",
122+
'section1-key8': "{{ env='NULL_VAR' }}-{{ env='CUSTOM_ENV' }}"
121123
}
122124
},
123125
'section2': [
124126
{},
125127
'var2',
126128
'var3',
127-
'{{ env=\'CUSTOM_ENV\' }}'
129+
'{{ env=\'CUSTOM_ENV\' }}',
130+
'{{ env=\'CUSTOM_ENV\' }} = {{ env=\'CUSTOM_ENV\' }}',
131+
'{{ env=\'NULL_VAR\' }}-{{ env=\'CUSTOM_ENV\' }}'
128132
],
129133
'section3': [0, 1, 2, 3, 4]
130134
}
@@ -137,13 +141,17 @@ def test_context_update_recursion(self):
137141
'section1-key4': [0, 1, 2, 3],
138142
'section1-key5': 'My value',
139143
'section1-key6': {'ha_ha': 'included_var'},
144+
'section1-key7': 'My value = My value',
145+
'section1-key8': "-My value"
140146
}
141147
},
142148
'section2': [
143149
{},
144150
'var2',
145151
'var3',
146-
'My value'
152+
'My value',
153+
'My value = My value',
154+
'-My value'
147155
],
148156
'section3': [0, 1, 2, 3, 4]
149157
}

0 commit comments

Comments
 (0)