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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inspec/cli.rb', line 105

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



47
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inspec/cli.rb', line 47

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
    %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]
end

#detectObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/inspec/cli.rb', line 137

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
end

#exec(*targets) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/inspec/cli.rb', line 126

def exec(*targets)
  diagnose
  o = opts.dup

  # run tests
  run_tests(targets, o)
end

#json(target) ⇒ Object



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

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/inspec/cli.rb', line 156

def shell_func
  diagnose
  o = opts.dup
  o[:logger] = Logger.new(STDOUT)
  o[:logger].level = get_log_level(o.log_level)
  if o[:command].nil?
    runner = Inspec::Runner.new(o)
    return Inspec::Shell.new(runner).start
  else
    res = run_command(o)
    jres = res.respond_to?(:to_json) ? res.to_json : JSON.dump(res)
    puts jres
  end
rescue RuntimeError, Train::UserError => e
  $stderr.puts e.message
end

#versionObject



174
175
176
# File 'lib/inspec/cli.rb', line 174

def version
  puts Inspec::VERSION
end