Skip to content

Commit 6f3ed4a

Browse files
authored
Merge pull request #546 from DannyBen/add/environment-variable-validation
Add support for environment variable validations
2 parents c598041 + 72efef7 commit 6f3ed4a

File tree

27 files changed

+533
-280
lines changed

27 files changed

+533
-280
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
6161
- [yaml](yaml#readme) - using the YAML reading functions
6262
- [colors](colors#readme) - using the color print feature
6363
- [completions](completions#readme) - adding bash completion functionality
64-
- [validations](validations#readme) - adding argument validation functions
64+
- [validations](validations#readme) - adding validation functions for arguments, flags or environment variables
6565
- [hooks](hooks#readme) - adding before/after hooks
6666

6767
## Real-world-like examples

examples/needs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ Options:
8383
### `$ ./cli --add deploy`
8484

8585
````shell
86-
--add requires --command
86+
--add needs --command
8787

8888

8989
````
9090

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

9393
````shell
94-
--add requires --target
94+
--add needs --target
9595

9696

9797
````

examples/validations/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ commands:
5050

5151
# Validations also work on flags (when they have arguments)
5252
validate: file_exists
53+
54+
- name: build
55+
environment_variables:
56+
- name: build_dir
57+
help: Path to the build directory
58+
default: release
59+
60+
# Validations also work on environment variables
61+
validate: dir_exists
5362
````
5463

5564

@@ -97,5 +106,28 @@ must be an existing file
97106

98107
````
99108

109+
### `$ ./validate build`
110+
111+
````shell
112+
validation error in environment variable BUILD_DIR:
113+
must be an existing directory
114+
115+
116+
````
117+
118+
### `$ BUILD_DIR=src ./validate build`
119+
120+
````shell
121+
# this file is located in 'src/build_command.sh'
122+
# code for 'validate build' goes here
123+
# you can edit it freely and regenerate (it will not be overwritten)
124+
args: none
125+
126+
environment variables:
127+
- $BUILD_DIR = src
128+
129+
130+
````
131+
100132

101133

examples/validations/src/bashly.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ commands:
2727
# Validations also work on flags (when they have arguments)
2828
validate: file_exists
2929

30+
- name: build
31+
environment_variables:
32+
- name: build_dir
33+
help: Path to the build directory
34+
default: release
35+
36+
# Validations also work on environment variables
37+
validate: dir_exists
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/build_command.sh'"
2+
echo "# code for 'validate build' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

examples/validations/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ bashly generate
1414
./validate calc A
1515
./validate calc 1 B
1616
./validate calc 1 2 --save no-such-file.txt
17+
18+
./validate build
19+
BUILD_DIR=src ./validate build

lib/bashly/docs/env.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ environment_variable.required:
4949
help: Specify that this variable is required.
5050
url: https://bashly.dannyb.co/configuration/environment-variable/#required
5151
example: |-
52+
environment_variables:
5253
- name: api_key
5354
help: Your API key
5455
required: true
56+
57+
environment_variable.validate:
58+
help: Apply custom validation functions.
59+
60+
url: https://bashly.dannyb.co/configuration/environment-variable/#validate
61+
example: |-
62+
environment_variables:
63+
- name: build_dir
64+
validate: dir_exists

lib/bashly/libraries/strings/strings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ disallowed_argument: "%{name} must be one of: %{allowed}"
4242
disallowed_environment_variable: "%{name} environment variable must be one of: %{allowed}"
4343
unsupported_bash_version: "bash version 4 or higher is required"
4444
validation_error: "validation error in %s:\\n%s"
45+
environment_variable_validation_error: "validation error in environment variable %s:\\n%s"

lib/bashly/script/environment_variable.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module Script
33
class EnvironmentVariable < Base
44
class << self
55
def option_keys
6-
@option_keys ||= %i[allowed default help name required private]
6+
@option_keys ||= %i[
7+
allowed default help name required private validate
8+
]
79
end
810
end
911

lib/bashly/script/introspection/environment_variables.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Bashly
22
module Script
33
module Introspection
44
module EnvironmentVariables
5-
# Returns an array of all the default Environment Variables
5+
# Returns an array of all the Environment Variables with default values
66
def default_environment_variables
77
environment_variables.select(&:default)
88
end
@@ -26,6 +26,11 @@ def required_environment_variables
2626
environment_variables.select(&:required)
2727
end
2828

29+
# Returns an array of all the environment_variables with a validation
30+
def validated_environment_variables
31+
environment_variables.select(&:validate)
32+
end
33+
2934
# Returns an array of all the environment_variables with a whitelist arg
3035
def whitelisted_environment_variables
3136
environment_variables.select(&:allowed)

0 commit comments

Comments
 (0)