settings_validator.rb revision 6bbb71b52f053c148669e5b128e236781150da1f
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainenclass SettingsValidator
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen class SettingsValidator::ValidationError < ::StandardError; end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen CONFIG_YML_FILES =
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
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def validate!
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen settings_validations_wrapper = SettingsValidationWrapper.new
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unless settings_validations_wrapper.valid?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen plain_errors = settings_validations_wrapper.errors.messages
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen formatted_errors = format_errors(plain_errors)
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainen print_errors(formatted_errors)
2cd2518bab14292a67cf8a490b58ab9ef89879daTimo Sirainen exit 1
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen protected
6ec925f52d04ec8700e47cb005bd7ddc65ac5614Timo Sirainen
c60178b2610a9b193ff72aa18448398ef72529a1Timo Sirainen def print_errors(formatted_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 $stderr.puts
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen formatted_errors.each { |error| $stderr.puts error }
cf2e6953d03a1c22f272ec19432fc03c136ac1bbTimo Sirainen $stderr.puts
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen $stderr.puts 'Stopping the application.'
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_errors(errors)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen errors.map { |key, messages| format_error(key, messages) }
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
617e13833c798435e2be425b99c27ecaad1b8393Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_error(key, messages)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen category, key_portions = key_info(key)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen opening_line =
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if category == :initializers
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen "#{format_initializer_error(key_portions)} #{format_key(key_portions)}"
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen elsif category == :yml
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen format_key(key_portions)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen else
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen key
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen end
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen bad_value = value_of(category, key_portions)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen <<-MESSAGE
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen#{opening_line}:
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen#{format_messages(messages)}
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen Value: #{bad_value.inspect}
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen MESSAGE
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen end
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen def key_info(key)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen category, *portions = key.to_s.split('__')
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen [category.to_sym, portions]
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen end
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_key(portions)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen portions.join('.')
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen def format_messages(messages)
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen messages.map { |message| format_message(message) }.join("\n")
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def format_message(message)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if message == 'is not included in the list'
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen message =
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen "#{message} (see the comments in the config/settings.yml at this key)"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen indent_message(message, 2)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
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 -'
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen else
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 end
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen end
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen def value_of(category, key_portions)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if !%i(yml initializers).include?(category)
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen SettingsValidationWrapper.new.send(category)
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen else
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen object = SettingsValidationWrapper.base(category.to_s)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen SettingsValidationWrapper.get_value(object, key_portions)
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen end
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen end
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen def indent_message(message, indentation)
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen message.split("\n").map { |line| "#{' ' * indentation}#{line}"}.join("\n")
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen end
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainenend
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen