Module: Idxfence::CLI

Defined in:
lib/idxfence/cli.rb

Class Method Summary collapse

Class Method Details

.check(argv, out:, err:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/idxfence/cli.rb', line 24

def check(argv, out:, err:)
  options = { json: false }

  parser = OptionParser.new do |opts|
    opts.banner = usage
    opts.on("--json", "emit JSON instead of text") { options[:json] = true }
  end

  begin
    parser.parse!(argv)
  rescue OptionParser::ParseError => e
    err.puts "idxfence: #{e.message}"
    return 2
  end

  if argv.empty?
    err.puts usage
    return 2
  end

  all_findings = []
  warnings = []

  argv.each do |project_dir|
    findings, warning = Idxfence.check(project_dir: project_dir)
    all_findings.concat(findings)
    warnings << warning if warning
  end

  warning_text = warnings.empty? ? nil : warnings.join("\n")

  if options[:json]
    out.puts Render.json(all_findings, warning: warning_text)
  else
    out.puts Render.text(all_findings, warning: warning_text)
  end

  all_findings.empty? ? 0 : 1
end

.run(argv, out: $stdout, err: $stderr) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/idxfence/cli.rb', line 7

def run(argv, out: $stdout, err: $stderr)
  command, *rest = argv

  case command
  when "check"
    check(rest, out: out, err: err)
  else
    err.puts usage
    2
  end
end

.usage ⇒ Object



19
20
21
22
# File 'lib/idxfence/cli.rb', line 19

def usage
  "usage: idxfence check <rails-project-dir> [<rails-project-dir> ...] [--json]\n" \
    "       (each <rails-project-dir> must contain app/models/ and db/schema.rb)"
end