Skip to content

Commit 1820306

Browse files
committed
Merge pull request #529 from chmouel/override-environement
Allow overriding environements on command line
2 parents bf1c1b4 + 9224936 commit 1820306

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

fig/cli/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,13 @@ def run(self, project, options):
261261
running. If you do not want to start linked services, use
262262
`fig run --no-deps SERVICE COMMAND [ARGS...]`.
263263
264-
Usage: run [options] SERVICE [COMMAND] [ARGS...]
264+
Usage: run [options] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
265265
266266
Options:
267267
-d Detached mode: Run container in the background, print
268268
new container name.
269269
--entrypoint CMD Override the entrypoint of the image.
270+
-e KEY=VAL Set an environment variable (can be used multiple times)
270271
--no-deps Don't start linked services.
271272
--rm Remove container after run. Ignored in detached mode.
272273
-T Disable pseudo-tty allocation. By default `fig run`
@@ -299,6 +300,13 @@ def run(self, project, options):
299300
'stdin_open': not options['-d'],
300301
}
301302

303+
if options['-e']:
304+
for option in options['-e']:
305+
if 'environment' not in service.options:
306+
service.options['environment'] = {}
307+
k, v = option.split('=', 1)
308+
service.options['environment'][k] = v
309+
302310
if options['--entrypoint']:
303311
container_options['entrypoint'] = options.get('--entrypoint')
304312

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
service:
2+
image: busybox:latest
3+
command: sleep 5
4+
5+
environment:
6+
foo: bar
7+
hello: world

tests/integration/cli_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ def test_run_service_with_entrypoint_overridden(self, _):
206206
u'/bin/echo helloworld'
207207
)
208208

209+
@patch('dockerpty.start')
210+
def test_run_service_with_environement_overridden(self, _):
211+
name = 'service'
212+
self.command.base_dir = 'tests/fixtures/environment-figfile'
213+
self.command.dispatch(
214+
['run', '-e', 'foo=notbar', '-e', 'allo=moto=bobo',
215+
'-e', 'alpha=beta', name],
216+
None
217+
)
218+
service = self.project.get_service(name)
219+
container = service.containers(stopped=True, one_off=True)[0]
220+
# env overriden
221+
self.assertEqual('notbar', container.environment['foo'])
222+
# keep environement from yaml
223+
self.assertEqual('world', container.environment['hello'])
224+
# added option from command line
225+
self.assertEqual('beta', container.environment['alpha'])
226+
# make sure a value with a = don't crash out
227+
self.assertEqual('moto=bobo', container.environment['allo'])
228+
209229
def test_rm(self):
210230
service = self.project.get_service('simple')
211231
service.create_container()

0 commit comments

Comments
 (0)