Skip to content

Commit 9879b75

Browse files
authored
Merge pull request #194 from DannyBen/update/config-validator
Add more friendly errors on invalid configuration
2 parents 65107a0 + a8c74b0 commit 9879b75

32 files changed

+168
-68
lines changed

lib/bashly/commands/base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Bashly
55
module Commands
66
class Base < MisterBin::Command
77
include AssetHelper
8+
9+
def validate_config
10+
config = Config.new "#{Settings.source_dir}/bashly.yml"
11+
validator = ConfigValidator.new config
12+
validator.validate
13+
end
814
end
915
end
1016
end

lib/bashly/commands/generate.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Generate < Base
2020
example "bashly generate --wrap my_function"
2121

2222
def run
23+
validate_config
2324
create_user_files
2425
upgrade_libs if args['--upgrade']
2526
create_master_script

lib/bashly/commands/validate.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ class Validate < Base
99
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
1010

1111
def run
12-
config = Config.new "#{Settings.source_dir}/bashly.yml"
13-
validator = ConfigValidator.new config
14-
validator.validate
12+
validate_config
1513
say "!txtgrn!OK"
1614
end
1715
end

lib/bashly/config_validator.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def assert_arg(key, value)
8484
assert_array "#{key}.allowed", value['allowed'], of: :string
8585

8686
refute value['name'].match(/^-/), "#{key}.name must not start with '-'"
87+
88+
refute value['required'] && value['default'], "#{key} cannot have both required and default"
8789
end
8890

8991
def assert_flag(key, value)
@@ -104,6 +106,16 @@ def assert_flag(key, value)
104106
assert value['long'].match(/^--[a-zA-Z0-9_\-]+$/), "#{key}.long must be in the form of '--name'" if value['long']
105107
assert value['short'].match(/^-[a-zA-Z0-9]$/), "#{key}.short must be in the form of '-n'" if value['short']
106108
refute value['arg'].match(/^-/), "#{key}.arg must not start with '-'" if value['arg']
109+
110+
refute value['required'] && value['default'], "#{key} cannot have both required and default"
111+
112+
if value['default']
113+
assert value['arg'], "#{key}.default does not make sense without arg"
114+
end
115+
116+
if value['allowed']
117+
assert value['arg'], "#{key}.allowed does not make sense without arg"
118+
end
107119
end
108120

109121
def assert_env_var(key, value)
@@ -140,6 +152,16 @@ def assert_command(key, value)
140152
assert_array "#{key}.filters", value['filters'], of: :string
141153
assert_array "#{key}.environment_variables", value['environment_variables'], of: :env_var
142154
assert_array "#{key}.examples", value['examples'], of: :string
155+
156+
if key == "root"
157+
refute value['short'], "#{key}.short makes no sense"
158+
refute value['group'], "#{key}.group makes no sense"
159+
refute value['default'], "#{key}.default makes no sense"
160+
refute value['private'], "#{key}.private makes no sense"
161+
else
162+
refute value['version'], "#{key}.version makes no sense"
163+
refute value['extensible'], "#{key}.extensible makes no sense"
164+
end
143165
end
144166
end
145167
end

lib/bashly/script/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Base
3636
def initialize(options)
3737
raise Error, "Invalid options provided" unless options.respond_to? :keys
3838
@options = options
39-
validate_options if respond_to? :validate_options
4039
end
4140

4241
def optional

lib/bashly/script/command.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,6 @@ def user_lib
128128
@user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.sh"].sort
129129
end
130130

131-
# Raise an exception if there are some serious issues with the command
132-
# definition. This is called by Base#initialize.
133-
def validate_options
134-
Bashly::ConfigValidator.new(options).validate
135-
end
136-
137131
end
138132
end
139133
end

spec/approvals/cli/error

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
Errno::ENOENT
22
No such file or directory @ rb_sysopen - src/bashly.yml
3-
creating user files in src
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.catch_all.label must be a string>

0 commit comments

Comments
 (0)