Class: Inspec::InspecCLI
- Inherits:
-
BaseCLI
- Object
- Thor
- BaseCLI
- Inspec::InspecCLI
show all
- Includes:
- ChefLicensing::CLIFlags::Thor, LicenseAcceptance::CLIFlags::Thor
- Defined in:
- lib/inspec/cli.rb
Instance Method Summary
collapse
Methods inherited from BaseCLI
audit_log_options, check_license!, exec_options, exit_on_failure?, fetch_and_persist_license, format_platform_info, help, profile_options, start, supermarket_options, target_options
Instance Method Details
#archive(path, log_level = nil) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/inspec/cli.rb', line 283
def archive(path, log_level = nil)
Inspec.with_feature("inspec-cli-archive") {
begin
o = config
diagnose(o)
o[:logger] = Logger.new($stdout)
o[:logger].level = get_log_level(log_level || o[:log_level])
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
vendor_options = o.dup
vendor_options[:overwrite] = true
vendor_deps(path, vendor_options)
profile = Inspec::Profile.for_target(path, o)
gem_deps = profile.metadata.gem_dependencies + \
profile.locked_dependencies.list.map { |_k, v| v.profile.metadata.gem_dependencies }.flatten
unless gem_deps.empty?
o[:logger].warn "Archiving a profile that contains gem dependencies, but InSpec cannot package gems with the profile! Please archive your ~/.inspec/gems directory separately."
end
result = profile.check if o[:check]
if result && !o[:ignore_errors] == false
o[:logger].info "Profile check failed. Please fix the profile before generating an archive."
return ui.exit Inspec::UI::EXIT_USAGE_ERROR
end
ui.exit Inspec::UI::EXIT_USAGE_ERROR unless profile.archive(o)
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#check(path) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/inspec/cli.rb', line 181
def check(path)
Inspec.with_feature("inspec-cli-check") {
begin
o = config
diagnose(o)
o["log_location"] ||= STDERR if o["format"] == "json"
o["log_level"] ||= "warn"
configure_logger(o)
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
o[:check_mode] = true
o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])
profile = Inspec::Profile.for_target(path, o)
result = o[:legacy_check] ? profile.legacy_check : profile.check
if o["format"] == "json"
puts JSON.generate(result)
else
%w{location profile controls timestamp valid}.each do |item|
prepared_string = format("%-12s %s",
"#{item.to_s.capitalize} :",
result[:summary][item.to_sym])
ui.plain_line(prepared_string)
end
puts
enable_offenses = !Inspec.locally_windows?
if result[:errors].empty? && result[:warnings].empty? && result[:offenses].empty?
if enable_offenses
ui.plain_line("No errors, warnings, or offenses")
else
ui.plain_line("No errors or warnings")
end
else
item_msg = lambda { |item|
pos = [item[:file], item[:line], item[:column]].compact.join(":")
pos.empty? ? item[:msg] : pos + ": " + item[:msg]
}
result[:errors].each { |item| ui.red " #{Inspec::UI::GLYPHS[:script_x]} #{item_msg.call(item)}\n" }
result[:warnings].each { |item| ui.yellow " ! #{item_msg.call(item)}\n" }
puts
if enable_offenses && !result[:offenses].empty?
puts "Offenses:\n"
result[:offenses].each { |item| ui.cyan(" #{Inspec::UI::GLYPHS[:script_x]} #{item_msg.call(item)}\n\n") }
end
offenses = ui.cyan("#{result[:offenses].length} offenses", print: false)
errors = ui.red("#{result[:errors].length} errors", print: false)
warnings = ui.yellow("#{result[:warnings].length} warnings", print: false)
if enable_offenses
ui.plain_line("Summary: #{errors}, #{warnings}, #{offenses}")
else
ui.plain_line("Summary: #{errors}, #{warnings}")
end
end
end
ui.exit Inspec::UI::EXIT_USAGE_ERROR unless result[:summary][:valid]
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#clear_cache ⇒ Object
571
572
573
574
575
576
577
578
579
580
581
582
|
# File 'lib/inspec/cli.rb', line 571
def clear_cache
Inspec.with_feature("inspec-cli-clear-cache") {
o = config
configure_logger(o)
cache_path = o[:vendor_cache] || "~/.inspec/cache"
FileUtils.rm_r Dir.glob(File.expand_path(cache_path))
o[:logger] = Logger.new($stdout)
o[:logger].level = get_log_level(o[:log_level])
o[:logger].info "== InSpec cache cleared successfully =="
}
end
|
#detect ⇒ Object
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/inspec/cli.rb', line 425
def detect
Inspec.with_feature("inspec-cli-detect") {
begin
o = config
deprecate_target_id(config)
o[:command] = "platform.params"
configure_logger(o)
(_, res) = run_command(o)
if o["format"] == "json"
puts res.to_json
else
ui.headline("Platform Details")
ui.plain Inspec::BaseCLI.format_platform_info(params: res, indent: 0, color: 36, enable_color: ui.color?)
end
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message
ui.exit Inspec::UI::EXIT_USAGE_ERROR
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#env(shell = nil) ⇒ Object
519
520
521
522
523
524
525
526
527
528
|
# File 'lib/inspec/cli.rb', line 519
def env(shell = nil)
Inspec.with_feature("inspec-cli-env") {
begin
p = Inspec::EnvPrinter.new(self.class, shell)
p.print_and_exit!
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#exec(*targets) ⇒ Object
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
# File 'lib/inspec/cli.rb', line 396
def exec(*targets)
Inspec.with_feature("inspec-cli-exec") {
begin
o = config
diagnose(o)
deprecate_target_id(config)
configure_logger(o)
Inspec.with_feature("inspec-audit-logging") {
set_and_validate_audit_log_options(o)
}
runner = Inspec::Runner.new(o)
targets.each { |target| runner.add_target(target) }
ui.exit runner.run
rescue ArgumentError, RuntimeError, Train::UserError => e
$stderr.puts e.message
ui.exit Inspec::UI::EXIT_USAGE_ERROR
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#export(target, as_json = false) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/inspec/cli.rb', line 108
def export(target, as_json = false)
Inspec.with_feature("inspec-cli-export") {
begin
o = config
diagnose(o)
o["log_location"] = $stderr
configure_logger(o)
what = o[:what].dup || "profile"
what.downcase!
raise Inspec::Error.new("Unrecognized option '#{what}' for --what - expected one of profile, readme, or metadata.") unless %w{profile readme metadata}.include?(what)
default_format_for_what = {
"profile" => "yaml",
"metadata" => "raw",
"readme" => "raw",
}
valid_formats_for_what = {
"profile" => %w{yaml json},
"metadata" => %w{yaml raw},
"readme" => ["raw"],
}
format = o[:format] || default_format_for_what[what]
format = "json" if as_json
raise Inspec::Error.new("Invalid option '#{format}' for --format and --what combination") unless format && valid_formats_for_what[what].include?(format)
o[:backend] = Inspec::Backend.create(Inspec::Config.mock)
o[:check_mode] = true
o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])
profile = Inspec::Profile.for_target(target, o)
dst = o[:output].to_s
case what
when "profile"
profile_info = o[:legacy_export] ? profile.info : profile.info_from_parse
if format == "json"
require "json" unless defined?(JSON)
Inspec::Utils::JsonProfileSummary.produce_json(
info: profile_info,
write_path: dst
)
elsif format == "yaml"
Inspec::Utils::YamlProfileSummary.produce_yaml(
info: profile_info,
write_path: dst
)
end
when "readme"
out = dst.empty? ? $stdout : File.open(dst, "w")
out.write(profile.readme)
when "metadata"
out = dst.empty? ? $stdout : File.open(dst, "w")
out.write(profile.metadata_src)
end
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#json(target) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/inspec/cli.rb', line 82
def json(target)
Inspec.with_feature("inspec-cli-json") {
config
Inspec.deprecate(:renamed_to_inspec_export)
export(target, true)
}
end
|
#run_context ⇒ Object
547
548
549
550
551
552
|
# File 'lib/inspec/cli.rb', line 547
def run_context
Inspec.with_feature("inspec-cli-run-context") {
require "inspec/utils/telemetry/run_context_probe"
puts Inspec::Telemetry::RunContextProbe.guess_run_context
}
end
|
#schema(name) ⇒ Object
533
534
535
536
537
538
539
540
541
542
543
544
|
# File 'lib/inspec/cli.rb', line 533
def schema(name)
Inspec.with_feature("inspec-cli-schema") {
begin
require "inspec/schema/output_schema"
o = config
puts Inspec::Schema::OutputSchema.json(name, o)
rescue StandardError => e
puts e
puts "Valid schemas are #{Inspec::Schema::OutputSchema.names.join(", ")}"
end
}
end
|
#shell_func ⇒ Object
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/inspec/cli.rb', line 473
def shell_func
Inspec.with_feature("inspec-cli-shell") {
begin
o = config
deprecate_target_id(config)
diagnose(o)
o[:debug_shell] = true
Inspec.with_feature("inspec-audit-logging") {
set_and_validate_audit_log_options(o)
}
Inspec::Resource.toggle_inspect unless o[:inspect]
log_device = suppress_log_output?(o) ? 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)
ui.exit res unless run_type == :ruby_eval
reporters = o["reporter"] || {}
if reporters.keys.include?("json")
res = if res.respond_to?(:to_json)
res.to_json
else
JSON.dump(res)
end
end
puts res
ui.exit Inspec::UI::EXIT_NORMAL
rescue RuntimeError, Train::UserError => e
$stderr.puts e.message
rescue StandardError => e
pretty_handle_exception(e)
end
}
end
|
#vendor(path = nil) ⇒ Object
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/inspec/cli.rb', line 252
def vendor(path = nil)
Inspec.with_feature("inspec-cli-vendor") {
o = config
configure_logger(o)
o[:logger] = Logger.new($stdout)
o[:logger].level = get_log_level(o[:log_level])
vendor_deps(path, o)
}
end
|
#version ⇒ Object
556
557
558
559
560
561
562
563
564
565
|
# File 'lib/inspec/cli.rb', line 556
def version
Inspec.with_feature("inspec-cli-version") {
if config["format"] == "json"
v = { version: Inspec::VERSION }
puts v.to_json
else
puts Inspec::VERSION
end
}
end
|