Skip to content

Commit 558f3f7

Browse files
committed
- Allow command.dependencies to be a hash for custom 'how to install' messages
1 parent 289360e commit 558f3f7

File tree

17 files changed

+140
-41
lines changed

17 files changed

+140
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/doc
66
/Gemfile.lock
77
/gems
8+
/spec/status.txt
89
/spec/tmp
910
/tmp
1011
/toc.txt

examples/command-examples/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ commands:
6666
# over how the examples are displayed. Note the use of the '|-' marker
6767
# that tells YAML to use the string as is, including the newlines it contains.
6868
examples: |-
69-
Upload a file
69+
Upload a file
7070
$ cli upload profile.png -u admin -p s3cr3t
7171
7272
Upload a file (you will be prompted to provide a password)
@@ -137,10 +137,10 @@ Arguments:
137137
138138
Examples:
139139
Upload a file
140-
$ cli upload profile.png --user admin
140+
$ cli upload profile.png -u admin -p s3cr3t
141141
142-
Upload a file and prompt for password
143-
$ cli upload profile.png -u admin -p
142+
Upload a file (you will be prompted to provide a password)
143+
$ cli upload profile.png --user admin
144144
145145
146146

examples/command-examples/src/bashly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ commands:
4949
# over how the examples are displayed. Note the use of the '|-' marker
5050
# that tells YAML to use the string as is, including the newlines it contains.
5151
examples: |-
52-
Upload a file
52+
Upload a file
5353
$ cli upload profile.png -u admin -p s3cr3t
5454
5555
Upload a file (you will be prompted to provide a password)

examples/dependencies/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This example was generated with:
77

88
```bash
99
$ bashly init
10+
$ bashly add colors
1011
# ... now edit src/bashly.yml to match the example ...
1112
$ bashly generate
1213
```
@@ -34,6 +35,12 @@ commands:
3435

