Skip to content

Commit a402e87

Browse files
committed
add support for specifying dependencies
1 parent 5c8f611 commit a402e87

File tree

25 files changed

+426
-1
lines changed

25 files changed

+426
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ args:
177177
# If this is provided, then you cannot specify commands.
178178
flags:
179179
- ... see below ...
180+
181+
# Specify an array of any required external dependencies (commands).
182+
# The script execution will be halted with a friendly error unless all
183+
# dependency commands exist.
184+
dependencies:
185+
- curl
180186
```
181187
182188

examples/colors/colorly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ parse_requirements() {
116116

117117
esac
118118
# :command.environment_variables_filter
119+
# :command.dependencies_filter
119120
# :command.command_filter
120121
action="root"
121122
# :command.required_args_filter

examples/config-ini/configly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ parse_requirements() {
291291

292292
esac
293293
# :command.environment_variables_filter
294+
# :command.dependencies_filter
294295
# :command.command_filter
295296
action=$1
296297

@@ -364,6 +365,7 @@ configly_set_parse_requirements() {
364365

365366
esac
366367
# :command.environment_variables_filter
368+
# :command.dependencies_filter
367369
# :command.command_filter
368370
action="set"
369371
# :command.required_args_filter
@@ -428,6 +430,7 @@ configly_get_parse_requirements() {
428430

429431
esac
430432
# :command.environment_variables_filter
433+
# :command.dependencies_filter
431434
# :command.command_filter
432435
action="get"
433436
# :command.required_args_filter
@@ -481,6 +484,7 @@ configly_list_parse_requirements() {
481484

482485
esac
483486
# :command.environment_variables_filter
487+
# :command.dependencies_filter
484488
# :command.command_filter
485489
action="list"
486490
# :command.required_args_filter

examples/custom-includes/download

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ parse_requirements() {
8888

8989
esac
9090
# :command.environment_variables_filter
91+
# :command.dependencies_filter
9192
# :command.command_filter
9293
action="root"
9394
# :command.required_args_filter

examples/custom-strings/download

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ parse_requirements() {
8383

8484
esac
8585
# :command.environment_variables_filter
86+
# :command.dependencies_filter
8687
# :command.command_filter
8788
action="root"
8889
# :command.required_args_filter

examples/dependencies/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Environment Variables Example
2+
==================================================
3+
4+
This example was generated with:
5+
6+
$ bashly init
7+
$ bashly generate

examples/dependencies/cli

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :command.version_command
6+
version_command() {
7+
echo "$version"
8+
}
9+
10+
# :command.usage
11+
cli_usage() {
12+
if [[ -n $long_usage ]]; then
13+
echo -e "cli - Sample application that requires dependencies"
14+
echo
15+
else
16+
echo -e "cli - Sample application that requires dependencies"
17+
echo
18+
fi
19+
echo -e "Usage:"
20+
echo -e " cli [command] [options]"
21+
echo -e " cli [command] --help | -h"
22+
echo -e " cli --version"
23+
echo
24+
# :command.usage_commands
25+
echo -e "Commands:"
26+
echo " download Download something"
27+
echo " upload Upload something"
28+
echo
29+
30+
if [[ -n $long_usage ]]; then
31+
echo -e "Options:"
32+
# :command.usage_fixed_flags
33+
echo " --help, -h"
34+
echo -e " Show this help"
35+
echo
36+
echo " --version"
37+
echo -e " Show version number"
38+
echo
39+
40+
fi
41+
}
42+
43+
# :command.usage
44+
cli_download_usage() {
45+
if [[ -n $long_usage ]]; then
46+
echo -e "cli download - Download something"
47+
echo
48+
else
49+
echo -e "cli download - Download something"
50+
echo
51+
fi
52+
echo -e "Usage:"
53+
echo -e " cli download [options]"
54+
echo -e " cli download --help | -h"
55+
echo
56+
57+
if [[ -n $long_usage ]]; then
58+
echo -e "Options:"
59+
# :command.usage_fixed_flags
60+
echo " --help, -h"
61+
echo -e " Show this help"
62+
echo
63+
64+
fi
65+
}
66+
67+
# :command.usage
68+
cli_upload_usage() {
69+
if [[ -n $long_usage ]]; then
70+
echo -e "cli upload - Upload something"
71+
echo
72+
else
73+
echo -e "cli upload - Upload something"
74+
echo
75+
fi
76+
echo -e "Usage:"
77+
echo -e " cli upload [options]"
78+
echo -e " cli upload --help | -h"
79+
echo
80+
81+
if [[ -n $long_usage ]]; then
82+
echo -e "Options:"
83+
# :command.usage_fixed_flags
84+
echo " --help, -h"
85+
echo -e " Show this help"
86+
echo
87+
88+
fi
89+
}
90+
91+
# :command.inspect_args
92+
inspect_args() {
93+
echo args:
94+
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
95+
}
96+
97+
# :command.command_functions
98+
# :command.function
99+
cli_download_command() {
100+
# :src/download_command.sh
101+
echo "# this file is located in 'src/download_command.sh'"
102+
echo "# code for 'cli download' goes here"
103+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
104+
inspect_args
105+
}
106+
107+
# :command.function
108+
cli_upload_command() {
109+
# :src/upload_command.sh
110+
echo "# this file is located in 'src/upload_command.sh'"
111+
echo "# code for 'cli upload' goes here"
112+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
113+
inspect_args
114+
}
115+
116+
# :command.parse_requirements
117+
parse_requirements() {
118+
# :command.fixed_flag_filter
119+
case "$1" in
120+
--version )
121+
version_command
122+
exit 1
123+
;;
124+
125+
--help | -h )
126+
long_usage=yes
127+
cli_usage
128+
exit 1
129+
;;
130+
131+
esac
132+
# :command.environment_variables_filter
133+
# :command.dependencies_filter
134+
# :command.command_filter
135+
action=$1
136+
137+
case $action in
138+
-* )
139+
;;
140+
141+
download )
142+
action="download"
143+
shift
144+
cli_download_parse_requirements "$@"
145+
shift $#
146+
;;
147+
148+
upload )
149+
action="upload"
150+
shift
151+
cli_upload_parse_requirements "$@"
152+
shift $#
153+
;;
154+
155+
* )
156+
cli_usage
157+
exit 1
158+
;;
159+
160+
esac
161+
# :command.required_args_filter
162+
# :command.required_flags_filter
163+
# :command.parse_requirements_while
164+
while [[ $# -gt 0 ]]; do
165+
key="$1"
166+
case "$key" in
167+
168+
-* )
169+
echo -e "invalid option: $key"
170+
exit 1
171+
;;
172+
173+
* )
174+
# :command.parse_requirements_case
175+
echo -e "invalid argument: $key"
176+
exit 1
177+
;;
178+
179+
esac
180+
done
181+
}
182+
183+
# :command.parse_requirements
184+
cli_download_parse_requirements() {
185+
# :command.fixed_flag_filter
186+
case "$1" in
187+
--version )
188+
version_command
189+
exit 1
190+
;;
191+
192+
--help | -h )
193+
long_usage=yes
194+
cli_download_usage
195+
exit 1
196+
;;
197+
198+
esac
199+
# :command.environment_variables_filter
200+
# :command.dependencies_filter
201+
if ! [[ -x "$(command -v curl)" ]]; then
202+
echo -e "missing dependency: curl"
203+
exit 1
204+
fi
205+
if ! [[ -x "$(command -v shmurl)" ]]; then
206+
echo -e "missing dependency: shmurl"
207+
exit 1
208+
fi
209+
# :command.command_filter
210+
action="download"
211+
# :command.required_args_filter
212+
# :command.required_flags_filter
213+
# :command.parse_requirements_while
214+
while [[ $# -gt 0 ]]; do
215+
key="$1"
216+
case "$key" in
217+
218+
-* )
219+
echo -e "invalid option: $key"
220+
exit 1
221+
;;
222+
223+
* )
224+
# :command.parse_requirements_case
225+
echo -e "invalid argument: $key"
226+
exit 1
227+
;;
228+
229+
esac
230+
done
231+
}
232+
233+
# :command.parse_requirements
234+
cli_upload_parse_requirements() {
235+
# :command.fixed_flag_filter
236+
case "$1" in
237+
--version )
238+
version_command
239+
exit 1
240+
;;
241+
242+
--help | -h )
243+
long_usage=yes
244+
cli_upload_usage
245+
exit 1
246+
;;
247+
248+
esac
249+
# :command.environment_variables_filter
250+
# :command.dependencies_filter
251+
# :command.command_filter
252+
action="upload"
253+
# :command.required_args_filter
254+
# :command.required_flags_filter
255+
# :command.parse_requirements_while
256+
while [[ $# -gt 0 ]]; do
257+
key="$1"
258+
case "$key" in
259+
260+
-* )
261+
echo -e "invalid option: $key"
262+
exit 1
263+
;;
264+
265+
* )
266+
# :command.parse_requirements_case
267+
echo -e "invalid argument: $key"
268+
exit 1
269+
;;
270+
271+
esac
272+
done
273+
}
274+
275+
# :command.initialize
276+
initialize() {
277+
version="0.1.0"
278+
long_usage=''
279+
set -e
280+
281+
# :src/initialize.sh
282+
# Code here runs inside the initialize() function
283+
# Use it for anything that you need to run before any other function, like
284+
# setting environment vairables:
285+
# CONFIG_FILE=settings.ini
286+
#
287+
# Feel free to empty (but not delete) this file.
288+
}
289+
290+
# :command.run
291+
run() {
292+
declare -A args
293+
parse_requirements "$@"
294+
295+
if [[ $action == "download" ]]; then
296+
if [[ ${args[--help]} ]]; then
297+
long_usage=yes
298+
cli_download_usage
299+
else
300+
cli_download_command
301+
fi
302+
303+
elif [[ $action == "upload" ]]; then
304+
if [[ ${args[--help]} ]]; then
305+
long_usage=yes
306+
cli_upload_usage
307+
else
308+
cli_upload_command
309+
fi
310+
311+
elif [[ ${args[--version]} ]]; then
312+
version_command
313+
elif [[ ${args[--help]} ]]; then
314+
long_usage=yes
315+
cli_usage
316+
elif [[ $action == "root" ]]; then
317+
root_command
318+
fi
319+
}
320+
321+
initialize
322+
run "$@"

0 commit comments

Comments
 (0)