Skip to content

Commit

Permalink
Provide target info on shell invocation
Browse files Browse the repository at this point in the history
When in inspec shell, you need to type the `help` command to find out info
about your target system. This info would be super helpful right out of the
gate so users have confidence that they're targeting the correct system.

The target info is still available via the `help` command as it always has
been, as well.

Signed-off-by: Adam Leff <[email protected]>
  • Loading branch information
adamleff committed Feb 8, 2017
1 parent 243d6d3 commit c22d491
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/inspec/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def configure_pry # rubocop:disable Metrics/AbcSize
# Add a help menu as the default intro
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 @@ -94,10 +96,20 @@ def intro
puts
end

def print_target_info
ctx = @runner.backend
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'}
EOF
end

def help(resource = nil)
if resource.nil?

ctx = @runner.backend
puts <<EOF
Available commands:
Expand All @@ -110,14 +122,9 @@ def help(resource = nil)
You can use resources in this environment to test the target machine. For example:
command('uname -a').stdout
file('/proc/cpuinfo').content => "value",
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'}
file('/proc/cpuinfo').content => "value"
#{print_target_info}
EOF
elsif resource == 'resources'
resources
Expand Down
5 changes: 5 additions & 0 deletions test/functional/inspec_shell_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def do_shell(code, exit_status = 0, stderr = '')
out
end

it 'displays the target device information for the user without requiring the help command' do
out = do_shell('1+1')
out.stdout.must_include 'You are currently running on:'
end

it 'provides a help command' do
out = do_shell('help')
out.stdout.must_include 'Available commands:'
Expand Down

0 comments on commit c22d491

Please sign in to comment.