Module: Goodcheck::Commands::ConfigLoading

Includes:
ExitStatus
Included in:
Check, Pattern, Test
Defined in:
lib/goodcheck/commands/config_loading.rb

Defined Under Namespace

Classes: ConfigFileNotFound

Constant Summary

Constants included from ExitStatus

ExitStatus::EXIT_ERROR, ExitStatus::EXIT_MATCH, ExitStatus::EXIT_SUCCESS, ExitStatus::EXIT_TEST_FAILED

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/goodcheck/commands/config_loading.rb', line 15

def config
  @config
end

Instance Method Details

#handle_config_errors(stderr) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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