Search code examples
rubyinspec

inspec - i want to output structured data to be parsed by another function


I have a inspec test, this is great:

inspec exec scratchpad/profiles/forum_profile --reporter yaml

Trouble is I want to run this in a script and output this to an array

I cannot find the documentation that indicated what method i need to use to simulate the same

I do this

def my_func
  http_checker = Inspec::Runner.new()
  http_checker.add_target('scratchpad/profiles/forum_profile')
  http_checker.run
  puts http_checker.report

So the report method seems to give me load of the equivalent type and much more - does anyone have any documentation or advice on returning the same output as the --reporter yaml type response but in a script? I want to parse the response so I can share output with another function


Solution

  • I've never touched inspec, so take the following with a grain of salt, but according to https://github.com/inspec/inspec/blob/master/lib/inspec/runner.rb#L140, you can provide reporter option while instantiating the runner. Looking at https://github.com/inspec/inspec/blob/master/lib/inspec/reporters.rb#L11 I think it should be smth. like ["yaml", {}]. So, could you please try

    # ...
    http_checker = Inspec::Runner.new(reporter: ["yaml", {}])
    # ...
    

    (chances are it will give you the desired output)