Skip to content

Commit

Permalink
Fix. Load config file from env (#92)
Browse files Browse the repository at this point in the history
If you set a config file from env, but exists a default file. PyMS load the default file first instead the env file
  • Loading branch information
avara1986 committed Feb 27, 2020
1 parent 31fc58e commit ba509ad
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyms/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_input(msg): # pragma: no cover
def run(self):
crypt = Crypt()
if self.create_key:
path = crypt._loader.get_or_setpath() # pylint: disable=protected-access
path = crypt._loader.get_path_from_env() # pylint: disable=protected-access
pwd = self.get_input('Type a password to generate the key file: ')
generate_file = self.get_input('Do you want to generate a file in {}? [Y/n]'.format(path))
generate_file = generate_file.lower() != "n"
Expand Down
3 changes: 2 additions & 1 deletion pyms/config/confile.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def __init__(self, *args, **kwargs):
if self._empty_init:
config = {}
else:
raise ConfigDoesNotFoundException("Configuration file not found")
path = self._loader.path if self._loader.path else ""
raise ConfigDoesNotFoundException("Configuration file {}not found".format(path + " "))

config = self.set_config(config)

Expand Down
2 changes: 1 addition & 1 deletion pyms/utils/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def decrypt(self, encrypted):
return str(decrypted, encoding="utf-8")

def delete_key(self):
os.remove(self._loader.get_or_setpath())
os.remove(self._loader.get_path_from_env())
20 changes: 9 additions & 11 deletions pyms/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,24 @@ def __init__(self, path, env_var, default_filename):
self.default_file = default_filename

def get_file(self, fn=None):
return self._get_conf_from_file(fn) or self._get_conf_from_env(fn)
return self._get_conf_from_env(fn) or self._get_conf_from_file(self.path, fn)

def put_file(self, content, mode="w"):
self.get_or_setpath()
file_to_write = open(self.path, mode)
path = self.get_path_from_env()
file_to_write = open(path, mode)
file_to_write.write(content) # The key is type bytes still
file_to_write.close()

def get_or_setpath(self):
def get_path_from_env(self):
config_file = os.environ.get(self.file_env_location, self.default_file)
logger.debug("Searching file in ENV[{}]: {}...".format(self.file_env_location, config_file))
self.path = config_file
return self.path
return config_file

def _get_conf_from_env(self, fn=None):
self.get_or_setpath()
return self._get_conf_from_file(fn)

def _get_conf_from_file(self, fn=None):
path = self.path
path = self.get_path_from_env()
return self._get_conf_from_file(path, fn)

def _get_conf_from_file(self, path, fn=None):
if path and os.path.isdir(path):
path = os.path.join(path, self.default_file)

Expand All @@ -48,6 +45,7 @@ def _get_conf_from_file(self, fn=None):
return {}
if path not in files_cached:
logger.debug("[CONF] Configmap {} found".format(path))
self.path = path
if fn:
files_cached[path] = fn(path)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_crypt_file_error(self):
cmd = Command(arguments=arguments, autorun=False)
with pytest.raises(FileDoesNotExistException) as excinfo:
cmd.run()
assert ("Decrypt key key.key not exists. You must set a correct env var KEY_FILE or run "
assert ("Decrypt key None not exists. You must set a correct env var KEY_FILE or run "
"`pyms crypt create-key` command") \
in str(excinfo.value)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_crypt_file_error(self):
crypt = Crypt()
with pytest.raises(FileDoesNotExistException) as excinfo:
crypt.read_key()
assert ("Decrypt key key.key not exists. You must set a correct env var KEY_FILE or run "
assert ("Decrypt key None not exists. You must set a correct env var KEY_FILE or run "
"`pyms crypt create-key` command") \
in str(excinfo.value)

Expand Down

0 comments on commit ba509ad

Please sign in to comment.