class Smith::Config
Constants
- CONFIG_FILENAME
Public Class Methods
# File lib/smith/config.rb, line 38 def self.get @config ||= Config.new end
# File lib/smith/config.rb, line 22 def initialize load_config end
Public Instance Methods
# File lib/smith/config.rb, line 34 def method_missing(method, *args) @config.send(method, *args) end
# File lib/smith/config.rb, line 30 def path @config_file end
# File lib/smith/config.rb, line 26 def reload @config = Config.new end
Private Instance Methods
# File lib/smith/config.rb, line 86 def broker_uri! uri = if @config.amqp.broker[:uri] Addressable::URI.parse(@config.amqp.broker[:uri]) else Addressable::URI.new.tap do |u| u.scheme = 'amqp' u.host = @config.amqp.broker.delete(:host) u.port = @config.amqp.broker.delete(:port) vhost = @config.amqp.broker.delete(:vhost) u.path = (vhost == '/') ? "" : vhost u.user = @config.amqp.broker.delete(:user) u.password = @config.amqp.broker.delete(:password) end end @config.amqp.broker[:uri] = uri_from_env('SMITH_BROKER_URI', uri) @config.amqp.broker[:vhost] = @config.amqp.broker[:uri].path end
Make sure the non-default direcotires are set. @raise [MissingConfigItemError<Array<String>>] the config items that are not set.
# File lib/smith/config.rb, line 62 def check_directories errors = [] errors << "agncy.acl_directories" if @config.agency[:acl_directories].empty? errors << "agncy.agent_directories" if @config.agency[:agent_directories].empty? unless errors.empty? raise MissingConfigItemError, errors end end
Check appropriate env vars and convert the string representation to Pathname @return [ConfigHash] the config with coerced paths.
# File lib/smith/config.rb, line 74 def coerce_directories! @config.tap do |c| c.agency[:pid_directory] = path_from_env('SMITH_PID_DIRECTORY', c.agency[:pid_directory]) c.agency[:cache_directory] = path_from_env('SMITH_CACHE_DIRECTORY', c.agency[:cache_directory]) c.agency[:acl_directories] = paths_from_env('SMITH_ACL_DIRECTORIES', c.agency[:acl_directories]) c.agency[:agent_directories] = paths_from_env('SMITH_AGENT_DIRECTORIES', c.agency[:agent_directories]) end check_directories @config.agency[:acl_directories] = @config.agency[:acl_directories] + [smith_acl_directory] end
@return [Pathnmae]
# File lib/smith/config.rb, line 182 def default_config_file gem_root.join('config', 'smithrc.toml') end
Find the config file. This checks the following paths before raising an exception:
-
./.smithrc
-
$HOME/.smithrc
-
/etc/smithrc
-
/etc/smith/smithrc
@return the config file path
# File lib/smith/config.rb, line 116 def find_config_file if ENV["SMITH_CONFIG"] to_pathname(ENV["SMITH_CONFIG"]).tap do |path| raise ConfigNotFoundError, "Cannot find a config file name: #{path}" unless path.exist? end else user = ["./.#{CONFIG_FILENAME}", "#{ENV['HOME']}/.#{CONFIG_FILENAME}"].map { |p| to_pathname(p) } system = ["/etc/#{CONFIG_FILENAME}", "/etc/smith/#{CONFIG_FILENAME}"].map { |p| to_pathname(p) } default = [default_config_file] (user + system + default).detect { |path| path.exist? } end end
Returns the gem root. We can't use Smith.root_path
here as it hasn't been initialised yet.
# File lib/smith/config.rb, line 205 def gem_root Pathname.new(__FILE__).dirname.parent.parent.expand_path end
# File lib/smith/config.rb, line 53 def load_config @config_file = find_config_file @config = load_tomls(default_config_file, @config_file) coerce_directories! broker_uri! end
Load the toml file specified
@param path [Pathname] the path of the toml file @return [ConfigHash] the toml file
# File lib/smith/config.rb, line 199 def load_toml(path) ConfigHash.new(TOML.parse(path.read, :symbolize_keys => true)) end
Loads and merges multiple toml files.
@param default [String] the default toml file @param secondary [String] the user supplied toml file @return [ConfigHash] the merge toml files.
# File lib/smith/config.rb, line 191 def load_tomls(default, secondary) load_toml(default).deep_merge(load_toml(secondary)) end
Returns a path from the environment variable passed in. If the environment variable is not set it returns nil.
@param env_var [String] the name of the environment variable @param default [String] the value to use if the environment variable is not set @return [Pathname]
# File lib/smith/config.rb, line 144 def path_from_env(env_var, default) to_pathname(ENV.fetch(env_var, default)) end
Returns an array of Paths from the environment variable passed in. If the environment variable is not set it returns an empty array.
@param env_var [String] the name of the environment variable @param default [String] the value to use if the environment variable is not set @return [Array<Pathnmae>]
# File lib/smith/config.rb, line 154 def paths_from_env(env_var, default) split_paths(ENV.fetch(env_var, default)) end
@return [Pathnmae]
# File lib/smith/config.rb, line 177 def smith_acl_directory Pathname.new(__FILE__).dirname.join('messaging').join('acl').expand_path end
Splits a string using PATH_SEPARATOR.
@param paths to split @return [Array<Pathnmae>]
# File lib/smith/config.rb, line 172 def split_paths(paths) (paths || '').split(File::PATH_SEPARATOR).map { |p| to_pathname(p) } end
Convert a string to a path
@param path [String] the string to convert @return [Pathname]
# File lib/smith/config.rb, line 134 def to_pathname(path) Pathname.new(path).expand_path end
Returns a URI from the environment variable passed in. If the environment variable is not set it returns nil.
@param env_var [String] the name of the environment variable @param default [String] the value to use if the environment variable is not set @return [Addressable::URI]
# File lib/smith/config.rb, line 164 def uri_from_env(env_var, default) Addressable::URI.parse(ENV.fetch(env_var, default)) end