Skip to content

Commit

Permalink
Merge pull request #474 from DannyBen/refactor/env-vars-inspect-args
Browse files Browse the repository at this point in the history
Refactor `inspect_args` handling of environment variables
  • Loading branch information
DannyBen authored Dec 22, 2023
2 parents ccf73dc + 423a271 commit a96044c
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 74 deletions.
6 changes: 6 additions & 0 deletions examples/command-aliases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ Examples:
args:
- ${args[source]} = somefile

environment variables:
- $API_KEY =


````

Expand Down Expand Up @@ -215,6 +218,9 @@ Examples:
args:
- ${args[source]} = somefile

environment variables:
- $API_KEY =


````

Expand Down
7 changes: 7 additions & 0 deletions examples/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ args:
- ${args[source]} = sourcefile
- ${args[target]} = targetfile

environment variables:
- $API_KEY =
- $DEFAULT_TARGET_LOCATION =


````

Expand Down Expand Up @@ -235,6 +239,9 @@ args:
- ${args[source]} = sourcefile
- ${args[--user]} = username

environment variables:
- $API_KEY =


````

Expand Down
49 changes: 25 additions & 24 deletions examples/environment-variables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ $ bashly generate
$ bashly generate
```

<!-- include: src/verify_command.sh -->

-----

## `bashly.yml`
Expand Down Expand Up @@ -46,25 +44,19 @@ commands:

# Using the `default: value` option will cause the value to variable to be
# set if it is not provided by the user.
- name: region
help: Cloud region
default: us-east-2

# Using the `allowed: [value1, value2]` option will halt the script's
# execution with a friendly error message, unless the variable matches one
# of the defined values.
- name: environment
help: One of development, production or test
allowed: [development, production, testing]
default: development
````

## `src/verify_command.sh`

````bash
echo "# this file is located in 'src/verify_command.sh'"
echo "# code for 'cli verify' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args

echo "environment:"
echo "- API_KEY=${API_KEY:-}"
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
echo "- MY_SECRET=${MY_SECRET:-}"

````


## Output
Expand Down Expand Up @@ -133,8 +125,13 @@ Environment Variables:
MY_SECRET (required)
Your secret

REGION
Cloud region
Default: us-east-2

ENVIRONMENT
One of development, production or test
Allowed: development, production, testing
Default: development


Expand All @@ -156,10 +153,12 @@ missing required environment variable: MY_SECRET
# code for 'cli verify' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args: none
environment:
- API_KEY=
- ENVIRONMENT=development
- MY_SECRET=there is no spoon

environment variables:
- $API_KEY =
- $ENVIRONMENT = development
- $MY_SECRET = there is no spoon
- $REGION = us-east-2


````
Expand All @@ -171,10 +170,12 @@ environment:
# code for 'cli verify' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args: none
environment:
- API_KEY=
- ENVIRONMENT=production
- MY_SECRET=safe-with-me

environment variables:
- $API_KEY =
- $ENVIRONMENT = production
- $MY_SECRET = safe-with-me
- $REGION = us-east-2


````
Expand Down
8 changes: 8 additions & 0 deletions examples/environment-variables/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ commands:

# Using the `default: value` option will cause the value to variable to be
# set if it is not provided by the user.
- name: region
help: Cloud region
default: us-east-2

# Using the `allowed: [value1, value2]` option will halt the script's
# execution with a friendly error message, unless the variable matches one
# of the defined values.
- name: environment
help: One of development, production or test
allowed: [development, production, testing]
default: development
5 changes: 0 additions & 5 deletions examples/environment-variables/src/verify_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ echo "# this file is located in 'src/verify_command.sh'"
echo "# code for 'cli verify' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args

echo "environment:"
echo "- API_KEY=${API_KEY:-}"
echo "- ENVIRONMENT=${ENVIRONMENT:-}"
echo "- MY_SECRET=${MY_SECRET:-}"
3 changes: 3 additions & 0 deletions examples/multiline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ Examples:
# you can edit it freely and regenerate (it will not be overwritten)
args: none

environment variables:
- $MULTI_VITAMIN =


````

Expand Down
40 changes: 21 additions & 19 deletions lib/bashly/views/command/environment_variables_filter.gtx
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
if default_environment_variables.any? || required_environment_variables.any? ||
whitelisted_environment_variables.any?

= view_marker
if environment_variables.any?
= view_marker
= render(:environment_variables_default)

if required_environment_variables.any?
required_environment_variables.each do |env_var|
> if [[ -z "${<%= env_var.name.upcase %>:-}" ]]; then
> printf "{{ strings[:missing_required_environment_variable] % { var: env_var.name.upcase } }}\n" >&2
> exit 1
> fi
end
environment_variables.each do |env_var|
> env_var_names+=("{{ env_var.name.upcase }}")
end
end

if required_environment_variables.any?
required_environment_variables.each do |env_var|
> if [[ -z "${<%= env_var.name.upcase %>:-}" ]]; then
> printf "{{ strings[:missing_required_environment_variable] % { var: env_var.name.upcase } }}\n" >&2
> exit 1
> fi
end
end

if whitelisted_environment_variables.any?
whitelisted_environment_variables.each do |env_var|
> if [[ -n "${<%= env_var.name.upcase %>:-}" ]] && [[ ! ${<%= env_var.name.upcase %>:-} =~ ^({{ env_var.allowed.join '|' }})$ ]]; then
> printf "%s\n" "{{ strings[:disallowed_environment_variable] % { name: env_var.name.upcase, allowed: env_var.allowed.join(', ') } }}" >&2
> exit 1
> fi
end
if whitelisted_environment_variables.any?
whitelisted_environment_variables.each do |env_var|
> if [[ -n "${<%= env_var.name.upcase %>:-}" ]] && [[ ! ${<%= env_var.name.upcase %>:-} =~ ^({{ env_var.allowed.join '|' }})$ ]]; then
> printf "%s\n" "{{ strings[:disallowed_environment_variable] % { name: env_var.name.upcase, allowed: env_var.allowed.join(', ') } }}" >&2
> exit 1
> fi
end
end
end
24 changes: 14 additions & 10 deletions lib/bashly/views/command/inspect_args.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
> if ((${#args[@]})); then
> readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
> echo args:
> for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
> for k in "${sorted_keys[@]}"; do
> echo "- \${args[$k]} = ${args[$k]}"
> done
> else
> echo args: none
> fi
Expand All @@ -22,16 +24,18 @@
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
> echo
> echo deps:
> for k in "${sorted_keys[@]}"; do echo "- \${deps[$k]} = ${deps[$k]}"; done
> for k in "${sorted_keys[@]}"; do
> echo "- \${deps[$k]} = ${deps[$k]}"
> done
> fi
>
if environment_variables.any?
> echo
> echo environment variables:
environment_variables.each do |env_var|
> echo "- \${{ env_var.name.upcase }} = ${<%= env_var.name.upcase %>:-unset}"
end
>
end
> if ((${#env_var_names[@]})); then
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
> echo
> echo "environment variables:"
> for k in "${sorted_names[@]}"; do
> echo "- \$$k = ${!k:-}"
> done
> fi
> }
>
1 change: 1 addition & 0 deletions lib/bashly/views/command/run.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
> declare -A args=()
> declare -A deps=()
> declare -a other_args=()
> declare -a env_var_names=()
> declare -a input=()
> normalize_input "$@"
> parse_requirements "${input[@]}"
Expand Down
21 changes: 21 additions & 0 deletions schemas/bashly.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,21 @@
"type": "boolean",
"default": true
},
"environment-variables-allowed-property": {
"title": "allowed",
"description": "Valid values of the current environment variable\nhttps://bashly.dannyb.co/configuration/environment-variable/#allowed",
"type": "array",
"minLength": 1,
"uniqueItems": true,
"items": {
"description": "A valid value of the current environment variable",
"type": "string",
"minLength": 1,
"examples": [
"production"
]
}
},
"environment-variables-property": {
"title": "environment variables",
"description": "Environment variables of the current application\nhttps://bashly.dannyb.co/configuration/environment-variable/#environment-variable",
Expand Down Expand Up @@ -448,6 +463,9 @@
},
"required": {
"$ref": "#/definitions/environment-variables-required-property"
},
"allowed": {
"$ref": "#/definitions/environment-variables-allowed-property"
}
},
"if": {
Expand All @@ -473,6 +491,9 @@
},
"required": {
"$ref": "#/definitions/environment-variables-required-property"
},
"allowed": {
"$ref": "#/definitions/environment-variables-allowed-property"
}
},
"patternProperties": {
Expand Down
4 changes: 2 additions & 2 deletions spec/approvals/examples/command-aliases
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ args:
- ${args[source]} = somefile

environment variables:
- $API_KEY = unset
- $API_KEY =
+ ./cli upload --help
cli upload - Upload a file

Expand Down Expand Up @@ -124,4 +124,4 @@ args:
- ${args[source]} = somefile

environment variables:
- $API_KEY = unset
- $API_KEY =
5 changes: 3 additions & 2 deletions spec/approvals/examples/commands
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ args:
- ${args[target]} = targetfile

environment variables:
- $API_KEY = unset
- $API_KEY =
- $DEFAULT_TARGET_LOCATION =
+ ./cli upload --help
cli upload - Upload a file

Expand Down Expand Up @@ -120,4 +121,4 @@ args:
- ${args[--user]} = username

environment variables:
- $API_KEY = unset
- $API_KEY =
23 changes: 13 additions & 10 deletions spec/approvals/examples/environment-variables
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ Environment Variables:
MY_SECRET (required)
Your secret

REGION
Cloud region
Default: us-east-2

ENVIRONMENT
One of development, production or test
Allowed: development, production, testing
Default: development

+ ./cli verify
Expand All @@ -67,11 +72,10 @@ missing required environment variable: MY_SECRET
args: none

environment variables:
- $API_KEY = unset
environment:
- API_KEY=
- ENVIRONMENT=development
- MY_SECRET=there is no spoon
- $API_KEY =
- $ENVIRONMENT = development
- $MY_SECRET = there is no spoon
- $REGION = us-east-2
+ ENVIRONMENT=production
+ MY_SECRET=safe-with-me
+ ./cli verify
Expand All @@ -81,8 +85,7 @@ environment:
args: none

environment variables:
- $API_KEY = unset
environment:
- API_KEY=
- ENVIRONMENT=production
- MY_SECRET=safe-with-me
- $API_KEY =
- $ENVIRONMENT = production
- $MY_SECRET = safe-with-me
- $REGION = us-east-2
2 changes: 1 addition & 1 deletion spec/approvals/examples/multiline
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Examples:
args: none

environment variables:
- $MULTI_VITAMIN = unset
- $MULTI_VITAMIN =
+ ./multi multiline -h
multi multiline

Expand Down
1 change: 1 addition & 0 deletions spec/approvals/fixtures/environment-variables-initialize
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ args:
environment variables:
- $API_KEY = must-be-available-in-initialize
- $CONFIG_PATH = somepath
- $NESTED_VAR = not-available-in-initialize
+ export API_KEY=override-value
+ API_KEY=override-value
+ ./cli
Expand Down
3 changes: 2 additions & 1 deletion spec/approvals/fixtures/partials-extension
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ args:
- ${args[source]} = something

environment variables:
- $API_KEY = unset
- $API_KEY =
- $DEFAULT_TARGET_LOCATION =
Loading

0 comments on commit a96044c

Please sign in to comment.