Skip to content

Commit

Permalink
Merge pull request #546 from DannyBen/add/environment-variable-valida…
Browse files Browse the repository at this point in the history
…tion

Add support for environment variable validations
  • Loading branch information
DannyBen authored Aug 14, 2024
2 parents c598041 + 72efef7 commit 6f3ed4a
Show file tree
Hide file tree
Showing 27 changed files with 533 additions and 280 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
- [yaml](yaml#readme) - using the YAML reading functions
- [colors](colors#readme) - using the color print feature
- [completions](completions#readme) - adding bash completion functionality
- [validations](validations#readme) - adding argument validation functions
- [validations](validations#readme) - adding validation functions for arguments, flags or environment variables
- [hooks](hooks#readme) - adding before/after hooks

## Real-world-like examples
Expand Down
4 changes: 2 additions & 2 deletions examples/needs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ Options:
### `$ ./cli --add deploy`

````shell
--add requires --command
--add needs --command


````

### `$ ./cli --add deploy --command 'git push'`

````shell
--add requires --target
--add needs --target


````
Expand Down
32 changes: 32 additions & 0 deletions examples/validations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ commands:

# Validations also work on flags (when they have arguments)
validate: file_exists

- name: build
environment_variables:
- name: build_dir
help: Path to the build directory
default: release

# Validations also work on environment variables
validate: dir_exists
````


Expand Down Expand Up @@ -97,5 +106,28 @@ must be an existing file

````

### `$ ./validate build`

````shell
validation error in environment variable BUILD_DIR:
must be an existing directory


````

### `$ BUILD_DIR=src ./validate build`

````shell
# this file is located in 'src/build_command.sh'
# code for 'validate build' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args: none

environment variables:
- $BUILD_DIR = src


````



8 changes: 8 additions & 0 deletions examples/validations/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ commands:
# Validations also work on flags (when they have arguments)
validate: file_exists

- name: build
environment_variables:
- name: build_dir
help: Path to the build directory
default: release

# Validations also work on environment variables
validate: dir_exists
4 changes: 4 additions & 0 deletions examples/validations/src/build_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "# this file is located in 'src/build_command.sh'"
echo "# code for 'validate build' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
3 changes: 3 additions & 0 deletions examples/validations/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ bashly generate
./validate calc A
./validate calc 1 B
./validate calc 1 2 --save no-such-file.txt

./validate build
BUILD_DIR=src ./validate build
10 changes: 10 additions & 0 deletions lib/bashly/docs/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ environment_variable.required:
help: Specify that this variable is required.
url: https://bashly.dannyb.co/configuration/environment-variable/#required
example: |-
environment_variables:
- name: api_key
help: Your API key
required: true
environment_variable.validate:
help: Apply custom validation functions.

url: https://bashly.dannyb.co/configuration/environment-variable/#validate
example: |-
environment_variables:
- name: build_dir
validate: dir_exists
1 change: 1 addition & 0 deletions lib/bashly/libraries/strings/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ disallowed_argument: "%{name} must be one of: %{allowed}"
disallowed_environment_variable: "%{name} environment variable must be one of: %{allowed}"
unsupported_bash_version: "bash version 4 or higher is required"
validation_error: "validation error in %s:\\n%s"
environment_variable_validation_error: "validation error in environment variable %s:\\n%s"
4 changes: 3 additions & 1 deletion lib/bashly/script/environment_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Script
class EnvironmentVariable < Base
class << self
def option_keys
@option_keys ||= %i[allowed default help name required private]
@option_keys ||= %i[
allowed default help name required private validate
]
end
end

Expand Down
7 changes: 6 additions & 1 deletion lib/bashly/script/introspection/environment_variables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Bashly
module Script
module Introspection
module EnvironmentVariables
# Returns an array of all the default Environment Variables
# Returns an array of all the Environment Variables with default values
def default_environment_variables
environment_variables.select(&:default)
end
Expand All @@ -26,6 +26,11 @@ def required_environment_variables
environment_variables.select(&:required)
end

# Returns an array of all the environment_variables with a validation
def validated_environment_variables
environment_variables.select(&:validate)
end

# Returns an array of all the environment_variables with a whitelist arg
def whitelisted_environment_variables
environment_variables.select(&:allowed)
Expand Down
10 changes: 5 additions & 5 deletions lib/bashly/script/introspection/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def global_flags?
flags.any? and commands.any?
end

# Returns an array of all fpags that need other flags
def needy_flags
flags.select(&:needs)
end

# Returns only flags that are not private
def public_flags
flags.reject(&:private)
Expand All @@ -32,11 +37,6 @@ def required_flags
flags.select(&:required)
end

# Returns an array of all fpags that need other flags
def needy_flags
flags.select(&:needs)
end

# Returns true if one of the flags matches the provided short code
def short_flag_exist?(flag)
flags.any? { |f| f.short == flag }
Expand Down
6 changes: 6 additions & 0 deletions lib/bashly/views/command/environment_variables_filter.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ if whitelisted_environment_variables.any?
> fi
end
end

if validated_environment_variables.any?
validated_environment_variables.each do |env_var|
= env_var.render(:validations)
end
end
9 changes: 9 additions & 0 deletions lib/bashly/views/environment_variable/validations.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if validate
= view_marker

> if [[ -v {{ name.upcase }} && -n $(validate_{{ validate }} "${{ name.upcase }}") ]]; then
> printf "{{ strings[:environment_variable_validation_error] }}\n" "{{ usage_string }}" "$(validate_{{ validate }} "${{ name.upcase }}")" >&2
> exit 1
> fi
>
end
11 changes: 11 additions & 0 deletions schemas/bashly.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@
"type": "boolean",
"default": true
},
"validate": {
"title": "validate",
"description": "A validation function for the current environment variable\nhttps://bashly.dannyb.co/configuration/environment-variable/#validate",
"type": "string",
"examples": [
"file_exists",
"dir_exists",
"integer",
"non_empty"
]
},
"allowed": {
"title": "allowed",
"description": "Valid values of the current environment variable\nhttps://bashly.dannyb.co/configuration/environment-variable/#allowed",
Expand Down
7 changes: 7 additions & 0 deletions schemas/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@
"type": "string",
"minLength": 1,
"default": "validation error in %s:\\n%s"
},
"environment_variable_validation_error": {
"title": "environment variable validation error",
"description": "The error message template for failed custom validation for environment variables\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
"type": "string",
"minLength": 1,
"default": "validation error in environment variable %s:\\n%s"
}
},
"additionalProperties": false
Expand Down
11 changes: 11 additions & 0 deletions spec/approvals/cli/doc/full
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,23 @@ environment_variable.required

Specify that this variable is required.

environment_variables:
- name: api_key
help: Your API key
required: true

See https://bashly.dannyb.co/configuration/environment-variable/#required

environment_variable.validate

Apply custom validation functions.

environment_variables:
- name: build_dir
validate: dir_exists

See https://bashly.dannyb.co/configuration/environment-variable/#validate

flag

Define option flags.
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/doc/index
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ environment_variable.help
environment_variable.name
environment_variable.private
environment_variable.required
environment_variable.validate
flag
flag.allowed
flag.arg
Expand Down
13 changes: 13 additions & 0 deletions spec/approvals/examples/validations
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ created src/lib/validations/validate_not_empty.sh
+ bashly generate
creating user files in src
created src/calc_command.sh
created src/build_command.sh
created ./validate
run ./validate --help to test your bash script
+ ./validate calc 1 2 --save README.md
Expand All @@ -25,3 +26,15 @@ must be an integer
+ ./validate calc 1 2 --save no-such-file.txt
validation error in --save PATH:
must be an existing file
+ ./validate build
validation error in environment variable BUILD_DIR:
must be an existing directory
+ BUILD_DIR=src
+ ./validate build
# this file is located in 'src/build_command.sh'
# code for 'validate build' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args: none

environment variables:
- $BUILD_DIR = src
Loading

0 comments on commit 6f3ed4a

Please sign in to comment.