Skip to content

Commit 7c6e712

Browse files
committed
- Refactor inspect_args handling of environment variables
1 parent ccf73dc commit 7c6e712

File tree

15 files changed

+107
-63
lines changed

15 files changed

+107
-63
lines changed

examples/command-aliases/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ Examples:
147147
args:
148148
- ${args[source]} = somefile
149149

150+
environment variables:
151+
- $API_KEY =
152+
150153

151154
````
152155

@@ -215,6 +218,9 @@ Examples:
215218
args:
216219
- ${args[source]} = somefile
217220

221+
environment variables:
222+
- $API_KEY =
223+
218224

219225
````
220226

examples/commands/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ args:
185185
- ${args[source]} = sourcefile
186186
- ${args[target]} = targetfile
187187

188+
environment variables:
189+
- $API_KEY =
190+
- $DEFAULT_TARGET_LOCATION =
191+
188192

189193
````
190194

@@ -235,6 +239,9 @@ args:
235239
- ${args[source]} = sourcefile
236240
- ${args[--user]} = username
237241

242+
environment variables:
243+
- $API_KEY =
244+
238245

239246
````
240247

examples/environment-variables/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ commands:
4646

4747
# Using the `default: value` option will cause the value to variable to be
4848
# set if it is not provided by the user.
49+
- name: region
50+
help: Cloud region
51+
default: us-east-2
52+
53+
# Using the `allowed: [value1, value2]` option will halt the script's
54+
# execution with a friendly error message, unless the variable matches one
55+
# of the defined values.
4956
- name: environment
5057
help: One of development, production or test
58+
allowed: [development, production, testing]
5159
default: development
5260
````
5361

@@ -59,11 +67,6 @@ echo "# code for 'cli verify' goes here"
5967
echo "# you can edit it freely and regenerate (it will not be overwritten)"
6068
inspect_args
6169

62-
echo "environment:"
63-
echo "- API_KEY=${API_KEY:-}"
64-
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
65-
echo "- MY_SECRET=${MY_SECRET:-}"
66-
6770
````
6871

6972

@@ -133,8 +136,13 @@ Environment Variables:
133136
MY_SECRET (required)
134137
Your secret
135138

139+
REGION
140+
Cloud region
141+
Default: us-east-2
142+
136143
ENVIRONMENT
137144
One of development, production or test
145+
Allowed: development, production, testing
138146
Default: development
139147

140148

@@ -156,10 +164,12 @@ missing required environment variable: MY_SECRET
156164
# code for 'cli verify' goes here
157165
# you can edit it freely and regenerate (it will not be overwritten)
158166
args: none
159-
environment:
160-
- API_KEY=
161-
- ENVIRONMENT=development
162-
- MY_SECRET=there is no spoon
167+
168+
environment variables:
169+
- $API_KEY =
170+
- $ENVIRONMENT = development
171+
- $MY_SECRET = there is no spoon
172+
- $REGION = us-east-2
163173

164174

165175
````
@@ -171,10 +181,12 @@ environment:
171181
# code for 'cli verify' goes here
172182
# you can edit it freely and regenerate (it will not be overwritten)
173183
args: none
174-
environment:
175-
- API_KEY=
176-
- ENVIRONMENT=production
177-
- MY_SECRET=safe-with-me
184+
185+
environment variables:
186+
- $API_KEY =
187+
- $ENVIRONMENT = production
188+
- $MY_SECRET = safe-with-me
189+
- $REGION = us-east-2
178190

179191

180192
````

examples/environment-variables/src/bashly.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ commands:
2323

2424
# Using the `default: value` option will cause the value to variable to be
2525
# set if it is not provided by the user.
26+
- name: region
27+
help: Cloud region
28+
default: us-east-2
29+
30+
# Using the `allowed: [value1, value2]` option will halt the script's
31+
# execution with a friendly error message, unless the variable matches one
32+
# of the defined values.
2633
- name: environment
2734
help: One of development, production or test
35+
allowed: [development, production, testing]
2836
default: development

examples/environment-variables/src/verify_command.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,3 @@ echo "# this file is located in 'src/verify_command.sh'"
22
echo "# code for 'cli verify' goes here"
33
echo "# you can edit it freely and regenerate (it will not be overwritten)"
44
inspect_args
5-
6-
echo "environment:"
7-
echo "- API_KEY=${API_KEY:-}"
8-
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
9-
echo "- MY_SECRET=${MY_SECRET:-}"

