Skip to content

Commit

Permalink
add --skip-env-file option
Browse files Browse the repository at this point in the history
Fixes #6741
Updates #6511
  • Loading branch information
hirochachacha committed Aug 14, 2019
1 parent cf3c07d commit b70e7ae
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
16 changes: 12 additions & 4 deletions compose/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@

def project_from_options(project_dir, options, additional_options={}):
override_dir = options.get('--project-directory')
environment_file = options.get('--env-file')
environment = Environment.from_env_file(override_dir or project_dir, environment_file)
skip_environment_file = options.get('--skip-env-file')
if skip_environment_file:
environment = Environment.from_nothing()
else:
environment_file = options.get('--env-file')
environment = Environment.from_env_file(override_dir or project_dir, environment_file)
environment.silent = options.get('COMMAND', None) in SILENT_COMMANDS
set_parallel_limit(environment)

Expand Down Expand Up @@ -79,8 +83,12 @@ def set_parallel_limit(environment):

def get_config_from_options(base_dir, options, additional_options={}):
override_dir = options.get('--project-directory')
environment_file = options.get('--env-file')
environment = Environment.from_env_file(override_dir or base_dir, environment_file)
skip_environment_file = options.get('--skip-env-file')
if skip_environment_file:
environment = Environment.from_nothing()
else:
environment_file = options.get('--env-file')
environment = Environment.from_env_file(override_dir or base_dir, environment_file)
config_path = get_config_path_from_options(
base_dir, options, environment
)
Expand Down
10 changes: 8 additions & 2 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class TopLevelCommand(object):
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent
--env-file PATH Specify an alternate environment file
--skip-env-file Skip loading an environment file
Commands:
build Build or rebuild services
Expand Down Expand Up @@ -249,8 +250,13 @@ def project_dir(self):

@property
def toplevel_environment(self):
environment_file = self.toplevel_options.get('--env-file')
return Environment.from_env_file(self.project_dir, environment_file)
skip_environment_file = self.toplevel_options.get('--skip-env-file')
if skip_environment_file:
environment = Environment.from_nothing()
else:
environment_file = self.toplevel_options.get('--env-file')
environment = Environment.from_env_file(self.project_dir, environment_file)
return environment

def build(self, options):
"""
Expand Down
6 changes: 6 additions & 0 deletions compose/config/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def __init__(self, *args, **kwargs):
self.missing_keys = []
self.silent = False

@classmethod
def from_nothing(cls):
instance = cls()
instance.update(os.environ)
return instance

@classmethod
def from_env_file(cls, base_dir, env_file=None):
def _initialize():
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/env-file-skip/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VAR2=VAL2
9 changes: 9 additions & 0 deletions tests/fixtures/env-file-skip/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'
services:
show_env:
image: alpine:latest
environment:
VAR1: VAL1
VAR2: $VAR2
command:
env
13 changes: 13 additions & 0 deletions tests/integration/environment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ def test_env_file_override(self):
assert "WHEREAMI=override" in containers[0].get('Config.Env')
assert "DEFAULT_CONF_LOADED=true" in containers[0].get('Config.Env')
dispatch(base_dir, ['--env-file', '.env.override', 'down'], None)


class EnvironmentSkipFileTest(DockerClientTestCase):
def test_env_file_skip(self):
base_dir = 'tests/fixtures/env-file-skip'

result = dispatch(base_dir, ['up'])
assert "VAR1=VAL1" in result.stdout
assert "VAR2=VAL2" in result.stdout

result = dispatch(base_dir, ['--skip-env-file', 'up'])
assert "VAR1=VAL1" in result.stdout
assert "VAR2=VAL2" not in result.stdout

0 comments on commit b70e7ae

Please sign in to comment.