class YamlLint::CLI

CLI execution

Attributes

opts[R]

Public Class Methods

new(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel) click to toggle source

setup CLI options

# File lib/yamllint/cli.rb, line 12
def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR,
               kernel = Kernel)
  @argv = argv
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @kernel = kernel
end

Public Instance Methods

execute!() click to toggle source

Run the CLI command

# File lib/yamllint/cli.rb, line 22
def execute!
  files_to_check = parse_options.leftovers

  YamlLint.logger.level = Logger::DEBUG if opts.debug

  no_yamls_to_check_msg = "Error: need at least one YAML file to check.\n"\
                          'Try --help for help.'
  abort(no_yamls_to_check_msg) if files_to_check.empty?
  lint(files_to_check)
end

Private Instance Methods

lint(files_to_check) click to toggle source
# File lib/yamllint/cli.rb, line 35
def lint(files_to_check)
  linter = if files_to_check == ['-']
             lint_stream
           else
             lint_files(files_to_check)
           end

  puts 'YamlLint found no errors' unless linter.errors?
  return unless linter.errors?
  linter.display_errors
  puts "YAML lint found #{linter.errors_count} errors"
  @kernel.exit(1)
end
lint_files(files_to_check) click to toggle source
# File lib/yamllint/cli.rb, line 49
def lint_files(files_to_check)
  ext = opts.extensions.split(',') unless opts.extensions.nil?
  linter = YamlLint::Linter.new(
    disable_ext_check: opts.disable_ext_check,
    extensions: ext
  )
  begin
    puts "Checking #{files_to_check.flatten.length} files"
    linter.check_all(files_to_check)
  rescue => e
    @stderr.puts e.message
    exit(1)
  end

  linter
end
lint_stream() click to toggle source
# File lib/yamllint/cli.rb, line 66
def lint_stream
  linter = YamlLint::Linter.new
  begin
    linter.check_stream(STDIN)
  rescue => e
    @stderr.puts e.message
    exit(1)
  end

  linter
end
parse_options() click to toggle source
# File lib/yamllint/cli.rb, line 92
def parse_options
  p = setup_options

  @opts = Trollop.with_standard_exception_handling p do
    p.parse(@argv)
  end

  p
end
setup_options() click to toggle source
# File lib/yamllint/cli.rb, line 78
def setup_options
  Trollop::Parser.new do
    banner 'Usage: yamllint [options] file1.yaml [file2.yaml ...]'
    version(YamlLint::VERSION)

    banner ''
    banner 'Options:'
    opt :debug, 'Debug logging', default: false, short: 'D'
    opt :disable_ext_check, 'Disable file extension check', default: false
    opt :extensions, 'Add more allowed extensions (comma delimited list)',
        type: :string
  end
end