class Twigg::Config

The Config class mediates all access to the Twigg config file.

First, we look for a YAML file at the location specified by the TWIGGRC environment variable. If that isn't set, we fallback to looking for a config file at `~/.twiggrc`.

Example use:

Config.bind                   # the bind address for the Twigg web app
                              # [default: 0.0.0.0]
Config.gerrit.host            # the (optional) Gerrit hostname
                              # [default: localhost]
Config.gerrit.port            # the (optional) Gerrit port
                              # [default: 29418]
Config.gerrit.user            # the (optional) Gerrit username
                              # [default: $USER environment variable]
Config.repositories_directory # where to find repositories

Constants

TWIGGRC

Public Class Methods

boot() click to toggle source

Perform boot-time configuration

# File lib/twigg/config.rb, line 35
def boot
  config # ensure `-c`/`--config` option is respected
  set_up_encoding
end
new(twiggrc: nil) click to toggle source
# File lib/twigg/config.rb, line 58
def initialize(twiggrc: nil)
  @settings = Settings.new(config_from_file(twiggrc) ||
                           config_from_argv ||
                           config_from_env ||
                           config_from_home)
end

Private Class Methods

config() click to toggle source

Maintain a “singleton” Config instance for convenient access.

# File lib/twigg/config.rb, line 53
def config
  @config ||= new
end
set_up_encoding() click to toggle source

Make sure we have a sane default encoding for reading across process boundaries (eg. consuming APIs, reading from `git` commands etc).

# File lib/twigg/config.rb, line 44
def set_up_encoding
  encoding = Encoding.find(config.default_encoding)
  Encoding.default_external = encoding
  Encoding.default_internal = encoding
rescue ArgumentError => e
  Console.warn "desired default encoding not available (#{e})"
end

Private Instance Methods

config_from_argv() click to toggle source
# File lib/twigg/config.rb, line 92
def config_from_argv
  # It is a bit of a smell to have the Config class know about argument
  # processing, but, at least in development, Bundler will end up eagerly
  # loading the config when it evaluates the Gemfile (and hence the
  # twigg-app.gemspec), which means that this happens before the
  # Twigg::Command.run method gets a chance to set things up properly.
  path = consume_option(%w[-c --config], ARGV)
  config_from_file(path)
end
config_from_env() click to toggle source
# File lib/twigg/config.rb, line 102
def config_from_env
  config_from_file(ENV['TWIGGRC'])
end
config_from_file(path) click to toggle source
# File lib/twigg/config.rb, line 72
    def config_from_file(path)
      return unless path

      YAML.load_file(path).tap do |contents|
        if File.world_readable?(path)
          warn "#{path} is world-readable"
          stderr strip_heredoc(<<-DOC)

            The Twigg config file may contain sensitive information, such as
            access credentials for external services.

            Suggested action: tighten the filesystem permissions with:

                chmod 600 #{Shellwords.escape path}

          DOC
        end
      end
    end
config_from_home() click to toggle source
# File lib/twigg/config.rb, line 108
def config_from_home
  config_from_file(File.join(Dir.home, TWIGGRC))
rescue Errno::ENOENT
  {} # no custom config; assume defaults
end
method_missing(method, *args, &block) click to toggle source

Foward all messages to the underlying {Settings} instance.

# File lib/twigg/config.rb, line 68
def method_missing(method, *args, &block)
  @settings.send(method, *args, &block)
end