3536
- name: upload
3637
help: Upload something
38+
39+
# Dependencies can also be defined as a hash of 'command: message' pairs.
40+
# The message can use colors from the colors library (bashly add colors).
41+
dependencies:
42+
mini-docker: install with $(green gem install mini-docker)
43+
docker: visit https://docker.com for more information
3744
```
3845
3946
@@ -51,10 +58,8 @@ missing dependency: shmurl
5158
### `$ ./cli upload`
5259

5360
```shell
54-
# this file is located in 'src/upload_command.sh'
55-
# code for 'cli upload' goes here
56-
# you can edit it freely and regenerate (it will not be overwritten)
57-
args: none
61+
missing dependency: mini-docker
62+
install with gem install mini-docker
5863
5964
6065
```

examples/dependencies/src/bashly.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ commands:
1616

1717
- name: upload
1818
help: Upload something
19+
20+
# Dependencies can also be defined as a hash of 'command: message' pairs.
21+
# The message can use colors from the colors library (bashly add colors).
22+
dependencies:
23+
mini-docker: install with $(green gem install mini-docker)
24+
docker: visit https://docker.com for more information
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Color functions [@bashly-upgrade colors]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## Use any of the functions below to color or format a portion of a string.
6+
##
7+
## echo "before $(red this is red) after"
8+
## echo "before $(green_bold this is green_bold) after"
9+
##
10+
## Color output will be disabled if `NO_COLOR` environment variable is set
11+
## in compliance with https://no-color.org/
12+
##
13+
print_in_color() {
14+
local color="$1"
15+
shift
16+
if [[ -z ${NO_COLOR+x} ]]; then
17+
printf "$color%b\e[0m\n" "$*";
18+
else
19+
printf "%b\n" "$*";
20+
fi
21+
}
22+
23+
red() { print_in_color "\e[31m" "$*"; }
24+
green() { print_in_color "\e[32m" "$*"; }
25+
yellow() { print_in_color "\e[33m" "$*"; }
26+
blue() { print_in_color "\e[34m" "$*"; }
27+
magenta() { print_in_color "\e[35m" "$*"; }
28+
cyan() { print_in_color "\e[36m" "$*"; }
29+
bold() { print_in_color "\e[1m" "$*"; }
30+
underlined() { print_in_color "\e[4m" "$*"; }
31+
red_bold() { print_in_color "\e[1;31m" "$*"; }
32+
green_bold() { print_in_color "\e[1;32m" "$*"; }
33+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
35+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
38+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
39+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

lib/bashly/concerns/validation_helpers.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,19 @@ def assert_array(key, value, of: nil)
4343
end
4444
end
4545

46-
def assert_hash(key, value, whitelist = nil)
46+
def assert_hash(key, value, keys: nil, of: nil)
4747
assert value.is_a?(Hash), "#{key} must be a hash"
48-
return unless whitelist
4948

50-
invalid_keys = value.keys.map(&:to_sym) - whitelist
51-
assert invalid_keys.empty?, "#{key} contains invalid options: #{invalid_keys.join ', '}"
49+
if keys
50+
invalid_keys = value.keys.map(&:to_sym) - keys
51+
assert invalid_keys.empty?, "#{key} contains invalid options: #{invalid_keys.join ', '}"
52+
end
53+
54+
return unless of
55+
56+
value.each do |k, v|
57+
send "assert_#{of}".to_sym, "#{key}.#{k}", v
58+
end
5259
end
5360

5461
def assert_uniq(key, value, array_keys)

lib/bashly/config_validator.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,26 @@ def assert_catch_all(key, value)
3131
end
3232

3333
def assert_catch_all_hash(key, value)
34-
assert_hash key, value, Script::CatchAll.option_keys
34+
assert_hash key, value, keys: Script::CatchAll.option_keys
3535
assert_string "#{key}.label", value['label']
3636
assert_optional_string "#{key}.help", value['help']
3737
assert_boolean "#{key}.required", value['required']
3838
end
3939

40+
def assert_dependencies(key, value)
41+
return unless value
42+
43+
case value
44+
when Array
45+
assert_array key, value, of: :string
46+
when Hash
47+
assert_hash key, value, of: :string
48+
else
49+
assert [Array, Hash].include?(value.class),
50+
"#{key} must be an array or a hash"
51+
end
52+
end
53+
4054
def assert_extensible(key, value)
4155
return unless value
4256

@@ -51,7 +65,7 @@ def assert_expose(key, value)
5165
end
5266

5367
def assert_arg(key, value)
54-
assert_hash key, value, Script::Argument.option_keys
68+
assert_hash key, value, keys: Script::Argument.option_keys
5569
assert_string "#{key}.name", value['name']
5670
assert_optional_string "#{key}.help", value['help']
5771
assert_optional_string "#{key}.default", value['default']
@@ -67,7 +81,7 @@ def assert_arg(key, value)
6781
end
6882

6983
def assert_flag(key, value)
70-
assert_hash key, value, Script::Flag.option_keys
84+
assert_hash key, value, keys: Script::Flag.option_keys
7185
assert value['short'] || value['long'], "#{key} must have at least one of long or short name"
7286

7387
refute value['allowed'] && value['completions'], "#{key} cannot have both allowed and completions"
@@ -105,15 +119,15 @@ def assert_flag(key, value)
105119
end
106120

107121
def assert_env_var(key, value)
108-
assert_hash key, value, Script::EnvironmentVariable.option_keys
122+
assert_hash key, value, keys: Script::EnvironmentVariable.option_keys
109123
assert_string "#{key}.name", value['name']
110124
assert_optional_string "#{key}.help", value['help']
111125
assert_optional_string "#{key}.default", value['default']
112126
assert_boolean "#{key}.required", value['required']
113127
end
114128

115129
def assert_command(key, value)
116-
assert_hash key, value, Script::Command.option_keys
130+
assert_hash key, value, keys: Script::Command.option_keys
117131

118132
refute value['commands'] && value['args'], "#{key} cannot have both commands and args"
119133
refute value['commands'] && value['catch_all'], "#{key} cannot have both commands and catch_all"
@@ -133,12 +147,12 @@ def assert_command(key, value)
133147
assert_string_or_array "#{key}.alias", value['alias']
134148
assert_string_or_array "#{key}.examples", value['examples']
135149
assert_extensible "#{key}.extensible", value['extensible']
150+
assert_dependencies "#{key}.dependencies", value['dependencies']
136151

137152
assert_array "#{key}.args", value['args'], of: :arg
138153
assert_array "#{key}.flags", value['flags'], of: :flag
139154
assert_array "#{key}.commands", value['commands'], of: :command
140155
assert_array "#{key}.completions", value['completions'], of: :string
141-
assert_array "#{key}.dependencies", value['dependencies'], of: :string
142156
assert_array "#{key}.filters", value['filters'], of: :string
143157
assert_array "#{key}.environment_variables", value['environment_variables'], of: :env_var
144158

lib/bashly/views/command/dependencies_filter.gtx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
if dependencies
22
= view_marker
33

4-
dependencies.each do |dependency|
4+
dependencies.each do |dependency, message|
55
> if ! [[ -x "$(command -v {{ dependency }})" ]]; then
66
> printf "{{ strings[:missing_dependency] % { dependency: dependency } }}\n" >&2
7+
if message and !message.empty?
8+
> # shellcheck disable=SC2059
9+
> printf "{{ message }}\n" >&2
10+
end
711
> exit 1
812
> fi
913
end

lib/bashly/views/command/fixed_flags_filter.gtx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44
> case "${1:-}" in
55

66
if root_command?
7-
= short_flag_exist?("-v") ? "--version )" : "--version | -v )"
8-
> version_command
9-
> exit
10-
> ;;
7+
= short_flag_exist?("-v") ? "--version )" : "--version | -v )".indent(4)
8+
> version_command
9+
> exit
10+
> ;;
1111
>
1212
end
1313

14-
= short_flag_exist?("-h") ? "--help )" : "--help | -h )"
15-
> long_usage=yes
16-
> <%= function_name %>_usage
17-
> exit
18-
> ;;
14+
= short_flag_exist?("-h") ? "--help )" : "--help | -h )".indent(4)
15+
> long_usage=yes
16+
> <%= function_name %>_usage
17+
> exit
18+
> ;;
1919
>
2020

2121
if global_flags?
2222
flags.each do |flag|
23-
= flag.render(:case).indent(2)
23+
= flag.render(:case).indent(4)
2424
end
2525
end
2626

27-
> * )
28-
> break
29-
> ;;
27+
> * )
28+
> break
29+
> ;;
3030
>
3131
> esac
3232
> done

0 commit comments

Comments
 (0)