Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/validations/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
calc
validate
4 changes: 0 additions & 4 deletions examples/validations/src/add_command.sh

This file was deleted.

14 changes: 11 additions & 3 deletions examples/validations/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: calc
name: validate
help: Sample application demonstrating validations
version: 0.1.0

commands:
- name: add
short: a
- name: calc
help: Add two numbers

args:
Expand All @@ -31,3 +30,12 @@ commands:

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

# Additional validation examples
- name: cat
help: Show a file
args:
- name: path
help: Path to file
required: true
validate: file_exists
4 changes: 4 additions & 0 deletions examples/validations/src/calc_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "# this file is located in 'src/calc_command.sh'"
echo "# code for 'validate calc' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
4 changes: 4 additions & 0 deletions examples/validations/src/cat_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "# this file is located in 'src/cat_command.sh'"
echo "# code for 'validate cat' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_file_exists() {
[[ -f "$1" ]] || echo "must be an existing file"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_not_empty() {
[[ -z "$1" ]] && echo "must not be empty"
}
12 changes: 7 additions & 5 deletions examples/validations/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ bashly generate

### Try Me ###

./calc -h
./calc add 1 2 --multiply 3
./calc add A
./calc add 1 B
./calc add 1 2 --multiply C
./validate -h
./validate calc 1 2 --multiply 3
./validate calc A
./validate calc 1 B
./validate calc 1 2 --multiply C
./validate cat .gitignore
./validate cat no-such-file


3 changes: 3 additions & 0 deletions lib/bashly/templates/lib/validations/validate_file_exists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_file_exists() {
[[ -f "$1" ]] || echo "must be an existing file"
}
3 changes: 3 additions & 0 deletions lib/bashly/templates/lib/validations/validate_not_empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_not_empty() {
[[ -z "$1" ]] && echo "must not be empty"
}
2 changes: 1 addition & 1 deletion lib/bashly/templates/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ missing_dependency: "missing dependency: %{dependency}"
disallowed_flag: "%{name} must be one of: %{allowed}"
disallowed_argument: "%{name} must be one of: %{allowed}"
unsupported_bash_version: "bash version 4 or higher is required"
validation_error: "validation error: %s"
validation_error: "validation error in %s:\\n%s"
2 changes: 1 addition & 1 deletion lib/bashly/views/argument/validations.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% validations.each do |validation_function|
if [[ -n $(validate_<%= validation_function %> "$1") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %> $(validate_<%= validation_function %> "$1")"
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %>" "$(validate_<%= validation_function %> "$1")"
exit 1
fi
% end
2 changes: 1 addition & 1 deletion lib/bashly/views/flag/validations.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% validations.each do |validation_function|
if [[ -n $(validate_<%= validation_function %> "$2") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= usage_string %> $(validate_<%= validation_function %> "$2")"
printf "<%= strings[:validation_error] %>\n" "<%= usage_string %>" "$(validate_<%= validation_function %> "$2")"
exit 1
fi
% end
2 changes: 2 additions & 0 deletions spec/approvals/cli/add/validations
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
created spec/tmp/src/lib/validations/validate_file_exists.sh
created spec/tmp/src/lib/validations/validate_integer.sh
created spec/tmp/src/lib/validations/validate_not_empty.sh
52 changes: 34 additions & 18 deletions spec/approvals/examples/validations
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
+ bashly add validations
created src/lib/validations/validate_file_exists.sh
created src/lib/validations/validate_integer.sh
created src/lib/validations/validate_not_empty.sh
+ bashly generate
creating user files in src
created src/initialize.sh
created src/add_command.sh
created ./calc
run ./calc --help to test your bash script
+ ./calc -h
calc - Sample application demonstrating validations
created src/calc_command.sh
created src/cat_command.sh
created ./validate
run ./validate --help to test your bash script
+ ./validate -h
validate - Sample application demonstrating validations

Usage:
calc [command]
calc [command] --help | -h
calc --version | -v
validate [command]
validate [command] --help | -h
validate --version | -v

Commands:
add Add two numbers
calc Add two numbers
cat Show a file

Options:
--help, -h
Expand All @@ -24,17 +28,29 @@ Options:
--version, -v
Show version number

+ ./calc add 1 2 --multiply 3
# this file is located in 'src/add_command.sh'
# code for 'calc add' goes here
+ ./validate calc 1 2 --multiply 3
# this file is located in 'src/calc_command.sh'
# code for 'validate calc' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[first]} = 1
- ${args[--multiply]} = 3
- ${args[second]} = 2
+ ./calc add A
validation error: FIRST must be an integer
+ ./calc add 1 B
validation error: SECOND must be an integer
+ ./calc add 1 2 --multiply C
validation error: --multiply, -m FACTOR must be an integer
+ ./validate calc A
validation error in FIRST:
must be an integer
+ ./validate calc 1 B
validation error in SECOND:
must be an integer
+ ./validate calc 1 2 --multiply C
validation error in --multiply, -m FACTOR:
must be an integer
+ ./validate cat .gitignore
# this file is located in 'src/cat_command.sh'
# code for 'validate cat' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[path]} = .gitignore
+ ./validate cat no-such-file
validation error in PATH:
must be an existing file