Skip to content

Commit f0742b7

Browse files
authored
Merge pull request #136 from DannyBen/add/validations
Add more custom validations
2 parents 7187c48 + 30f3339 commit f0742b7

File tree

29 files changed

+203
-149
lines changed

29 files changed

+203
-149
lines changed

examples/validations/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
calc
1+
validate

examples/validations/README.md

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,103 +22,77 @@ failure, or an empty string on success.
2222
## `bashly.yml`
2323

2424
```yaml
25-
name: calc
25+
name: validate
2626
help: Sample application demonstrating validations
2727
version: 0.1.0
2828

2929
commands:
30-
- name: add
31-
short: a
30+
- name: calc
3231
help: Add two numbers
3332

3433
args:
3534
- name: first
3635
help: First number
3736
required: true
3837

39-
# Specify one or more validation types (as string or array)
40-
# This validation will look for a function named `validate_integer` in your
41-
# script.
38+
# Specify a validation function.
39+
# Bashly will look for a function named `validate_integer` in your
40+
# script, you can use any name as long as it has a matching function.
4241
validate: integer
4342
- name: second
4443
help: Second number
45-
46-
# Using the array syntax, you can specify more than one validations
47-
validate:
48-
- integer
44+
validate: integer
4945

5046
flags:
51-
- long: --multiply
52-
short: -m
53-
arg: factor
54-
help: Multiply the result
47+
- long: --save
48+
arg: path
49+
help: Save the result to a file
5550

5651
# Validations also work on flags (when they have arguments)
57-
validate: integer
52+
validate: file_exists
5853
```
5954
6055
6156
6257
## Generated script output
6358
64-
### `$ ./calc -h`
65-
66-
```shell
67-
calc - Sample application demonstrating validations
68-
69-
Usage:
70-
calc [command]
71-
calc [command] --help | -h
72-
calc --version | -v
73-
74-
Commands:
75-
add Add two numbers
76-
77-
Options:
78-
--help, -h
79-
Show this help
80-
81-
--version, -v
82-
Show version number
83-
84-
85-
86-
```
87-
88-
### `$ ./calc add 1 2 --multiply 3`
59+
### `$ ./validate calc 1 2 --save README.md`
8960

9061
```shell
91-
# this file is located in 'src/add_command.sh'
92-
# code for 'calc add' goes here
62+
# this file is located in 'src/calc_command.sh'
63+
# code for 'validate calc' goes here
9364
# you can edit it freely and regenerate (it will not be overwritten)
9465
args:
9566
- ${args[first]} = 1
96-
- ${args[--multiply]} = 3
67+
- ${args[--save]} = README.md
9768
- ${args[second]} = 2
9869
9970
10071
```
10172

102-
### `$ ./calc add A`
73+
### `$ ./validate calc A`
10374

10475
```shell
105-
validation error: FIRST must be an integer
76+
validation error in FIRST:
77+
must be an integer
10678
10779
10880
```
10981

110-
### `$ ./calc add 1 B`
82+
### `$ ./validate calc 1 B`
11183

11284
```shell
113-
validation error: SECOND must be an integer
85+
validation error in SECOND:
86+
must be an integer
11487
11588
11689
```
11790

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

12093
```shell
121-
validation error: --multiply, -m FACTOR must be an integer
94+
validation error in --save PATH:
95+
must be an existing file
12296
12397
12498
```

examples/validations/src/add_command.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/validations/src/bashly.yml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
1-
name: calc
1+
name: validate
22
help: Sample application demonstrating validations
33
version: 0.1.0
44

55
commands:
6-
- name: add
7-
short: a
6+
- name: calc
87
help: Add two numbers
98

109
args:
1110
- name: first
1211
help: First number
1312
required: true
1413

15-
# Specify one or more validation types (as string or array)
16-
# This validation will look for a function named `validate_integer` in your
17-
# script.
14+
# Specify a validation function.
15+
# Bashly will look for a function named `validate_integer` in your
16+
# script, you can use any name as long as it has a matching function.
1817
validate: integer
1918
- name: second
2019
help: Second number
21-
22-
# Using the array syntax, you can specify more than one validations
23-
validate:
24-
- integer
20+
validate: integer
2521

2622
flags:
27-
- long: --multiply
28-
short: -m
29-
arg: factor
30-
help: Multiply the result
23+
- long: --save
24+
arg: path
25+
help: Save the result to a file
3126

3227
# Validations also work on flags (when they have arguments)
33-
validate: integer
28+
validate: file_exists
29+
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/calc_command.sh'"
2+
echo "# code for 'validate calc' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_dir_exists() {
2+
[[ -d "$1" ]] || echo "must be an existing directory"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_file_exists() {
2+
[[ -f "$1" ]] || echo "must be an existing file"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate_not_empty() {
2+
[[ -z "$1" ]] && echo "must not be empty"
3+
}

examples/validations/test.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ rm -rf ./src/lib
55

66
set -x
77

8-
bashly add validations
8+
bashly add validations --force
99
bashly generate
1010

1111
### Try Me ###
1212

13-
./calc -h
14-
./calc add 1 2 --multiply 3
15-
./calc add A
16-
./calc add 1 B
17-
./calc add 1 2 --multiply C
18-
19-
13+
./validate calc 1 2 --save README.md
14+
./validate calc A
15+
./validate calc 1 B
16+
./validate calc 1 2 --save no-such-file.txt

lib/bashly/commands/add.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def comp_command
7575
private
7676

7777
def safe_copy_dir(dir)
78-
Dir[asset("templates/lib/#{dir}/*.sh")].each do |file|
78+
Dir[asset("templates/lib/#{dir}/*.sh")].sort.each do |file|
7979
safe_copy_file "#{dir}/#{File.basename file}"
8080
end
8181
end

0 commit comments

Comments
 (0)