class Smith::Config

Constants

CONFIG_FILENAME

Public Class Methods

get() click to toggle source
# File lib/smith/config.rb, line 38
def self.get
  @config ||= Config.new
end
new() click to toggle source
# File lib/smith/config.rb, line 22
def initialize
  load_config
end

Public Instance Methods

method_missing(method, *args) click to toggle source
# File lib/smith/config.rb, line 34
def method_missing(method, *args)
  @config.send(method, *args)
end
path() click to toggle source
# File lib/smith/config.rb, line 30
def path
  @config_file
end
reload() click to toggle source
# File lib/smith/config.rb, line 26
def reload
  @config = Config.new
end

Private Instance Methods

broker_uri!() click to toggle source
# 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
check_directories() click to toggle source

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
coerce_directories!() click to toggle source

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
default_config_file() click to toggle source

@return [Pathnmae]

# File lib/smith/config.rb, line 182
def default_config_file
  gem_root.join('config', 'smithrc.toml')
end
find_config_file() click to toggle source

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
gem_root() click to toggle source

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
load_config() click to toggle source
# 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_toml(path) click to toggle source

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
load_tomls(default, secondary) click to toggle source

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
path_from_env(env_var, default) click to toggle source

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
paths_from_env(env_var, default) click to toggle source

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
smith_acl_directory() click to toggle source

@return [Pathnmae]

# File lib/smith/config.rb, line 177
def smith_acl_directory
  Pathname.new(__FILE__).dirname.join('messaging').join('acl').expand_path
end
split_paths(paths) click to toggle source

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
to_pathname(path) click to toggle source

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
uri_from_env(env_var, default) click to toggle source

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