Class: Periphery::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/periphery/runner.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binary_path) ⇒ Runner

Returns a new instance of Runner.



10
11
12
# File 'lib/periphery/runner.rb', line 10

def initialize(binary_path)
  @binary_path = binary_path || 'periphery'
end

Instance Attribute Details

#binary_pathObject (readonly)

Returns the value of attribute binary_path.



8
9
10
# File 'lib/periphery/runner.rb', line 8

def binary_path
  @binary_path
end

Instance Method Details

#scan(options) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/periphery/runner.rb', line 14

def scan(options)
  arguments = [binary_path, 'scan'] + scan_arguments(options)
  stdout, stderr, status = Open3.capture3(*arguments)
  raise "error: #{arguments} exited with status code #{status.exitstatus}. #{stderr}" unless status.success?

  stdout
end

#scan_arguments(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/periphery/runner.rb', line 22

def scan_arguments(options)
  options.each_with_object([]) do |(key, value), new_options|
    next unless value

    value = nil if value.is_a?(TrueClass)
    if value.is_a?(Array)
      if Gem::Version.new(version) >= Gem::Version.new('2.18.0')
        new_options << "--#{key.to_s.tr('_', '-')}"
        new_options.push(*value.map(&:to_s))
        next
      else
        value = value.join(',')
      end
    end
    new_options << "--#{key.to_s.tr('_', '-')}"
    new_options << value&.to_s if value
  end
end

#versionObject



41
42
43
44
45
46
47
# File 'lib/periphery/runner.rb', line 41

def version
  arguments = [binary_path, 'version']
  stdout, stderr, status = Open3.capture3(*arguments)
  raise "error: #{arguments} existed with status code #{status.exitstatus}. #{stderr}" unless status.success?

  stdout.strip
end