Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable to configure the logger via cli #405

Merged
merged 1 commit into from Feb 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions bin/inspec
Expand Up @@ -42,6 +42,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength
desc: 'Allow remote scans with self-signed certificates (WinRM).'
option :json_config, type: :string,
desc: 'Read configuration from JSON file (`-` reads from stdin).'
option :log_level, aliases: :l, type: :string,
desc: 'Set the log level: info (default), debug, warn, error'
end

desc 'json PATH', 'read all tests in PATH and generate a JSON summary'
Expand Down Expand Up @@ -75,6 +77,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength

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

o[:ignore_supports] = true # we check for integrity only
profile = Inspec::Profile.from_path(path, o)
exit 1 unless profile.check
Expand All @@ -94,6 +98,8 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength

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

profile = Inspec::Profile.from_path(path, o)
# generate archive
exit 1 unless profile.archive(opts)
Expand All @@ -109,6 +115,7 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength

o = opts.dup
o[:logger] = Logger.new(opts['format'] == 'json' ? nil : STDOUT)
o[:logger].level = get_log_level(o.log_level)

runner = Inspec::Runner.new(o)
runner.add_tests(tests)
Expand Down Expand Up @@ -173,6 +180,20 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength
@json ||= conffile ? read_config(conffile) : {}
end

# get the log level
# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
def get_log_level(level)
valid = %w{debug info warn error fatal}

if valid.include?(level)
l = level
else
l = 'info'
end

Logger.const_get(l.upcase)
end

def read_config(file)
if file == '-'
puts 'WARN: reading JSON config from standard input' if STDIN.tty?
Expand Down