Module: SolidQueueGuard::CLI

Defined in:
lib/solid_queue_guard/cli.rb

Class Method Summary collapse

Class Method Details

.format_flag(argv) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/solid_queue_guard/cli.rb', line 42

def format_flag(argv)
  if (format_argument = argv.find { |argument| argument.start_with?('--format=') })
    format_argument.split('=', 2).last.to_sym
  else
    ENV.fetch('SOLID_QUEUE_GUARD_FORMAT', 'text').to_sym
  end
end

.formatter_for(format) ⇒ Object



31
32
33
34
35
36
# File 'lib/solid_queue_guard/cli.rb', line 31

def formatter_for(format)
  case format.to_sym
  when :json then Formatters::Json
  else Formatters::Terminal
  end
end

.notify?(report) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/solid_queue_guard/cli.rb', line 58

def notify?(report)
  return false unless SolidQueueGuard.config.notify_with.any?

  report.status != :healthy
end

.options(argv: ARGV) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/solid_queue_guard/cli.rb', line 7

def options(argv: ARGV)
  {
    strict: strict_flag?(argv),
    format: format_flag(argv),
    scope: scope_flag(argv)
  }
end

.run!(scope: nil, format: nil, strict: nil, argv: ARGV) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/solid_queue_guard/cli.rb', line 15

def run!(scope: nil, format: nil, strict: nil, argv: ARGV)
  scope ||= scope_flag(argv)
  format ||= format_flag(argv)
  strict = strict_flag?(argv) if strict.nil?

  report = Runner.new(scope: scope).run
  Notifier.deliver_all(report) if notify?(report)
  Metrics::Exporter.export(report) if SolidQueueGuard.config.metrics_backends.any?

  output = formatter_for(format).new(report).render

  $stdout.puts(output) unless output.empty?

  exit report.exit_code(strict: strict)
end

.scope_flag(argv) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/solid_queue_guard/cli.rb', line 50

def scope_flag(argv)
  if (scope_argument = argv.find { |argument| argument.start_with?('--scope=') })
    scope_argument.split('=', 2).last.to_sym
  else
    ENV.fetch('SOLID_QUEUE_GUARD_SCOPE', 'config').to_sym
  end
end

.strict_flag?(argv) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/solid_queue_guard/cli.rb', line 38

def strict_flag?(argv)
  SolidQueueGuard.config.strict? || argv.include?('--strict')
end