Skip to content

Commit a96044c

Browse files
authored
Merge pull request #474 from DannyBen/refactor/env-vars-inspect-args
Refactor `inspect_args` handling of environment variables
2 parents ccf73dc + 423a271 commit a96044c

File tree

17 files changed

+146
-74
lines changed

17 files changed

+146
-74
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 & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ $ bashly generate
1414
$ bashly generate
1515
```
1616

17-
<!-- include: src/verify_command.sh -->
18-
1917
-----
2018

2119
## `bashly.yml`
@@ -46,25 +44,19 @@ commands:
4644

4745
# Using the `default: value` option will cause the value to variable to be
4846
# set if it is not provided by the user.
47+
- name: region
48+
help: Cloud region
49+
default: us-east-2
50+
51+
# Using the `allowed: [value1, value2]` option will halt the script's
52+
# execution with a friendly error message, unless the variable matches one
53+
# of the defined values.
4954
- name: environment
5055
help: One of development, production or test
56+
allowed: [development, production, testing]
5157
default: development
5258
````
5359

54-
## `src/verify_command.sh`
55-
56-
````bash
57-
echo "# this file is located in 'src/verify_command.sh'"
58-
echo "# code for 'cli verify' goes here"
59-
echo "# you can edit it freely and regenerate (it will not be overwritten)"
60-
inspect_args
61-
62-
echo "environment:"
63-
echo "- API_KEY=${API_KEY:-}"
64-
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
65-
echo "- MY_SECRET=${MY_SECRET:-}"
66-
67-
````
6860

6961

7062
## Output
@@ -133,8 +125,13 @@ Environment Variables:
133125
MY_SECRET (required)
134126
Your secret
135127

128+
REGION
129+
Cloud region
130+
Default: us-east-2
131+
136132
ENVIRONMENT
137133
One of development, production or test
134+
Allowed: development, production, testing
138135
Default: development
139136

140137

@@ -156,10 +153,12 @@ missing required environment variable: MY_SECRET
156153
# code for 'cli verify' goes here
157154
# you can edit it freely and regenerate (it will not be overwritten)
158155
args: none
159-
environment:
160-
- API_KEY=
161-
- ENVIRONMENT=development
162-
- MY_SECRET=there is no spoon
156+
157+
environment variables:
158+
- $API_KEY =
159+
- $ENVIRONMENT = development
160+
- $MY_SECRET = there is no spoon
161+
- $REGION = us-east-2
163162

164163

165164
````
@@ -171,10 +170,12 @@ environment:
171170
# code for 'cli verify' goes here
172171
# you can edit it freely and regenerate (it will not be overwritten)
173172
args: none
174-
environment:
175-
- API_KEY=
176-
- ENVIRONMENT=production
177-
- MY_SECRET=safe-with-me
173+
174+
environment variables:
175+
- $API_KEY =
176+
- $ENVIRONMENT = production
177+
- $MY_SECRET = safe-with-me
178+
- $REGION = us-east-2
178179

179180

180181
````

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[@]}"

schemas/bashly.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,21 @@
421421
"type": "boolean",
422422
"default": true
423423
},
424+
"environment-variables-allowed-property": {
425+
"title": "allowed",
426+
"description": "Valid values of the current environment variable\nhttps://bashly.dannyb.co/configuration/environment-variable/#allowed",
427+
"type": "array",
428+
"minLength": 1,
429+
"uniqueItems": true,
430+
"items": {
431+
"description": "A valid value of the current environment variable",
432+
"type": "string",
433+
"minLength": 1,
434+
"examples": [
435+
"production"
436+
]
437+
}
438+
},
424439
"environment-variables-property": {
425440
"title": "environment variables",
426441
"description": "Environment variables of the current application\nhttps://bashly.dannyb.co/configuration/environment-variable/#environment-variable",
@@ -448,6 +463,9 @@
448463
},
449464
"required": {
450465
"$ref": "#/definitions/environment-variables-required-property"
466+
},
467+
"allowed": {
468+
"$ref": "#/definitions/environment-variables-allowed-property"
451469
}
452470
},
453471
"if": {
@@ -473,6 +491,9 @@
473491
},
474492
"required": {
475493
"$ref": "#/definitions/environment-variables-required-property"
494+
},
495+
"allowed": {
496+
"$ref": "#/definitions/environment-variables-allowed-property"
476497
}
477498
},
478499
"patternProperties": {

0 commit comments

Comments
 (0)