class Csvlint::Cli

Public Instance Methods

help() click to toggle source
# File lib/csvlint/cli.rb, line 22
def help
  self.class.command_help(shell, :validate)
end
validate(source = nil) click to toggle source
# File lib/csvlint/cli.rb, line 13
def validate(source = nil)
  source = read_source(source)
  @schema = get_schema(options[:schema]) if options[:schema]
  fetch_schema_tables(@schema, options) if source.nil?

  valid = validate_csv(source, @schema, options[:dump])
  exit 1 unless valid
end

Private Instance Methods

fetch_schema_tables(schema, options) click to toggle source
# File lib/csvlint/cli.rb, line 67
def fetch_schema_tables(schema, options)
  valid = true

  unless schema.instance_of? Csvlint::Csvw::TableGroup
    return_error "No CSV data to validate."
  end
  schema.tables.keys.each do |source|
    begin
      source = source.sub("file:","")
      source = File.new( source )
    rescue Errno::ENOENT
      return_error "#{source} not found"
    end unless source =~ /^http(s)?/
    valid &= validate_csv(source, schema, options[:dump])
  end

  exit 1 unless valid
end
get_schema(schema) click to toggle source
# File lib/csvlint/cli.rb, line 51
def get_schema(schema)
  begin
    schema = Csvlint::Schema.load_from_json(schema, false)
  rescue Csvlint::Csvw::MetadataError => e
    return_error "invalid metadata: #{e.message}#{" at " + e.path if e.path}"
  rescue OpenURI::HTTPError, Errno::ENOENT
    return_error "#{options[:schema]} not found"
  end

  if schema.class == Csvlint::Schema && schema.description == "malformed"
    return_error "invalid metadata: malformed JSON"
  end

  schema
end
print_error(index, error, dump, color) click to toggle source
print_errors(errors, dump) click to toggle source
read_source(source) click to toggle source
# File lib/csvlint/cli.rb, line 30
def read_source(source)
  if source.nil?
    # If no source is present, try reading from stdin
    if !$stdin.tty?
      source = StringIO.new(STDIN.read) rescue nil
      return_error "No CSV data to validate" if !options[:schema] && source.nil?
    end
  else
    # If the source isn't a URL, it's a file
    unless source =~ /^http(s)?/
      begin
        source = File.new( source )
      rescue Errno::ENOENT
        return_error "#{source} not found"
      end
    end
  end

  source
end
report_lines() click to toggle source
# File lib/csvlint/cli.rb, line 152
def report_lines
  lambda do |row|
    new_errors = row.errors.count
    if new_errors > @error_count
      print "!".red
    else
      print ".".green
    end
    @error_count = new_errors
  end
end
return_error(message) click to toggle source
# File lib/csvlint/cli.rb, line 118
def return_error(message)
  if $stdout.tty?
    puts message.colorize(:red)
  else
    puts message
  end
  exit 1
end
validate_csv(source, schema, dump) click to toggle source
# File lib/csvlint/cli.rb, line 127
def validate_csv(source, schema, dump)
  @error_count = 0

  validator = Csvlint::Validator.new( source, {}, schema, { lambda: report_lines } )

  if source.class == String
    csv = source
  elsif source.class == File
    csv = source.path
  else
    csv = "CSV"
  end

  if $stdout.tty?
    puts "\r\n#{csv} is #{validator.valid? ? "VALID".green : "INVALID".red}"
  else
    puts "\r\n#{csv} is #{validator.valid? ? "VALID" : "INVALID"}"
  end

  print_errors(validator.errors, dump)
  print_errors(validator.warnings, dump)

  return validator.valid?
end