Skip to content

Commit

Permalink
Merge pull request #46 from andreiboyanov/get_env_varaibles_from_file
Browse files Browse the repository at this point in the history
[IMP] Get env variables from file
  • Loading branch information
sbidoul committed Aug 28, 2020
2 parents 055980a + 91f3257 commit e303e9b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ Expand environment variables inside of the configuration file when loading:
$ gitaggregate -c repos.yaml --expand-env
The variable in the configuration file can be specified in one of the following ways:

- $VARIABLE
- ${VARIABLE}

For mor info, see the Python's string.Template documentation.

Use additional variables from file while expanding:

.. code-block:: bash
$ gitaggregate -c repos.yaml --expand-env --env-file .env
The env file should contain `VAR=value` lines. Lines starting with # are ignored.

You can also aggregate and automatically push the result to the target:

.. code-block:: bash
Expand Down
19 changes: 16 additions & 3 deletions git_aggregator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ def get_repos(config, force=False):
return repo_list


def load_config(config, expand_env=False, force=False):
def load_config(config, expand_env=False, env_file=None, force=False):
"""Return repos from a directory and fnmatch. Not recursive.
:param config: paths to config file
:type config: str
:param expand_env: True to expand environment varialbes in the config.
:param expand_env: True to expand environment variables in the config.
:type expand_env: bool
:param env_file: path to file with variables to add to the environment.
:type env_file: str or None
:param bool force: True to aggregate even if repo is dirty.
:returns: expanded config dict item
:rtype: iter(dict)
Expand All @@ -141,9 +143,20 @@ def load_config(config, expand_env=False, force=False):
conf = kaptan.Kaptan(handler=kaptan.HANDLER_EXT.get(file_extension))

if expand_env:
environment = {}
if env_file is not None and os.path.isfile(env_file):
with open(env_file) as env_file_handler:
for line in env_file_handler:
line = line.strip()
if line.startswith('#'):
continue
if '=' in line:
key, value = line.split('=')
environment.update({key.strip(): value.strip()})
environment.update(os.environ)
with open(config, 'r') as file_handler:
config = Template(file_handler.read())
config = config.substitute(os.environ)
config = config.substitute(environment)

conf.import_config(config)
return get_repos(conf.export('dict') or {}, force)
11 changes: 9 additions & 2 deletions git_aggregator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def get_parser():
action='store_true',
help='Expand environment variables in configuration file',
)
main_parser.add_argument(
'--env-file',
dest='env_file',
default=None,
help='Path to file with variables to be added to the environment',
)
main_parser.add_argument(
'-f', '--force',
dest='force',
Expand Down Expand Up @@ -183,7 +189,7 @@ def match_dir(cwd, dirmatch=None):
def load_aggregate(args):
"""Load YAML and JSON configs and begin creating / updating , aggregating
and pushing the repos (deprecated in favor or run())"""
repos = load_config(args.config, args.expand_env)
repos = load_config(args.config, args.expand_env, args.env_file)
dirmatch = args.dirmatch
for repo_dict in repos:
r = Repo(**repo_dict)
Expand Down Expand Up @@ -227,7 +233,8 @@ def run(args):
"""Load YAML and JSON configs and run the command specified
in args.command"""

repos = load_config(args.config, args.expand_env, args.force)
repos = load_config(
args.config, args.expand_env, args.env_file, args.force)

jobs = max(args.jobs, 1)
threads = []
Expand Down
38 changes: 38 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,44 @@ def test_import_config_expand_env(self):
remotes[0]['url'], os.environ['TEST_REPO']
)

def test_import_config_expand_env_from_file(self):
""" It should expand the config with variables form file. """
os.environ['TEST_REPO'] = 'https://github.com/test/test.git'
data_yaml = """
${TEST_FOLDER}:
remotes:
oca: $TEST_REPO
merges:
- oca 8.0
target: oca aggregated_branch_name
"""
vars_env = """
# comment
TEST_REPO=to be overridden by environ
TEST_FOLDER = /test
"""

_, env_path = tempfile.mkstemp(suffix='.env')
try:
with open(env_path, 'w') as env_file:
env_file.write(vars_env)
_, config_path = tempfile.mkstemp(suffix='.yaml')
with open(config_path, 'w') as config_file:
config_file.write(data_yaml)
repos = config.load_config(config_file.name, True, env_file.name)
finally:
if os.path.exists(env_path):
os.remove(env_path)
if os.path.exists(config_path):
os.remove(config_path)

self.assertEqual(repos[0]['cwd'], '/test')
remotes = repos[0]['remotes']
self.assertEqual(
remotes[0]['url'], os.environ['TEST_REPO']
)

def test_fetch_all_string(self):
config_yaml = """
./test:
Expand Down
1 change: 1 addition & 0 deletions tests/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def test_multithreading(self):
dirmatch=None,
do_push=False,
expand_env=False,
env_file=None,
force=False,
)

Expand Down

0 comments on commit e303e9b

Please sign in to comment.