Class: Audition::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/audition/cli.rb

Constant Summary collapse

USAGE =
"usage: audition [options] TARGET [FILE...] " \
"(a .rb script, directory, config.ru dir, Rails root, " \
"gem dir, installed gem name, or several .rb files)"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:) ⇒ CLI

Returns a new instance of CLI.



24
25
26
27
# File 'lib/audition/cli.rb', line 24

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
end

Class Method Details

.run(argv, stdout: $stdout, stderr: $stderr) ⇒ Integer

Entry point used by exe/audition.

Parameters:

  • argv (Array<String>)

    command line arguments

  • stdout (IO) (defaults to: $stdout)
  • stderr (IO) (defaults to: $stderr)

Returns:

  • (Integer)

    exit code: 0 clean, 1 findings at or above the --fail-on threshold, 2 usage error



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

def self.run(argv, stdout: $stdout, stderr: $stderr)
  new(stdout: stdout, stderr: stderr).run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



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
# File 'lib/audition/cli.rb', line 29

def run(argv)
  options = parse(argv.dup)
  return options if options.is_a?(Integer)

  return print_capabilities(options) if options[:capabilities]

  args = options[:args]
  if args.empty?
    @stderr.puts(style(options, io: @stderr).dim(USAGE))
    return 2
  end

  # A single argument keeps full detection (including the
  # dynamic probe); several arguments are the git-hook shape,
  # a plain list of staged .rb files audited statically.
  target = if args.size == 1
    Target.detect(args.first)
  else
    Target.for_files(args)
  end
  target = deps_target(target) if options[:deps]
  if target.type == :bundle
    sweep(target, options)
  else
    audit(target, options)
  end
rescue Error => e
  complain(e.message, options)
  2
end