Class: Inspec::InspecCLI

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

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods inherited from BaseCLI

exec_options, profile_options, target_options

Instance Method Details

#archive(path) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inspec/cli.rb', line 90

def archive(path)
  diagnose

  o = opts.dup
  o[:logger] = Logger.new(STDOUT)
  o[:logger].level = get_log_level(o.log_level)

  profile = Inspec::Profile.for_target(path, o)
  result = profile.check

  if result && !opts[:ignore_errors] == false
    @logger.info 'Profile check failed. Please fix the profile before generating an archive.'
    return exit 1
  end

  # generate archive
  exit 1 unless profile.archive(opts)
end

#check(path) ⇒ Object

rubocop:disable Metrics/AbcSize



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/inspec/cli.rb', line 48

def check(path) # rubocop:disable Metrics/AbcSize
  diagnose
  o = opts.dup
  # configure_logger(o) # we do not need a logger for check yet
  o[:ignore_supports] = true # we check for integrity only

  # run check
  profile = Inspec::Profile.for_target(path, o)
  result = profile.check

  if opts['format'] == 'json'
    puts JSON.generate(result)
  else
    headline('Summary')
    %w{location profile controls timestamp valid}.each { |item|
      puts "#{mark_text(item.to_s.capitalize + ':')} #{result[:summary][item.to_sym]}"
    }
    puts

    %w{errors warnings}.each { |list|
      headline(list.to_s.capitalize)
      result[list.to_sym].each { |item|
        puts "#{item[:file]}:#{item[:line]}:#{item[:column]}: #{item[:msg]} "
      }
      puts
    }
  end
  exit 1 unless result[:summary][:valid]
end

#detectObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/inspec/cli.rb', line 118

def detect
  diagnose

  rel = File.join(File.dirname(__FILE__), *%w{.. utils detect.rb})
  detect_util = File.expand_path(rel)
  # exits on execution:
  runner = Inspec::Runner.new(opts)
  profile = Inspec::Profile.for_target(detect_util, opts)
  runner.add_profile(profile)
  exit runner.run
rescue RuntimeError => e
  puts e.message
end

#exec(*targets) ⇒ Object



111
112
113
114
# File 'lib/inspec/cli.rb', line 111

def exec(*targets)
  diagnose
  run_tests(targets, opts)
end

#json(target) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/inspec/cli.rb', line 25

def json(target)
  diagnose
  o = opts.dup
  o[:ignore_supports] = 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
end

#shell_funcObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inspec/cli.rb', line 135

def shell_func
  diagnose
  o = opts.dup
  o[:logger] = Logger.new(STDOUT)
  o[:logger].level = get_log_level(o.log_level)

  runner = Inspec::Runner.new(o)
  Inspec::Shell.new(runner).start
rescue RuntimeError => e
  puts e.message
end

#versionObject



148
149
150
# File 'lib/inspec/cli.rb', line 148

def version
  puts Inspec::VERSION
end