Class: Inspec::Shell
- Inherits:
-
Object
- Object
- Inspec::Shell
- Defined in:
- lib/inspec/shell.rb
Instance Method Summary collapse
- #configure_pry ⇒ Object
- #help(resource = nil) ⇒ Object
-
#initialize(runner) ⇒ Shell
constructor
A new instance of Shell.
- #intro ⇒ Object
- #mark(x) ⇒ Object
- #print_example(example) ⇒ Object
- #readline_ignore(code) ⇒ Object
- #resources ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(runner) ⇒ Shell
Returns a new instance of Shell.
9 10 11 12 13 14 |
# File 'lib/inspec/shell.rb', line 9 def initialize(runner) @runner = runner # load and configure pry require 'pry' configure_pry end |
Instance Method Details
#configure_pry ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/inspec/shell.rb', line 23 def configure_pry # Remove all hooks and checks Pry.hooks.clear_all that = self # Add the help command Pry::Commands.block_command 'help', 'Show examples' do |resource| that.help(resource) end # configure pry shell prompt Pry.config.prompt_name = 'inspec' Pry.prompt = [proc { "#{readline_ignore("\e[0;32m")}#{Pry.config.prompt_name}> #{readline_ignore("\e[0m")}" }] # Add a help menu as the default intro Pry.hooks.add_hook(:before_session, :intro) do intro end end |
#help(resource = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/inspec/shell.rb', line 70 def help(resource = nil) if resource.nil? ctx = @runner.backend puts "\nAvailable commands:\n\n`[resource]` - run resource on target machine\n`help resources` - show all available resources that can be used as commands\n`help [resource]` - information about a specific resource\n`exit` - exit the InSpec shell\n\nYou can use resources in this environment to test the target machine. For example:\n\ncommand('uname -a').stdout\nfile('/proc/cpuinfo').content => \"value\",\n\nYou are currently running on:\n\nOS platform: \#{mark ctx.os[:name] || 'unknown'}\nOS family: \#{mark ctx.os[:family] || 'unknown'}\nOS release: \#{mark ctx.os[:release] || 'unknown'}\n\n" elsif resource == 'resources' resources elsif !Inspec::Resource.registry[resource].nil? puts "\#{mark 'Name:'} \#{resource}\n\n\#{mark 'Description:'}\n\n\#{Inspec::Resource.registry[resource].desc}\n\n\#{mark 'Example:'}\n\#{print_example(Inspec::Resource.registry[resource].example)}\n\n\#{mark 'Web Reference:'}\n\nhttps://github.com/chef/inspec/blob/master/docs/resources.rst#\#{resource}\n\n" else puts 'Only the following resources are available:' resources end end |
#intro ⇒ Object
64 65 66 67 68 |
# File 'lib/inspec/shell.rb', line 64 def intro puts 'Welcome to the interactive InSpec Shell' puts "To find out how to use it, type: #{mark 'help'}" puts end |
#mark(x) ⇒ Object
47 48 49 |
# File 'lib/inspec/shell.rb', line 47 def mark(x) "#{readline_ignore("\033[1m")}#{x}#{readline_ignore("\033[0m")}" end |
#print_example(example) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/inspec/shell.rb', line 51 def print_example(example) # determine min whitespace that can be removed min = nil example.lines.each do |line| if line.strip.length > 0 # ignore empty lines line_whitespace = line.length - line.lstrip.length min = line_whitespace if min.nil? || line_whitespace < min end end # remove whitespace from each line example.gsub(/\n\s{#{min}}/, "\n") end |
#readline_ignore(code) ⇒ Object
43 44 45 |
# File 'lib/inspec/shell.rb', line 43 def readline_ignore(code) "\001#{code}\002" end |
#resources ⇒ Object
119 120 121 |
# File 'lib/inspec/shell.rb', line 119 def resources puts Inspec::Resource.registry.keys.join(' ') end |
#start ⇒ Object
16 17 18 19 20 21 |
# File 'lib/inspec/shell.rb', line 16 def start # store context to run commands in this context c = { content: 'binding.pry', ref: nil, line: nil } @runner.add_content(c, []) @runner.run end |