Class: Inspec::InspecCLI
  
  
  
  
  
    - Inherits:
- 
      BaseCLI
      
        
          - Object
- Thor
- BaseCLI
- Inspec::InspecCLI
 show all
      - Includes:
- LicenseAcceptance::CLIFlags::Thor
    - Defined in:
- lib/inspec/cli.rb
 
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  Methods inherited from BaseCLI
  check_license!, exec_options, exit_on_failure?, format_platform_info, help, profile_options, start, target_options
  
  
    Instance Method Details
    
      
  
  
    #archive(path)  ⇒ Object 
  
  
  
  
    | 
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 | # File 'lib/inspec/cli.rb', line 171
def archive(path)
  o = config
  diagnose(o)
  o[:logger] = Logger.new($stdout)
  o[:logger].level = get_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)
  result = profile.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 | 
 
    
      
  
  
    #check(path)  ⇒ Object 
  
  
  
  
    
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
   
 
  
  
    | 
96
97
98
99
100
101
102
103
104
105
106
107
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 | # File 'lib/inspec/cli.rb', line 96
def check(path)   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 = 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
    if result[:errors].empty? && result[:warnings].empty?
      ui.plain_line("No errors or warnings")
    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
      errors = ui.red("#{result[:errors].length} errors", print: false)
      warnings = ui.yellow("#{result[:warnings].length} warnings", print: false)
      ui.plain_line("Summary:     #{errors}, #{warnings}")
    end
  end
  ui.exit Inspec::UI::EXIT_USAGE_ERROR unless result[:summary][:valid]
rescue StandardError => e
  pretty_handle_exception(e)
end | 
 
    
      
  
  
    #clear_cache  ⇒ Object 
  
  
  
  
    | 
408
409
410
411
412
413
414
415
416
417 | # File 'lib/inspec/cli.rb', line 408
def 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 
  
  
  
  
    | 
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 | # File 'lib/inspec/cli.rb', line 292
def detect
  o = 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)
  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 | 
 
    
      
  
  
    #env(shell = nil)  ⇒ Object 
  
  
  
  
    | 
370
371
372
373
374
375 | # File 'lib/inspec/cli.rb', line 370
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 
  
  
  
  
    | 
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 | # File 'lib/inspec/cli.rb', line 273
def exec(*targets)
  o = config
  diagnose(o)
  configure_logger(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 | 
 
    
      
  
  
    #json(target)  ⇒ Object 
  
  
  
  
    | 
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 | # File 'lib/inspec/cli.rb', line 69
def json(target)
  require "json" unless defined?(JSON)
  o = config
  diagnose(o)
  o["log_location"] = $stderr
  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(target, o)
  dst = o[:output].to_s
    Inspec::Utils::JsonProfileSummary.produce_json(
    info: profile.info,
    write_path: dst
  )
rescue StandardError => e
  pretty_handle_exception(e)
end | 
 
    
      
    
      
    
      
  
  
    #shell_func  ⇒ Object 
  
  
  
  
    | 
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367 | # File 'lib/inspec/cli.rb', line 332
def shell_func
  o = config
  diagnose(o)
  o[:debug_shell] = true
  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 | 
 
    
      
  
  
    #vendor(path = nil)  ⇒ Object 
  
  
  
  
    | 
148
149
150
151
152
153
154
155 | # File 'lib/inspec/cli.rb', line 148
def vendor(path = nil)
  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 
  
  
  
  
    | 
395
396
397
398
399
400
401
402 | # File 'lib/inspec/cli.rb', line 395
def version
  if config["format"] == "json"
    v = { version: Inspec::VERSION }
    puts v.to_json
  else
    puts Inspec::VERSION
  end
end |