Skip to content

Commit 436f175

Browse files
committed
- Add custom command filters
1 parent 3b278fe commit 436f175

File tree

16 files changed

+220
-0
lines changed

16 files changed

+220
-0
lines changed

examples/filters/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli

examples/filters/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 -->
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+
61+
## Generated script output
62+
63+
### `$ ./cli container sample-id`
64+
65+
```shell
66+
# this file is located in 'src/container_command.sh'
67+
# code for 'cli container' goes here
68+
# you can edit it freely and regenerate (it will not be overwritten)
69+
args:
70+
- ${args[id]} = sample-id
71+
72+
73+
```
74+
75+
### `$ ./cli redis`
76+
77+
```shell
78+
Redis must be running (fake)
79+
80+
81+
```
82+
83+
### `$ echo $?`
84+
85+
```shell
86+
0
87+
88+
89+
```
90+
91+
92+

examples/filters/src/bashly.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
commands:
6+
- name: container
7+
help: Perform actions on a docker container
8+
9+
# The filters option can be a string or an array of strings.
10+
# When the command is executed, your script will look for a function named
11+
# "filter_docker_running" and execute it. If it prints a string, it will be
12+
# considered an error and the command execution will be halted.
13+
filters: docker_running
14+
15+
args:
16+
- name: id
17+
required: true
18+
help: Container ID
19+
20+
- name: redis
21+
help: Perform actions in redis
22+
filters: [docker_running, redis_running]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/container_command.sh'"
2+
echo "# code for 'cli container' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

examples/filters/src/initialize.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment vairables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Print an error string if docker is not running.
2+
# The script will automatically exit if this function prints anything.
3+
filter_docker_running() {
4+
docker info > /dev/null 2>&1 || echo "Docker must be running"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This is just a sample filter designed to always fail
2+
filter_redis_running() {
3+
echo "Redis must be running (fake)"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/redis_command.sh'"
2+
echo "# code for 'cli redis' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

examples/filters/test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./cli container sample-id
10+
./cli redis
11+
echo $?

lib/bashly/script/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Base
1616
environment_variables
1717
examples
1818
extensible
19+
filters
1920
flags
2021
footer
2122
group

0 commit comments

Comments
 (0)