Skip to content

Commit d9e1174

Browse files
authored
Merge pull request #175 from DannyBen/add/filters
Add custom command filters
2 parents 3b278fe + 69b54f4 commit d9e1174

File tree

20 files changed

+242
-0
lines changed

20 files changed

+242
-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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

lib/bashly/config_validator.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def assert_extensible(key, value)
7272
"#{key} must be a boolean or a string"
7373
end
7474

75+
def assert_filters(key, value)
76+
return unless value
77+
assert [Array, String].include?(value.class),
78+
"#{key} must be an array or a string"
79+
80+
assert_array key, value, of: :string if value.is_a? Array
81+
end
82+
7583
def assert_arg(key, value)
7684
assert_hash key, value
7785
assert_string "#{key}.name", value['name']
@@ -129,6 +137,7 @@ def assert_command(key, value)
129137
assert_version "#{key}.version", value['version']
130138
assert_catch_all "#{key}.catch_all", value['catch_all']
131139
assert_extensible "#{key}.extensible", value['extensible']
140+
assert_filters "#{key}.filters", value['filters']
132141

133142
assert_array "#{key}.args", value['args'], of: :arg
134143
assert_array "#{key}.flags", value['flags'] , of: :flag

0 commit comments

Comments
 (0)