Module: Html2rss::CLI::Validate

Defined in:
lib/html2rss/cli/validate.rb

Overview

Validate command: file/glob/stdin branching and multi-file reporting.

Class Method Summary collapse

Class Method Details

.resolve_files(files) ⇒ Array<String>

Parameters:

  • files (Array<String>)

Returns:

  • (Array<String>)


49
50
51
52
53
54
55
56
57
58
# File 'lib/html2rss/cli/validate.rb', line 49

def resolve_files(files)
  return ['-'] if files.empty? || files == ['-']

  resolved = []
  files.each do |f|
    matched = Dir.glob(f)
    resolved.concat(matched.empty? ? [f] : matched)
  end
  resolved.uniq
end

.run(files, params:, quiet:) ⇒ void

This method returns an undefined value.

Parameters:

  • files (Array<String>)
  • params (Hash)
  • quiet (Boolean)

Raises:

  • (Thor::Error)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/html2rss/cli/validate.rb', line 16

def run(files, params:, quiet:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  if files.size == 2 && File.file?(files[0].to_s) && !File.exist?(files[1].to_s)
    raise Thor::Error, "No such file: #{files[1]}" if path_like_config?(files[1])

    return run_named_feed(files[0], files[1], params:, quiet:)
  end

  target_files = resolve_files(files)
  failed = []

  target_files.each do |file|
    result = validate_file(file, params:)

    if result.success?
      puts(target_files.size == 1 ? 'Configuration is valid' : "ok   #{file}") unless quiet
    else
      error_details = result.errors.to_h
      raise Thor::Error, "Invalid configuration: #{error_details}" if target_files.size == 1

      warn "FAIL #{file}"
      error_details.each { |key, msg| warn "       #{key}: #{Array(msg).join(', ')}" }
      failed << file
    end
  end

  return if failed.empty?

  raise Thor::Error, "#{failed.size}/#{target_files.size} configurations failed validation."
end