Skip to content

Commit

Permalink
Run shell substitutions in $ASTRALITY_CONFIG_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 12, 2018
1 parent 7573492 commit 8eab97e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion astrality/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def infer_config_location(
config_directory: Optional[Path] = None,
) -> Tuple[Path, Path]:
"""
Return path of Astrality configuration file based on config folder path.
Try to find the configuration directory and file for astrality, based on
filesystem or specific environment variables if they are present several
places to put it. If the expected config file is not present, use an
Expand Down Expand Up @@ -205,12 +207,16 @@ def expand_environment_variable(match: Match[str]) -> str:

def insert_command_substitutions(content: str) -> str:
"""Replace all occurences in string: $(command) -> command stdout."""
working_directory = infer_config_location()[0].parent

command_substitution_pattern = re.compile(r'\$\((.*)\)')

def command_substitution(match: Match[str]) -> str:
command = match.groups()[0]
result = run_shell(command=command)
result = run_shell(
command=command,
working_directory=working_directory,
)
if result == '':
logger.error(
f'Command substitution $({command}) returned empty stdout.'
Expand Down
7 changes: 5 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ Astrality makes two non-standard additions to the ``YAML`` syntax, so-called int
Then the occurrence of ``${VAR1}`` in ``astrality.yaml`` will be replaced with ``run command`` and **not** ``run $VAR2``.
If you want the ability to turn off this "recursive expansion" feature, `open an issue <https://github.com/JakobGM/astrality/issues>`_, and I will add configuration option for it.

.. caution::
Only ``${NAME}`` blocks are expanded. ``$NAME`` will be left in place, to allow runtime expansions of environment variables when modules define shell commands to be run.

.. _command_substitution:

* **Command substitution**:
* **Command substitution**:
``$( some_shell_command )`` is replaced with the standard output resulting from running ``some_shell_command`` in a ``bash`` shell.

.. note::
Shell commands are run from ``$ASTRALITY_CONFIG_HOME``. If you need to refer to paths outside this directory, you can use absolute paths, e.g. ``$( cat ~/.home_directory_file )``.

.. note::

Interpolations in ``astrality.yaml`` occur on Astrality startup, and will not reflect changes to environment variables and shell commands after startup.
Expand Down

0 comments on commit 8eab97e

Please sign in to comment.