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



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

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



46
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
# File 'lib/inspec/cli.rb', line 46

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



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

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 "#{mark_text(item.to_s.capitalize + ':')} #{res[item.to_sym]}"
    }
  end
end

#exec(*targets) ⇒ Object



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

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

  # run tests
  run_tests(targets, o)
end

#json(target) ⇒ Object



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

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



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

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



156
157
158
# File 'lib/inspec/cli.rb', line 156

def version
  puts Inspec::VERSION
end