Update shell detect to work with platforms (#2712)

* Update shell to use the same detect logic as cli detect.

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2018-02-20 07:37:23 -05:00 committed by GitHub
parent bd7327b91c
commit 378e7c5048
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 22 deletions

View file

@ -167,6 +167,23 @@ module Inspec
raise ArgumentError, 'The option --reporter can only have a single report outputting to stdout.' if stdout > 1
end
def self.detect(params: {}, indent: 0, color: 39)
str = ''
params.each { |item, info|
data = info
# Format Array for better output if applicable
data = data.join(', ') if data.is_a?(Array)
# Do not output fields of data is missing ('unknown' is fine)
next if data.nil?
data = "\e[1m\e[#{color}m#{data}\e[0m"
str << format("#{' ' * indent}%-10s %s\n", item.to_s.capitalize + ':', data)
}
str
end
private
def suppress_log_output?(opts)

View file

@ -184,17 +184,7 @@ class Inspec::InspecCLI < Inspec::BaseCLI
puts res.to_json
else
headline('Platform Details')
%w{name families release arch}.each { |item|
data = res[item.to_sym]
# Format Array for better output if applicable
data = data.join(', ') if data.is_a?(Array)
# Do not output fields of data is missing ('unknown' is fine)
next if data.nil?
puts format('%-10s %s', item.to_s.capitalize + ':', mark_text(data))
}
puts Inspec::BaseCLI.detect(params: res, indent: 0, color: 36)
end
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message

View file

@ -47,7 +47,6 @@ module Inspec
Pry.hooks.add_hook(:before_session, 'inspec_intro') do
intro
print_target_info
puts
end
# Track the rules currently registered and what their merge count is.
@ -105,9 +104,7 @@ module Inspec
puts <<~EOF
You are currently running on:
OS platform: #{mark ctx.os[:name] || 'unknown'}
OS family: #{mark ctx.os[:family] || 'unknown'}
OS release: #{mark ctx.os[:release] || 'unknown'}
#{Inspec::BaseCLI.detect(params: ctx.platform.params, indent: 4, color: 39)}
EOF
end

View file

@ -10,10 +10,10 @@ describe 'inspec detect' do
stdout = res.stdout
stdout.must_include "\n== Platform Details\n\n"
stdout.must_include "\nName: \e[0;36m"
stdout.must_include "\nFamilies: \e[0;36m"
stdout.must_include "\nArch: \e[0;36m"
stdout.must_include "\nRelease: \e[0;36m"
stdout.must_include "\nName: \e[1m\e[36m"
stdout.must_include "\nFamilies: \e[1m\e[36m"
stdout.must_include "\nArch: \e[1m\e[36m"
stdout.must_include "\nRelease: \e[1m\e[36m"
end
it 'outputs the correct data when target the target an API' do
@ -23,9 +23,9 @@ describe 'inspec detect' do
stdout = res.stdout
stdout.must_include "\n== Platform Details\n\n"
stdout.must_include "\nName: \e[0;36m"
stdout.must_include "\nFamilies: \e[0;36m"
stdout.must_include "\nRelease: \e[0;36m"
stdout.must_include "\nName: \e[1m\e[36m"
stdout.must_include "\nFamilies: \e[1m\e[36m"
stdout.must_include "\nRelease: \e[1m\e[36m"
stdout.wont_include "\nArch:"
end

View file

@ -20,6 +20,16 @@ describe 'BaseCLI' do
opts.must_equal expected
end
it 'verify platform detect' do
hash = { name: 'test-os', families: 'aws, cloud', release: 'aws-sdk-v1' }
expect = <<EOF
Name: \e[1m\e[35mtest-os\e[0m
Families: \e[1m\e[35maws, cloud\e[0m
Release: \e[1m\e[35maws-sdk-v1\e[0m
EOF
_(Inspec::BaseCLI.detect(params: hash, indent: 2, color: 35)).must_equal expect
end
it 'json-config options override cli defaults' do
Inspec::BaseCLI.stubs(:default_options).returns(default_options)