settings_validator.rb revision 6bbb71b52f053c148669e5b128e236781150da1f
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen class SettingsValidator::ValidationError < ::StandardError; end
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen "config/settings[.local].yml or config/settings/#{Rails.env}[.local].yml"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen CONFIG_INITIALIZER_FILES = "config/environments/#{Rails.env}[.local].rb"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen settings_validations_wrapper = SettingsValidationWrapper.new
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen plain_errors = settings_validations_wrapper.errors.messages
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen formatted_errors = format_errors(plain_errors)
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen $stderr.puts "The settings are invalid. Please check your #{CONFIG_YML_FILES}"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen $stderr.puts 'The following errors were detected:'
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen formatted_errors.each { |error| $stderr.puts error }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen errors.map { |key, messages| format_error(key, messages) }
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen "#{format_initializer_error(key_portions)} #{format_key(key_portions)}"
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen#{opening_line}:
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen#{format_messages(messages)}
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen Value: #{bad_value.inspect}
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen def key_info(key)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen category, *portions = key.to_s.split('__')
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen [category.to_sym, portions]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_key(portions)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen portions.join('.')
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen def format_messages(messages)
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen messages.map { |message| format_message(message) }.join("\n")
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_message(message)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if message == 'is not included in the list'
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen "#{message} (see the comments in the config/settings.yml at this key)"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen indent_message(message, 2)
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen def format_initializer_error(key_portions)
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen key = key_portions.join('.')
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if 'fqdn' == key
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen "The FQDN could not be determined. Please set the hostname in #{CONFIG_YML_FILES}"
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen elsif %w(data_root git_root git_daemon_path git_ssh_path commits_path).include?(key)
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen 'Please check the paths keys in the settings -'
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen # other possible values: %w(consider_all_requests_local secret_token log_level)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen "Please set a valid value in the #{CONFIG_INITIALIZER_FILES} for"
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen def value_of(category, key_portions)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if !%i(yml initializers).include?(category)
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen SettingsValidationWrapper.new.send(category)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen object = SettingsValidationWrapper.base(category.to_s)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen SettingsValidationWrapper.get_value(object, key_portions)
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen def indent_message(message, indentation)
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen message.split("\n").map { |line| "#{' ' * indentation}#{line}"}.join("\n")