Skip to content

Commit 9880579

Browse files
committed
add heredoc example, closes #211
1 parent c365149 commit 9880579

File tree

9 files changed

+107
-0
lines changed

9 files changed

+107
-0
lines changed

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Each of these examples demonstrates one aspect or feature of bashly.
4747
- [docker-like](docker-like#readme) - a sample script with deep commands (like `docker container run`)
4848
- [git-like](git-like#readme) - a sample script with sub-commands similar to git
4949

50+
## Other Examples
51+
52+
- [heredoc](heredoc#readme) - using heredoc strings
53+
5054
## Bashly library features
5155

5256
- [config-ini](config-ini#readme) - using the config (INI) functions

examples/heredoc/.gitignore

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

examples/heredoc/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Heredoc Example
2+
3+
This example shows how (and where) you can use *Here document* (heredoc)
4+
constructs.
5+
6+
Since bashly applies indentation to most of your code, it is not possible to
7+
use heredoc everywhere. Code in the `src/lib` directory is injected to your
8+
script as is, so in cases where you must use heredocs, this is where you can.
9+
10+
This example was generated with:
11+
12+
```bash
13+
$ bashly init
14+
$ bashly add lib
15+
# ... now edit src/bashly.yml to match the example ...
16+
# ... now edit src/root_command.sh to match the example ...
17+
# ... now edit src/lib/heredocs.sh to match the example ...
18+
$ bashly generate
19+
```
20+
21+
<!-- include: src/root_command.sh src/lib/heredocs.sh -->
22+
23+
-----
24+
25+
## `bashly.yml`
26+
27+
```yaml
28+
name: cli
29+
help: Sample application showing the use of heredoc
30+
version: 0.1.0
31+
```
32+
33+
## `src/root_command.sh`
34+
35+
```bash
36+
echo "$(message1)"
37+
```
38+
39+
## `src/lib/heredocs.sh`
40+
41+
```bash
42+
message1() {
43+
cat << EOF
44+
this is a
45+
multiline
46+
heredoc text
47+
EOF
48+
}
49+
50+
```
51+
52+
53+
## Generated script output
54+
55+
### `$ ./cli`
56+
57+
```shell
58+
this is a
59+
multiline
60+
heredoc text
61+
62+
63+
```
64+
65+
66+

examples/heredoc/src/bashly.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: cli
2+
help: Sample application showing the use of heredoc
3+
version: 0.1.0

examples/heredoc/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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
message1() {
2+
cat << EOF
3+
this is a
4+
multiline
5+
heredoc text
6+
EOF
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo "$(message1)"

examples/heredoc/test.sh

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

spec/approvals/examples/heredoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
+ bashly generate
2+
creating user files in src
3+
skipped src/initialize.sh (exists)
4+
skipped src/root_command.sh (exists)
5+
created ./cli
6+
run ./cli --help to test your bash script
7+
+ ./cli
8+
this is a
9+
multiline
10+
heredoc text

0 commit comments

Comments
 (0)