Skip to content

Commit c4e2553

Browse files
committed
- Validate uniqueness of command args, flags and subcommands
1 parent e436f15 commit c4e2553

File tree

8 files changed

+62
-1
lines changed

8 files changed

+62
-1
lines changed

lib/bashly/config_validator.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def assert_hash(key, value, whitelist = nil)
5959
end
6060
end
6161

62+
def assert_uniq(key, value, array_key)
63+
return unless value
64+
list = value.map { |c| c[array_key] }.compact.flatten
65+
assert list.uniq?, "#{key} cannot have elements with similar #{array_key} values"
66+
end
67+
6268
def assert_string_or_array(key, value)
6369
return unless value
6470
assert [Array, String].include?(value.class),
@@ -149,7 +155,7 @@ def assert_env_var(key, value)
149155
assert_boolean "#{key}.required", value['required']
150156
end
151157

152-
def assert_command(key, value)
158+
def assert_command(key, value)
153159
assert_hash key, value, Script::Command.option_keys
154160

155161
refute value['commands'] && value['args'], "#{key} cannot have both commands and args"
@@ -177,6 +183,12 @@ def assert_command(key, value)
177183
assert_array "#{key}.environment_variables", value['environment_variables'], of: :env_var
178184
assert_array "#{key}.examples", value['examples'], of: :string
179185

186+
assert_uniq "#{key}.commands", value['commands'], 'name'
187+
assert_uniq "#{key}.commands", value['commands'], 'alias'
188+
assert_uniq "#{key}.flags", value['flags'], 'long'
189+
assert_uniq "#{key}.flags", value['flags'], 'short'
190+
assert_uniq "#{key}.args", value['args'], 'name'
191+
180192
if value['catch_all'] and value['args']
181193
repeatable_arg = value['args'].select { |a| a['repeatable'] }.first&.dig 'name'
182194
refute repeatable_arg, "#{key}.catch_all makes no sense with repeatable arg (#{repeatable_arg})"

lib/bashly/extensions/array.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ def indent(offset)
44
indentation = " " * offset
55
map { |line| "#{indentation}#{line}" }
66
end
7+
8+
def uniq?
9+
self == uniq
10+
end
11+
712
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.args cannot have elements with similar name values>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.commands cannot have elements with similar alias values>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.commands cannot have elements with similar name values>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags cannot have elements with similar long values>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.flags cannot have elements with similar short values>

spec/fixtures/script/validations.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,42 @@
209209
help: Path to one or more files
210210
repeatable: true
211211
required: true
212+
213+
:non_uniq_command_names:
214+
name: invalid
215+
help: commands with the same name
216+
commands:
217+
- name: upload
218+
- name: download
219+
- name: download
220+
221+
:non_uniq_command_aliases:
222+
name: invalid
223+
help: commands with the same alias
224+
commands:
225+
- name: connect
226+
alias: c
227+
- name: config
228+
alias: [c, conf]
229+
230+
:non_uniq_flags_longs:
231+
name: invalid
232+
help: flags with the same long name
233+
flags:
234+
- long: --force
235+
- long: --force
236+
237+
:non_uniq_flags_shorts:
238+
name: invalid
239+
help: flags with the same short name
240+
flags:
241+
- short: -f
242+
- short: -f
243+
244+
:non_uniq_arg_names:
245+
name: invalid
246+
help: args with the same name
247+
args:
248+
- name: file
249+
- name: file
250+

0 commit comments

Comments
 (0)