Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 23 additions & 49 deletions examples/validations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,103 +22,77 @@ failure, or an empty string on success.
## `bashly.yml`

```yaml
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:
- name: first
help: First number
required: true

# Specify one or more validation types (as string or array)
# This validation will look for a function named `validate_integer` in your
# script.
# Specify a validation function.
# Bashly will look for a function named `validate_integer` in your
# script, you can use any name as long as it has a matching function.
validate: integer
- name: second
help: Second number

# Using the array syntax, you can specify more than one validations
validate:
- integer
validate: integer

flags:
- long: --multiply
short: -m
arg: factor
help: Multiply the result
- long: --save
arg: path
help: Save the result to a file

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



## Generated script output

### `$ ./calc -h`

```shell
calc - Sample application demonstrating validations

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

Commands:
add Add two numbers

Options:
--help, -h
Show this help

--version, -v
Show version number



```

### `$ ./calc add 1 2 --multiply 3`
### `$ ./validate calc 1 2 --save README.md`

```shell
# this file is located in 'src/add_command.sh'
# code for 'calc add' goes here
# 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[--save]} = README.md
- ${args[second]} = 2


```

### `$ ./calc add A`
### `$ ./validate calc A`

```shell
validation error: FIRST must be an integer
validation error in FIRST:
must be an integer


```

### `$ ./calc add 1 B`
### `$ ./validate calc 1 B`

```shell
validation error: SECOND must be an integer
validation error in SECOND:
must be an integer


```

### `$ ./calc add 1 2 --multiply C`
### `$ ./validate calc 1 2 --save no-such-file.txt`

```shell
validation error: --multiply, -m FACTOR must be an integer
validation error in --save PATH:
must be an existing file


```
Expand Down
4 changes: 0 additions & 4 deletions examples/validations/src/add_command.sh

This file was deleted.

26 changes: 11 additions & 15 deletions examples/validations/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
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:
- name: first
help: First number
required: true

# Specify one or more validation types (as string or array)
# This validation will look for a function named `validate_integer` in your
# script.
# Specify a validation function.
# Bashly will look for a function named `validate_integer` in your
# script, you can use any name as long as it has a matching function.
validate: integer
- name: second
help: Second number

# Using the array syntax, you can specify more than one validations
validate:
- integer
validate: integer

flags:
- long: --multiply
short: -m
arg: factor
help: Multiply the result
- long: --save
arg: path
help: Save the result to a file

# Validations also work on flags (when they have arguments)
validate: integer
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_dir_exists() {
[[ -d "$1" ]] || echo "must be an existing directory"
}
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"
}
13 changes: 5 additions & 8 deletions examples/validations/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ rm -rf ./src/lib

set -x

bashly add validations
bashly add validations --force
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 calc 1 2 --save README.md
./validate calc A
./validate calc 1 B
./validate calc 1 2 --save no-such-file.txt
2 changes: 1 addition & 1 deletion lib/bashly/commands/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def comp_command
private

def safe_copy_dir(dir)
Dir[asset("templates/lib/#{dir}/*.sh")].each do |file|
Dir[asset("templates/lib/#{dir}/*.sh")].sort.each do |file|
safe_copy_file "#{dir}/#{File.basename file}"
end
end
Expand Down
9 changes: 0 additions & 9 deletions lib/bashly/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ def help
options['help'] ||= ''
end

def validations
return [] unless options['validate']
if options['validate'].is_a? String
[options['validate']]
else
options['validate']
end
end

def method_missing(method_name, *arguments, &block)
key = method_name.to_s
respond_to?(method_name) ? options[key] : super
Expand Down
3 changes: 3 additions & 0 deletions lib/bashly/templates/lib/validations/validate_dir_exists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
validate_dir_exists() {
[[ -d "$1" ]] || echo "must be an existing directory"
}
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"
10 changes: 6 additions & 4 deletions lib/bashly/views/argument/validations.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
% validations.each do |validation_function|
if [[ -n $(validate_<%= validation_function %> "$1") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %> $(validate_<%= validation_function %> "$1")"
# :argument.validations
% if validate
if [[ -n $(validate_<%= validate %> "$1") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= name.upcase %>" "$(validate_<%= validate %> "$1")"
exit 1
fi
% end

% end
8 changes: 5 additions & 3 deletions lib/bashly/views/flag/validations.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
% validations.each do |validation_function|
if [[ -n $(validate_<%= validation_function %> "$2") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= usage_string %> $(validate_<%= validation_function %> "$2")"
# :flag.validations
% if validate
if [[ -n $(validate_<%= validate %> "$2") ]]; then
printf "<%= strings[:validation_error] %>\n" "<%= usage_string %>" "$(validate_<%= validate %> "$2")"
exit 1
fi

% end
3 changes: 3 additions & 0 deletions spec/approvals/cli/add/validations
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
created spec/tmp/src/lib/validations/validate_dir_exists.sh
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
49 changes: 49 additions & 0 deletions spec/approvals/examples/validation-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
+ bundle exec bashly add validations --force
created src/lib/validations/validate_dir_exists.sh
created src/lib/validations/validate_file_exists.sh
created src/lib/validations/validate_integer.sh
created src/lib/validations/validate_not_empty.sh
+ bundle exec bashly generate
creating user files in src
created src/initialize.sh
created src/root_command.sh
created ./validate
run ./validate --help to test your bash script
+ ./validate 1
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[integer_arg]} = 1
+ ./validate A
validation error in INTEGER_ARG:
must be an integer
+ ./validate 1 README.md
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[file_exists_arg]} = README.md
- ${args[integer_arg]} = 1
+ ./validate 1 no-such-file
validation error in FILE_EXISTS_ARG:
must be an existing file
+ ./validate 1 README.md src
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[dir_exists_arg]} = src
- ${args[file_exists_arg]} = README.md
- ${args[integer_arg]} = 1
+ ./validate 1 README.md no-such-dir
validation error in DIR_EXISTS_ARG:
must be an existing directory
+ ./validate 1 README.md src non-empty
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[dir_exists_arg]} = src
- ${args[file_exists_arg]} = README.md
- ${args[integer_arg]} = 1
- ${args[not_empty_arg]} = non-empty
+ ./validate 1 README.md src ''
validation error in NOT_EMPTY_ARG:
must not be empty
Loading