mirror of
1
0
Fork 0

feat(shell): run shell command conditionally

## what
  - add feature to run shell command (optionally)

  ## how
  - the shell directive will check if the 'if' property is available.
    If it is, it will run the command in 'if' property.
    If the command results in a return 0 code or 'true', then the
    'command' property will run.
    If the condition to run the command is a non 0 code or 'false',
      the shell command won't run
  - dotbot config example:
    ```yaml
    - shell:
        - command: echo "this is running on a MacOS"
          if: uname -s | grep -i "Darwin"
    ```
  - dotbot config example: skipping command if false
    ```yaml
    - shell:
        - command: echo "This command should be skipped"
          if: false
    ```

  ## why
  - can run the shell command conditionally
  - can use multiple dotbot configs to run different OS
    - Ex: incorporating it in https://github.com/ecarlson94/dotbot-template

  ## where
  - file changed in `./dotbot/plugins/shell.py`

  ## usage
  Create dotbot config
    ```yaml
    - shell:
        - command: echo "this is running on a MacOS"
          if: uname -s | grep -i "Darwin"
    ```

    ```yaml
    - shell:
        - command: echo "This command should be skipped"
          if: false
    ```
This commit is contained in:
Clumsy-Coder 2022-08-11 20:37:54 -06:00
parent d2f76a2593
commit 25ef5d5a5f
1 changed files with 24 additions and 0 deletions

View File

@ -36,6 +36,30 @@ class Shell(dotbot.Plugin):
stdout = item.get("stdout", stdout)
stderr = item.get("stderr", stderr)
quiet = item.get("quiet", quiet)
# run shell command if the 'if' key is present
# Ex:
# - shell:
# - command: echo "This computer is a Mac"
# if: uname -s | grep -i "Darwin"
#
# Ex: skipping shell command
# - shell:
# - command: echo "skip this shell command"
# if: false
if "if" in item:
run_if_result = dotbot.util.shell_command(
str(item["if"]), # this to make sure python doesn't run it. Had a odd behaviour with having the value 'false'. Check Pull Request #321
cwd=self._context.base_directory(),
enable_stdin=False,
enable_stdout=False,
enable_stderr=stderr,
)
# if the condition to run the command is false,
# skip running the command
if run_if_result != 0:
continue
elif isinstance(item, list):
cmd = item[0]
msg = item[1] if len(item) > 1 else None