Class: Inspec::InspecCLI
Instance Method Summary
collapse
Methods inherited from BaseCLI
default_options, exec_options, profile_options, target_options
Instance Method Details
#archive(path) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/inspec/cli.rb', line 130
def archive(path)
diagnose
o = opts.dup
o[:logger] = Logger.new(STDOUT)
o[:logger].level = get_log_level(o.log_level)
o[:backend] = Inspec::Backend.create(target: 'mock://')
profile = Inspec::Profile.for_target(path, o)
result = profile.check
if result && !opts[:ignore_errors] == false
o[:logger].info 'Profile check failed. Please fix the profile before generating an archive.'
return exit 1
end
exit 1 unless profile.archive(opts)
rescue StandardError => e
pretty_handle_exception(e)
end
|
#check(path) ⇒ Object
rubocop:disable Metrics/AbcSize
61
62
63
64
65
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
|
# File 'lib/inspec/cli.rb', line 61
def check(path) diagnose
o = opts.dup
o[:ignore_supports] = true o[:backend] = Inspec::Backend.create(target: 'mock://')
o[:check_mode] = true
profile = Inspec::Profile.for_target(path, o)
result = profile.check
if opts['format'] == 'json'
puts JSON.generate(result)
else
%w{location profile controls timestamp valid}.each do |item|
puts format('%-12s %s', item.to_s.capitalize + ':',
mark_text(result[:summary][item.to_sym]))
end
puts
if result[:errors].empty? and result[:warnings].empty?
puts 'No errors or warnings'
else
red = "\033[31m"
yellow = "\033[33m"
rst = "\033[0m"
item_msg = lambda { |item|
pos = [item[:file], item[:line], item[:column]].compact.join(':')
pos.empty? ? item[:msg] : pos + ': ' + item[:msg]
}
result[:errors].each do |item|
puts "#{red} ✖ #{item_msg.call(item)}#{rst}"
end
result[:warnings].each do |item|
puts "#{yellow} ! #{item_msg.call(item)}#{rst}"
end
puts
puts format('Summary: %s%d errors%s, %s%d warnings%s',
red, result[:errors].length, rst,
yellow, result[:warnings].length, rst)
end
end
exit 1 unless result[:summary][:valid]
rescue StandardError => e
pretty_handle_exception(e)
end
|
#detect ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/inspec/cli.rb', line 175
def detect
o = opts.dup
o[:command] = 'os.params'
(_, res) = run_command(o)
if opts['format'] == 'json'
puts res.to_json
else
headline('Operating System Details')
%w{name family release arch}.each { |item|
puts format('%-10s %s', item.to_s.capitalize + ':',
mark_text(res[item.to_sym]))
}
end
rescue StandardError => e
pretty_handle_exception(e)
end
|
#env(shell = nil) ⇒ Object
229
230
231
232
233
234
|
# File 'lib/inspec/cli.rb', line 229
def env(shell = nil)
p = Inspec::EnvPrinter.new(self.class, shell)
p.print_and_exit!
rescue StandardError => e
pretty_handle_exception(e)
end
|
#exec(*targets) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/inspec/cli.rb', line 154
def exec(*targets)
diagnose
o = opts(:exec).dup
configure_logger(o)
if o.key?('cache')
o[:vendor_cache] = o[:cache]
o[:logger].warn '[DEPRECATED] The use of `--cache` is being deprecated in InSpec 2.0. Please use `--vendor-cache` instead.'
end
run_tests(targets, o)
rescue StandardError => e
pretty_handle_exception(e)
end
|
#json(target) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/inspec/cli.rb', line 34
def json(target)
diagnose
o = opts.dup
o[:ignore_supports] = true
o[:backend] = Inspec::Backend.create(target: 'mock://')
o[:check_mode] = true
profile = Inspec::Profile.for_target(target, o)
dst = o[:output].to_s
if dst.empty?
puts JSON.dump(profile.info)
else
if File.exist? dst
puts "----> updating #{dst}"
else
puts "----> creating #{dst}"
end
fdst = File.expand_path(dst)
File.write(fdst, JSON.dump(profile.info))
end
rescue StandardError => e
pretty_handle_exception(e)
end
|
#schema(name) ⇒ Object
237
238
239
240
241
242
|
# File 'lib/inspec/cli.rb', line 237
def schema(name)
puts Inspec::Schema.json(name)
rescue StandardError => e
puts e
puts "Valid schemas are #{Inspec::Schema.names.join(', ')}"
end
|
#shell_func ⇒ Object
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/inspec/cli.rb', line 200
def shell_func
diagnose
o = opts.dup
o[:debug_shell] = true
json_output = ['json', 'json-min'].include?(opts['format'])
log_device = json_output ? nil : STDOUT
o[:logger] = Logger.new(log_device)
o[:logger].level = get_log_level(o.log_level)
if o[:command].nil?
runner = Inspec::Runner.new(o)
return Inspec::Shell.new(runner).start
end
run_type, res = run_command(o)
exit res unless run_type == :ruby_eval
res = (res.respond_to?(:to_json) ? res.to_json : JSON.dump(res)) if json_output
puts res
exit 0
rescue RuntimeError, Train::UserError => e
$stderr.puts e.message
rescue StandardError => e
pretty_handle_exception(e)
end
|
#vendor(path = nil) ⇒ Object
113
114
115
116
|
# File 'lib/inspec/cli.rb', line 113
def vendor(path = nil)
o = opts.dup
vendor_deps(path, o)
end
|
#version ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/inspec/cli.rb', line 246
def version
if opts['format'] == 'json'
v = { version: Inspec::VERSION }
puts v.to_json
else
puts Inspec::VERSION
latest = LatestInSpecVersion.new.latest
if Gem::Version.new(Inspec::VERSION) < Gem::Version.new(latest)
puts "\nYour version of InSpec is out of date! The latest version is #{latest}."
end
end
end
|