|
| 1 | +# Filters Example |
| 2 | + |
| 3 | +Demonstrates the use of filters that run before a command, and possibly halt |
| 4 | +its execution. |
| 5 | + |
| 6 | +This example was generated with: |
| 7 | + |
| 8 | +```bash |
| 9 | +$ bashly init |
| 10 | +# ... now edit src/bashly.yml to match the example ... |
| 11 | +# ... now edit src/lib/filter_docker_running.sh to match the example ... |
| 12 | +# ... now edit src/lib/filter_redis_running.sh to match the example ... |
| 13 | +$ bashly generate |
| 14 | +``` |
| 15 | + |
| 16 | +<!-- include: src/lib/filter_docker_running.sh src/lib/filter_redis_running.sh --> |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +----- |
| 21 | + |
| 22 | +## `bashly.yml` |
| 23 | + |
| 24 | +```yaml |
| 25 | +name: cli |
| 26 | +help: Sample application |
| 27 | +version: 0.1.0 |
| 28 | + |
| 29 | +commands: |
| 30 | +- name: container |
| 31 | + help: Perform actions on a docker container |
| 32 | + |
| 33 | + # The filters option can be a string or an array of strings. |
| 34 | + # When the command is executed, your script will look for a function named |
| 35 | + # "filter_docker_running" and execute it. If it prints a string, it will be |
| 36 | + # considered an error and the command execution will be halted. |
| 37 | + filters: docker_running |
| 38 | + |
| 39 | + args: |
| 40 | + - name: id |
| 41 | + required: true |
| 42 | + help: Container ID |
| 43 | + |
| 44 | +- name: redis |
| 45 | + help: Perform actions in redis |
| 46 | + filters: [docker_running, redis_running] |
| 47 | +``` |
| 48 | +
|
| 49 | +## `src/lib/filter_docker_running.sh` |
| 50 | + |
| 51 | +```bash |
| 52 | +# Print an error string if docker is not running. |
| 53 | +# The script will automatically exit if this function prints anything. |
| 54 | +filter_docker_running() { |
| 55 | + docker info > /dev/null 2>&1 || echo "Docker must be running" |
| 56 | +} |
| 57 | +
|
| 58 | +``` |
| 59 | + |
| 60 | +## `src/lib/filter_redis_running.sh` |
| 61 | + |
| 62 | +```bash |
| 63 | +# This is just a sample filter designed to always fail |
| 64 | +filter_redis_running() { |
| 65 | + echo "Redis must be running (fake)" |
| 66 | +} |
| 67 | +
|
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +## Generated script output |
| 72 | + |
| 73 | +### `$ ./cli container sample-id` |
| 74 | + |
| 75 | +```shell |
| 76 | +# this file is located in 'src/container_command.sh' |
| 77 | +# code for 'cli container' goes here |
| 78 | +# you can edit it freely and regenerate (it will not be overwritten) |
| 79 | +args: |
| 80 | +- ${args[id]} = sample-id |
| 81 | +
|
| 82 | +
|
| 83 | +``` |
| 84 | + |
| 85 | +### `$ ./cli redis` |
| 86 | + |
| 87 | +```shell |
| 88 | +Redis must be running (fake) |
| 89 | +
|
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments