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



44
45
46
47
# File 'lib/overcommit/configuration_loader.rb', line 44

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
# File 'lib/overcommit/configuration_loader.rb', line 26

def load_from_file(file, options = {})
  hash =
    if yaml = YAML.load_file(file)
      yaml.to_hash
    else
      {}
    end

  Overcommit::Configuration.new(hash, options)
end

Instance Method Details

#load_file(file) ⇒ Object

Loads a configuration, ensuring it extends the default configuration.



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

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 => error
  raise Overcommit::Exceptions::ConfigurationError,
        "Unable to load configuration from '#{file}': #{error}",
        error.backtrace
end

#load_repo_configOvercommit::Configuration

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/overcommit/configuration_loader.rb', line 52

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