module Goodcheck::Commands::ConfigLoading

Attributes

config[R]

Public Instance Methods

handle_config_errors(stderr) { || ... } click to toggle source
# File lib/goodcheck/commands/config_loading.rb, line 31
def handle_config_errors(stderr)
  begin
    yield
  rescue ConfigFileNotFound => exn
    stderr.puts "Configuration file not found: #{exn.path}"
    EXIT_ERROR
  rescue ConfigLoader::InvalidPattern => exn
    stderr.puts exn.message
    EXIT_ERROR
  rescue Psych::Exception => exn
    stderr.puts "Unexpected error happens while loading YAML file: #{exn.inspect}"
    exn.backtrace.each do |trace_loc|
      stderr.puts "  #{trace_loc}"
    end
    EXIT_ERROR
  rescue StrongJSON::Type::TypeError, StrongJSON::Type::UnexpectedAttributeError => exn
    stderr.puts "Invalid config: #{exn.message}"
    stderr.puts StrongJSON::ErrorReporter.new(path: exn.path).to_s
    EXIT_ERROR
  rescue Errno::ENOENT => exn
    stderr.puts "#{exn}"
    EXIT_ERROR
  end
end
load_config!(force_download:, cache_path:) click to toggle source
# File lib/goodcheck/commands/config_loading.rb, line 17
def load_config!(force_download:, cache_path:)
  config_content =
    begin
      config_path.read
    rescue Errno::ENOENT
      raise ConfigFileNotFound.new(path: config_path)
    end

  import_loader = ImportLoader.new(cache_path: cache_path, force_download: force_download, config_path: config_path)
  content = JSON.parse(JSON.dump(YAML.safe_load(config_content, filename: config_path.to_s)), symbolize_names: true)
  loader = ConfigLoader.new(path: config_path, content: content, stderr: stderr, import_loader: import_loader)
  @config = loader.load
end