Skip to content

Commit b2f9a2b

Browse files
authored
Merge pull request #563 from DannyBen/add/command-variables
Add `command.variables` as a convenience utility for defining global command variables
2 parents 9e8d7ab + 0522209 commit b2f9a2b

File tree

15 files changed

+279
-3
lines changed

15 files changed

+279
-3
lines changed

examples/variables/.gitignore

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

examples/variables/README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Variables Example
2+
3+
Demonstrates how to define variables from the bashly.yml configuration.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init
9+
# ... now edit src/bashly.yml to match the example ...
10+
# ... now edit src/download_command.sh to match the example ...
11+
# ... now edit src/compress_command.sh to match the example ...
12+
$ bashly generate
13+
```
14+
15+
<!-- include: src/download_command.sh src/compress_command.sh -->
16+
17+
-----
18+
19+
## `bashly.yml`
20+
21+
````yaml
22+
name: cli
23+
help: Sample application demonstrating the use of variables
24+
version: 0.1.0
25+
26+
# The `build_number` variable will be available globally
27+
variables:
28+
- name: build_number
29+
value: 1337
30+
31+
commands:
32+
- name: download
33+
alias: d
34+
help: Download a profile
35+
36+
args:
37+
- name: profile
38+
required: true
39+
help: Profile to download information from
40+
41+
# These variables will be declared when the `download` command is executed.
42+
# Note the use of an array value.
43+
variables:
44+
- name: output_folder
45+
value: output
46+
- name: download_sources
47+
value:
48+
- youtube
49+
- instagram
50+
51+
- name: compress
52+
alias: c
53+
help: Compress data
54+
55+
# These variables will be declared when the `compress` command is executed.
56+
# Note the use of an associative array value.
57+
variables:
58+
- name: zip_options
59+
value:
60+
pattern: "*.json"
61+
compression_level: fast
62+
````
63+
64+
## `src/download_command.sh`
65+
66+
````bash
67+
echo "build_number: $build_number"
68+
echo "output_folder: $output_folder"
69+
echo "download_sources:"
70+
for value in "${download_sources[@]}"; do
71+
echo "- $value"
72+
done
73+
````
74+
75+
## `src/compress_command.sh`
76+
77+
````bash
78+
echo "build_number: $build_number"
79+
echo "zip_options:"
80+
for key in "${!zip_options[@]}"; do
81+
echo " $key: ${zip_options[$key]}"
82+
done
83+
84+
````
85+
86+
87+
## Output
88+
89+
### `$ ./cli download something`
90+
91+
````shell
92+
build_number: 1337
93+
output_folder: output
94+
download_sources:
95+
- youtube
96+
- instagram
97+
98+
99+
````
100+
101+
### `$ ./cli compress`
102+
103+
````shell
104+
build_number: 1337
105+
zip_options:
106+
compression_level: fast
107+
pattern: *.json
108+
109+
110+
````
111+
112+
113+

examples/variables/src/bashly.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: cli
2+
help: Sample application demonstrating the use of variables
3+
version: 0.1.0
4+
5+
# The `build_number` variable will be available globally
6+
variables:
7+
- name: build_number
8+
value: 1337
9+
10+
commands:
11+
- name: download
12+
alias: d
13+
help: Download a profile
14+
15+
args:
16+
- name: profile
17+
required: true
18+
help: Profile to download information from
19+
20+
# These variables will be declared when the `download` command is executed.
21+
# Note the use of an array value.
22+
variables:
23+
- name: output_folder
24+
value: output
25+
- name: download_sources
26+
value:
27+
- youtube
28+
- instagram
29+
30+
- name: compress
31+
alias: c
32+
help: Compress data
33+
34+
# These variables will be declared when the `compress` command is executed.
35+
# Note the use of an associative array value.
36+
variables:
37+
- name: zip_options
38+
value:
39+
pattern: "*.json"
40+
compression_level: fast
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
echo "build_number: $build_number"
2+
echo "zip_options:"
3+
for key in "${!zip_options[@]}"; do
4+
echo " $key: ${zip_options[$key]}"
5+
done
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
echo "build_number: $build_number"
2+
echo "output_folder: $output_folder"
3+
echo "download_sources:"
4+
for value in "${download_sources[@]}"; do
5+
echo "- $value"
6+
done

examples/variables/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 download something
10+
./cli compress

lib/bashly/config_validator.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def assert_version(key, value)
2424
def assert_catch_all(key, value)
2525
return unless value
2626

27-
assert [TrueClass, String, Hash].include?(value.class),
27+
assert [TrueClass, FalseClass, String, Hash].include?(value.class),
2828
"#{key} must be a boolean, a string or a hash"
2929

3030
assert_catch_all_hash key, value if value.is_a? Hash
@@ -77,7 +77,7 @@ def assert_dependency(key, value)
7777
def assert_extensible(key, value)
7878
return unless value
7979

80-
assert [TrueClass, String].include?(value.class),
80+
assert [TrueClass, FalseClass, String].include?(value.class),
8181
"#{key} must be a boolean or a string"
8282
end
8383

@@ -173,6 +173,11 @@ def assert_env_var(key, value)
173173
refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
174174
end
175175

176+
def assert_var(key, value)
177+
assert_hash key, value, keys: %i[name value]
178+
assert_string "#{key}.name", value['name']
179+
end
180+
176181
def assert_command(key, value)
177182
assert_hash key, value, keys: Script::Command.option_keys
178183

@@ -207,6 +212,7 @@ def assert_command(key, value)
207212
assert_array "#{key}.completions", value['completions'], of: :string
208213
assert_array "#{key}.filters", value['filters'], of: :string
209214
assert_array "#{key}.environment_variables", value['environment_variables'], of: :env_var
215+
assert_array "#{key}.variables", value['variables'], of: :var
210216

211217
assert_uniq "#{key}.commands", value['commands'], %w[name alias]
212218
assert_uniq "#{key}.flags", value['flags'], 'long'

lib/bashly/docs/command.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,28 @@ command.private:
309309
help: Send bash completions
310310
private: true
311311
312+
command.variables:
313+
help: Define a list of global bash variables.
314+
url: https://bashly.dannyb.co/configuration/command/#variables
315+
example: |-
316+
variables:
317+
# Simple value
318+
- name: output_folder
319+
value: output
320+
321+
# Array
322+
- name: download_sources
323+
value:
324+
- youtube
325+
- instagram
326+
327+
# Associative array
328+
- name: zip_options
329+
value:
330+
pattern: "*.json"
331+
compression_level: fast
332+
333+
312334
command.version:
313335
help: Specify the version to show when running with `--version`.
314336
url: https://bashly.dannyb.co/configuration/command/#version

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def option_keys
1717
default dependencies environment_variables examples
1818
extensible expose filename filters flags
1919
footer function group help name
20-
private version
20+
private variables version
2121
]
2222
end
2323
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
= view_marker
22

33
> {{ function_name }}_command() {
4+
= render(:variables).indent(2) if variables&.any?
45
= load_user_file(filename).indent 2
56
> }
67
>

0 commit comments

Comments
 (0)