Skip to content

Commit

Permalink
CLI - allow configuration of environment variables during nv create
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-muir committed May 26, 2017
1 parent 3d5d28c commit 4f1134a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions nv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ def main():
help='''Activate a python virtualenv via pew''')
@click.option('--aws-profile', default=None,
help='''Obtain credentials for the given profile.''')
def cmd_create(environment_name, project_name, project_dir, use_pew, aws_profile):
@click.option('environment_vars', '--env', type=(unicode, unicode), multiple=True)
def cmd_create(environment_name, project_name, project_dir, use_pew, aws_profile, environment_vars):
"""Create a new environment in %PROJECT%/.nv-%ENVIRONMENT_NAME%"""
nv_dir = create(environment_name, project_dir, project_name=project_name, use_pew=use_pew, aws_profile=aws_profile)
nv_dir = create(environment_name, project_dir, project_name=project_name, use_pew=use_pew, aws_profile=aws_profile,
environment_vars=dict(environment_vars))
rel_dir = os.path.relpath(nv_dir, os.getcwd())
click.echo("""
environment created at {0}.
Expand Down
13 changes: 12 additions & 1 deletion nv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@
logger = logging.getLogger(__name__)


def create(environment_name, project_dir, project_name=None, use_pew=False, aws_profile=None):
def create(environment_name, project_dir, project_name=None, use_pew=False, aws_profile=None, environment_vars=None):
# TODO check that environment_name contains only [a-z0-9_]
# TODO check that project_dir is fully resolved

nv_dir = join(project_dir, '.nv-{0}'.format(environment_name))
if exists(nv_dir):
raise RuntimeError("Environment exists at '{0}'".format(nv_dir))

if environment_vars:
if not isinstance(environment_vars, dict):
raise RuntimeError('Environment: Expected dict got {0}'.format(type(environment_vars)))
for k, v in environment_vars.items():
if not isinstance(v, six.string_types):
raise RuntimeError('Environment "{0}" expected str got {1}'.format(k, type(v)))

if not project_name:
project_name = basename(project_dir)

Expand All @@ -44,6 +51,10 @@ def create(environment_name, project_dir, project_name=None, use_pew=False, aws_
with open(join(nv_dir, 'nv.json'), 'wb') as fp:
json.dump(nv_conf, fp, indent=2)

if environment_vars:
with open(join(nv_dir, 'environment.json'), 'wb') as fp:
json.dump(environment_vars, fp, indent=2)

return nv_dir


Expand Down

0 comments on commit 4f1134a

Please sign in to comment.