Class: Overcommit::ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/overcommit/configuration_loader.rb

Overview

Manages configuration file loading.

Constant Summary collapse

DEFAULT_CONFIG_PATH =
File.join(Overcommit::HOME, 'config', 'default.yml')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, options = {}) ⇒ ConfigurationLoader

Create a configuration loader which writes warnings/errors to the given Logger instance.

Parameters:

  • logger (Overcommit::Logger)
  • options (Hash) (defaults to: {})
  • verify (Hash)

    a customizable set of options


47
48
49
50
# File 'lib/overcommit/configuration_loader.rb', line 47

def initialize(logger, options = {})
  @log = logger
  @options = options
end

Class Method Details

.default_configurationOvercommit::Configuration

Loads and returns the default configuration.


14
15
16
# File 'lib/overcommit/configuration_loader.rb', line 14

def default_configuration
  @default_configuration ||= load_from_file(DEFAULT_CONFIG_PATH, default: true, verify: false)
end

.load_from_file(file, options = {}) ⇒ Overcommit::Configuration

Loads configuration from file.

Parameters:

  • file (String)

    path to file

  • options (Hash) (defaults to: {})
  • default (Hash)

    a customizable set of options

  • verify (Hash)

    a customizable set of options

  • logger (Hash)

    a customizable set of options

Returns:


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/overcommit/configuration_loader.rb', line 26

def load_from_file(file, options = {})
  # Psych 4 introduced breaking behavior that doesn't support aliases by
  # default. Explicitly enable aliases if the option is available.
  yaml =
    begin
      YAML.load_file(file, aliases: true)
    rescue ArgumentError
      YAML.load_file(file)
    end

  hash = yaml ? yaml.to_hash : {}
  Overcommit::Configuration.new(hash, options)
end

Instance Method Details

#load_file(file) ⇒ Object

Loads a configuration, ensuring it extends the default configuration.


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/overcommit/configuration_loader.rb', line 67

def load_file(file)
  config = self.class.load_from_file(file, default: false, logger: @log)
  config = self.class.default_configuration.merge(config)

  if @options.fetch(:verify) { config.verify_signatures? }
    verify_signatures(config)
  end

  config
rescue Overcommit::Exceptions::ConfigurationSignatureChanged
  raise
rescue StandardError => e
  raise Overcommit::Exceptions::ConfigurationError,
        "Unable to load configuration from '#{file}': #{e}",
        e.backtrace
end

#load_repo_configOvercommit::Configuration

Loads and returns the configuration for the repository we're running in.


55
56
57
58
59
60
61
62
63
64
# File 'lib/overcommit/configuration_loader.rb', line 55

def load_repo_config
  overcommit_yml = File.join(Overcommit::Utils.repo_root,
                             Overcommit::CONFIG_FILE_NAME)

  if File.exist?(overcommit_yml)
    load_file(overcommit_yml)
  else
    self.class.default_configuration
  end
end