examples/multiline/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Examples:
142142
# you can edit it freely and regenerate (it will not be overwritten)
143143
args: none
144144

145+
environment variables:
146+
- $MULTI_VITAMIN =
147+
145148

146149
````
147150

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
if default_environment_variables.any? || required_environment_variables.any? ||
2-
whitelisted_environment_variables.any?
3-
4-
= view_marker
1+
if environment_variables.any?
2+
= view_marker
53
= render(:environment_variables_default)
64

7-
if required_environment_variables.any?
8-
required_environment_variables.each do |env_var|
9-
> if [[ -z "${<%= env_var.name.upcase %>:-}" ]]; then
10-
> printf "{{ strings[:missing_required_environment_variable] % { var: env_var.name.upcase } }}\n" >&2
11-
> exit 1
12-
> fi
13-
end
5+
environment_variables.each do |env_var|
6+
> env_var_names+=("{{ env_var.name.upcase }}")
7+
end
8+
end
9+
10+
if required_environment_variables.any?
11+
required_environment_variables.each do |env_var|
12+
> if [[ -z "${<%= env_var.name.upcase %>:-}" ]]; then
13+
> printf "{{ strings[:missing_required_environment_variable] % { var: env_var.name.upcase } }}\n" >&2
14+
> exit 1
15+
> fi
1416
end
17+
end
1518

16-
if whitelisted_environment_variables.any?
17-
whitelisted_environment_variables.each do |env_var|
18-
> if [[ -n "${<%= env_var.name.upcase %>:-}" ]] && [[ ! ${<%= env_var.name.upcase %>:-} =~ ^({{ env_var.allowed.join '|' }})$ ]]; then
19-
> printf "%s\n" "{{ strings[:disallowed_environment_variable] % { name: env_var.name.upcase, allowed: env_var.allowed.join(', ') } }}" >&2
20-
> exit 1
21-
> fi
22-
end
19+
if whitelisted_environment_variables.any?
20+
whitelisted_environment_variables.each do |env_var|
21+
> if [[ -n "${<%= env_var.name.upcase %>:-}" ]] && [[ ! ${<%= env_var.name.upcase %>:-} =~ ^({{ env_var.allowed.join '|' }})$ ]]; then
22+
> printf "%s\n" "{{ strings[:disallowed_environment_variable] % { name: env_var.name.upcase, allowed: env_var.allowed.join(', ') } }}" >&2
23+
> exit 1
24+
> fi
2325
end
24-
end
26+
end

lib/bashly/views/command/inspect_args.gtx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
> if ((${#args[@]})); then
55
> readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
66
> echo args:
7-
> for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
7+
> for k in "${sorted_keys[@]}"; do
8+
> echo "- \${args[$k]} = ${args[$k]}"
9+
> done
810
> else
911
> echo args: none
1012
> fi
@@ -22,16 +24,18 @@
2224
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
2325
> echo
2426
> echo deps:
25-
> for k in "${sorted_keys[@]}"; do echo "- \${deps[$k]} = ${deps[$k]}"; done
27+
> for k in "${sorted_keys[@]}"; do
28+
> echo "- \${deps[$k]} = ${deps[$k]}"
29+
> done
2630
> fi
2731
>
28-
if environment_variables.any?
29-
> echo
30-
> echo environment variables:
31-
environment_variables.each do |env_var|
32-
> echo "- \${{ env_var.name.upcase }} = ${<%= env_var.name.upcase %>:-unset}"
33-
end
34-
>
35-
end
32+
> if ((${#env_var_names[@]})); then
33+
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
34+
> echo
35+
> echo "environment variables:"
36+
> for k in "${sorted_names[@]}"; do
37+
> echo "- \$$k = ${!k:-}"
38+
> done
39+
> fi
3640
> }
3741
>

lib/bashly/views/command/run.gtx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
> declare -A args=()
55
> declare -A deps=()
66
> declare -a other_args=()
7+
> declare -a env_var_names=()
78
> declare -a input=()
89
> normalize_input "$@"
910
> parse_requirements "${input[@]}"

spec/approvals/examples/command-aliases

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ args:
7171
- ${args[source]} = somefile
7272

7373
environment variables:
74-
- $API_KEY = unset
74+
- $API_KEY =
7575
+ ./cli upload --help
7676
cli upload - Upload a file
7777

@@ -124,4 +124,4 @@ args:
124124
- ${args[source]} = somefile
125125

126126
environment variables:
127-
- $API_KEY = unset
127+
- $API_KEY =

0 commit comments

Comments
 (0)