Skip to content

Commit bccd3b0

Browse files
committed
- Add support for environment variable validations
1 parent c598041 commit bccd3b0

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def required_environment_variables
3030
def whitelisted_environment_variables
3131
environment_variables.select(&:allowed)
3232
end
33+
34+
# Returns an array of all the environment_variables with a validation
35+
def validated_environment_variables
36+
environment_variables.select(&:validate)
37+
end
3338
end
3439
end
3540
end

lib/bashly/views/command/environment_variables_filter.gtx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ if whitelisted_environment_variables.any?
2424
> fi
2525
end
2626
end
27+
28+
if validated_environment_variables.any?
29+
validated_environment_variables.each do |env_var|
30+
= env_var.render(:validations)
31+
end
32+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if validate
2+
= view_marker
3+
4+
> if [[ -v {{ name.upcase }} && -n $(validate_{{ validate }} "${{ name.upcase }}") ]]; then
5+
> printf "{{ strings[:environment_variable_validation_error] }}\n" "{{ usage_string }}" "$(validate_{{ validate }} "${{ name.upcase }}")" >&2
6+
> exit 1
7+
> fi
8+
>
9+
end

0 commit comments

Comments
 (0)