Class: Inspec::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/shell.rb

Instance Method Summary collapse

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_pryObject



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 { "\e[0;32m#{Pry.config.prompt_name}>\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



66
67
68
69
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
# File 'lib/inspec/shell.rb', line 66

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

    ctx = @runner.backend
    puts <<EOF

Available commands:

`[resource]` - run resource on target machine
`help resources` - show all available resources that can be used as commands
`help [resource]` - information about a specific resource
`exit` - exit the InSpec shell

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 family:  #{mark ctx.os[:family] || 'unknown'}
OS release: #{mark ctx.os[:release] || 'unknown'}

EOF
  elsif resource == 'resources'
    resources
  elsif !Inspec::Resource.registry[resource].nil?
    puts <<EOF
#{mark 'Name:'} #{resource}

#{mark 'Description:'}

#{Inspec::Resource.registry[resource].desc}

#{mark 'Example:'}
#{print_example(Inspec::Resource.registry[resource].example)}

#{mark 'Web Reference:'}

https://github.com/chef/inspec/blob/master/docs/resources.rst##{resource}

EOF
  else
    puts 'Only the following resources are available:'
    resources
  end
end

#introObject



60
61
62
63
64
# File 'lib/inspec/shell.rb', line 60

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



43
44
45
# File 'lib/inspec/shell.rb', line 43

def mark(x)
  "\033[1m#{x}\033[0m"
end


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/inspec/shell.rb', line 47

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

#resourcesObject



114
115
116
# File 'lib/inspec/shell.rb', line 114

def resources
  puts Inspec::Resource.registry.keys.join(' ')
end

#startObject



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: __FILE__, line: __LINE__ }
  @runner.add_content(c, [])
  @runner.run
end