Skip to content

Commit 2310022

Browse files
authored
Merge pull request #362 from DannyBen/add/hooks
Add before/after hooks
2 parents 3b27d8b + f71717f commit 2310022

File tree

23 files changed

+239
-17
lines changed

23 files changed

+239
-17
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
6262
- [yaml](yaml#readme) - using the YAML reading functions
6363
- [completions](completions#readme) - adding bash completion functionality
6464
- [validations](validations#readme) - adding argument validation functions
65+
- [hooks](hooks#readme) - adding before/after hooks
6566

6667
## Other Examples
6768

examples/colors/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ $ bashly generate
1313
$ bashly generate
1414
```
1515

16-
The `bashly add colors` command, simply created the [src/lib/colors.sh]
17-
(src/lib/colors.sh) file, with useful color functions that will be
18-
automatically included in the generated script.
16+
The `bashly add colors` command, simply created the
17+
[src/lib/colors.sh](src/lib/colors.sh) file, with useful color functions that
18+
will be automatically included in the generated script.
1919

2020
See the manually edited [src/root_command.sh](src/root_command.sh) for usage
2121
examples.

examples/hooks/.gitignore

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

examples/hooks/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Before/After Hooks Example
2+
3+
This example demonstrates how to run common code before or after executing any
4+
command.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init
10+
# ... now edit src/bashly.yml to match the example ...
11+
$ bashly add hooks
12+
$ bashly generate
13+
```
14+
15+
-----
16+
17+
## `bashly.yml`
18+
19+
```yaml
20+
name: hooks
21+
help: Sample application that uses before/after hooks
22+
version: 0.1.0
23+
24+
flags:
25+
- long: --debug
26+
short: -d
27+
help: Enable debug mode
28+
```
29+
30+
31+
32+
## Generated script output
33+
34+
### `$ ./hooks`
35+
36+
```shell
37+
==[ Before Hook Called ]==
38+
args: none
39+
# this file is located in 'src/root_command.sh'
40+
# you can edit it freely and regenerate (it will not be overwritten)
41+
args: none
42+
==[ After Hook Called ]==
43+
44+
45+
```
46+
47+
### `$ ./hooks --debug`
48+
49+
```shell
50+
==[ Before Hook Called ]==
51+
args:
52+
- ${args[--debug]} = 1
53+
# this file is located in 'src/root_command.sh'
54+
# you can edit it freely and regenerate (it will not be overwritten)
55+
args:
56+
- ${args[--debug]} = 1
57+
==[ After Hook Called ]==
58+
59+
60+
```
61+
62+
63+

examples/hooks/src/after.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## after hook
2+
##
3+
## Any code here will be placed inside an `after_hook()` function and called
4+
## after running any command.
5+
##
6+
## You can safely delete this file if you do not need it.
7+
echo "==[ After Hook Called ]=="

examples/hooks/src/bashly.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: hooks
2+
help: Sample application that uses before/after hooks
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --debug
7+
short: -d
8+
help: Enable debug mode

examples/hooks/src/before.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## before hook
2+
##
3+
## Any code here will be placed inside a `before_hook()` function and called
4+
## before running any command (but after processing its arguments).
5+
##
6+
## You can safely delete this file if you do not need it.
7+
echo "==[ Before Hook Called ]=="
8+
inspect_args

examples/hooks/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 variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.

examples/hooks/src/root_command.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "# this file is located in 'src/root_command.sh'"
2+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
3+
inspect_args

examples/hooks/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 add hooks --force
6+
bashly generate
7+
8+
### Try Me ###
9+
10+
./hooks
11+
./hooks --debug

0 commit comments

Comments
 (0)