/
config_helper.rb
210 lines (173 loc) · 6.58 KB
/
config_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# typed: strict
# frozen_string_literal: true
require "yaml"
module Tapioca
module ConfigHelper
extend T::Sig
extend T::Helpers
requires_ancestor { Thor }
sig { returns(String) }
attr_reader :command_name
sig { returns(Thor::CoreExt::HashWithIndifferentAccess) }
attr_reader :defaults
sig { params(args: T.untyped, local_options: T.untyped, config: T.untyped).void }
def initialize(args = [], local_options = {}, config = {})
# Store current command
command = config[:current_command]
command_options = config[:command_options]
@command_name = T.let(command.name, String)
@merged_options = T.let(nil, T.nilable(Thor::CoreExt::HashWithIndifferentAccess))
@defaults = T.let(Thor::CoreExt::HashWithIndifferentAccess.new, Thor::CoreExt::HashWithIndifferentAccess)
# Filter command options unless we are handling the help command.
# This is so that the defaults are printed
filter_defaults(command_options) unless command_name == "help"
super
end
sig { returns(Thor::CoreExt::HashWithIndifferentAccess) }
def options
@merged_options ||= begin
original_options = super
config_options = config_options(original_options)
merge_options(defaults, config_options, original_options)
end
end
private
sig { params(options: T::Hash[Symbol, Thor::Option]).void }
def filter_defaults(options)
options.each do |key, option|
# Store the value of the current default in our defaults hash
defaults[key] = option.default
# Remove the default value from the option
option.instance_variable_set(:@default, nil)
end
end
sig { params(options: Thor::CoreExt::HashWithIndifferentAccess).returns(Thor::CoreExt::HashWithIndifferentAccess) }
def config_options(options)
config_file = options[:config]
config = {}
if File.exist?(config_file)
config = YAML.load_file(config_file, fallback: {})
end
validate_config!(config_file, config)
Thor::CoreExt::HashWithIndifferentAccess.new(config[command_name] || {})
end
sig { params(config_file: String, config: T::Hash[T.untyped, T.untyped]).void }
def validate_config!(config_file, config)
# To ensure that this is not re-entered, we mark during validation
return if @validating_config
@validating_config = T.let(true, T.nilable(T::Boolean))
commands = T.cast(self, Thor).class.commands
errors = config.flat_map do |config_key, config_options|
command = commands[config_key.to_s]
unless command
next build_error("unknown key `#{config_key}`")
end
validate_config_options(command.options, config_key, config_options || {})
end.compact
unless errors.empty?
print_errors(config_file, errors)
exit(1)
end
ensure
@validating_config = false
end
sig do
params(
command_options: T::Hash[Symbol, Thor::Option],
config_key: String,
config_options: T::Hash[T.untyped, T.untyped]
).returns(T::Array[ConfigError])
end
def validate_config_options(command_options, config_key, config_options)
config_options.map do |config_option_key, config_option_value|
command_option = command_options[config_option_key.to_sym]
error_msg = "unknown option `#{config_option_key}` for key `#{config_key}`"
next build_error(error_msg) unless command_option
config_option_value_type = case config_option_value
when FalseClass, TrueClass
:boolean
when Numeric
:numeric
when Hash
:hash
when Array
:array
when String
:string
else
:object
end
error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \
"`#{command_option.type.capitalize}` but found #{config_option_value_type.capitalize}"
next build_error(error_msg) unless config_option_value_type == command_option.type
case config_option_value_type
when :array
error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \
"`Array[String]` but found `#{config_option_value}`"
next build_error(error_msg) unless config_option_value.all? { |v| v.is_a?(String) }
when :hash
error_msg = "invalid value for option `#{config_option_key}` for key `#{config_key}` - expected " \
"`Hash[String, String]` but found `#{config_option_value}`"
all_strings = (config_option_value.keys + config_option_value.values).all? { |v| v.is_a?(String) }
next build_error(error_msg) unless all_strings
end
end.compact
end
class ConfigErrorMessagePart < T::Struct
const :message, String
const :colors, T::Array[Symbol]
end
class ConfigError < T::Struct
const :message_parts, T::Array[ConfigErrorMessagePart]
end
sig { params(msg: String).returns(ConfigError) }
def build_error(msg)
parts = msg.split(/(`[^`]+` ?)/)
message_parts = parts.map do |part|
match = part.match(/`([^`]+)`( ?)/)
if match
ConfigErrorMessagePart.new(
message: "#{match[1]}#{match[2]}",
colors: [:bold, :blue]
)
else
ConfigErrorMessagePart.new(
message: part,
colors: [:yellow]
)
end
end
ConfigError.new(
message_parts: message_parts
)
end
sig { params(config_file: String, errors: T::Array[ConfigError]).void }
def print_errors(config_file, errors)
say_error("\nConfiguration file ", :red)
say_error("#{config_file} ", :blue, :bold)
say_error("has the following errors:\n\n", :red)
errors.each do |error|
say_error("- ")
error.message_parts.each do |part|
T.unsafe(self).say_error(part.message, *part.colors)
end
end
end
sig do
params(options: T.nilable(Thor::CoreExt::HashWithIndifferentAccess))
.returns(Thor::CoreExt::HashWithIndifferentAccess)
end
def merge_options(*options)
merged = options.each_with_object({}) do |option, result|
result.merge!(option || {}) do |_, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
Thor::CoreExt::HashWithIndifferentAccess.new(this_val.merge(other_val))
else
other_val
end
end
end
Thor::CoreExt::HashWithIndifferentAccess.new(merged)
end
end
end