Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update shell detect to work with platforms #2712

Merged
merged 3 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/inspec/base_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ def self.validate_reporters(reporters)
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)
Expand Down
12 changes: 1 addition & 11 deletions lib/inspec/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,7 @@ def detect
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
Expand Down
5 changes: 1 addition & 4 deletions lib/inspec/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def configure_pry # rubocop:disable Metrics/AbcSize
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.
Expand Down Expand Up @@ -105,9 +104,7 @@ def print_target_info
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

Expand Down
14 changes: 7 additions & 7 deletions test/functional/inspec_detect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

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
Expand All @@ -23,9 +23,9 @@

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
Expand Down
10 changes: 10 additions & 0 deletions test/unit/base_cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
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)

Expand Down