Skip to content

Commit

Permalink
Merge pull request #1271 from jerearista/feature-json-content
Browse files Browse the repository at this point in the history
Enable the json resource to accept command output or JSON content
  • Loading branch information
chris-rock committed Nov 2, 2016
2 parents 8067eca + f8a9eab commit 408a180
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions lib/resources/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,47 @@ class JsonConfig < Inspec.resource(1)
describe json('policyfile.lock.json') do
its(['cookbook_locks','omnibus','version']) { should eq('2.2.0') }
end
describe json({ command: 'retrieve_data.py --json' }) do
its('state') { should eq('open') }
end
describe json({ content: '{\"item1\": { \"status\": \"available\" } }' }) do
its(['item1', 'status']) { should cmp 'available' }
end
"

include ObjectTraverser

# make params readable
attr_reader :params

def initialize(path)
@path = path
@file = inspec.file(@path)
@file_content = @file.content
def initialize(opts)
@opts = opts
if opts.is_a?(Hash)
if opts.key?(:content)
@file_content = opts[:content]
elsif opts.key?(:command)
@command = inspec.command(opts[:command])
@file_content = @command.stdout
end
else
@path = opts
@file = inspec.file(@opts)
@file_content = @file.content

# check if file is available
if !@file.file?
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end
# check if file is available
if !@file.file?
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end

# check if file is readable
if @file_content.empty? && @file.size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
# check if file is readable
if @file_content.empty? && @file.size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
end
end

@params = parse(@file_content)
Expand Down Expand Up @@ -61,7 +80,11 @@ def method_missing(*keys)
end

def to_s
"Json #{@path}"
if @opts.is_a?(Hash) && @opts.key?(:content)
'Json content'
else
"Json #{@path}"
end
end
end
end

0 comments on commit 408a180

Please sign in to comment.