Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell): run shell command conditionally #321

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ fine-grained control.
| `stdin` | Allow a command to read from standard input (default: false) |
| `stdout` | Show a command's output from stdout (default: false) |
| `stderr` | Show a command's error output from stderr (default: false) |
| `if` | Run command if a condition is true (default: true) (optional) |

Note that `quiet` controls whether the command (a string) is printed in log
output, it does not control whether the output from running the command is
Expand All @@ -356,6 +357,19 @@ printed (that is controlled by `stdout` / `stderr`). When a command's `stdin` /
stderr: true
```

##### Running shell command conditionally

```yaml
- shell:
- command: apt update && apt upgrade -y
if: lsb_release -i | grep -io 'debian'
description: Update APT package repository

- command: dnf update -y
if: lsb_release -i | grep -io 'fedora'
description: Update DNF package repository
```

### Clean

Clean commands specify directories that should be checked for dead symbolic
Expand Down
24 changes: 24 additions & 0 deletions dotbot/plugins/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ def _process_commands(self, data):
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
Expand Down