Sha256: 87f68e4f0c7bb6c8044074f5b5c7f26cc515a8b8a7937793138b36ae90eafae4

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

#!/usr/bin/env ruby
$:.unshift File.join( File.dirname(__FILE__), "..", "lib")

require 'csvlint'
require 'colorize'

def print_error(index, error, color=:red)
  location = ""
  location += error.row.to_s if error.row
  location += "#{error.row ? "," : ""}#{error.column.to_s}" if error.column
  if error.row || error.column
    location = "#{error.row ? "Row" : "Column"}: #{location}"
  end
  puts "#{index+1}. #{error.type}. #{location}".colorize(color)
end

if ARGV.length == 0 && !$stdin.tty?
  source = StringIO.new(ARGF.read)
else
  if ARGV[0]
    source = ARGV[0]
    unless source =~ /^http(s)?/
      begin
        source = File.new( source ) unless source =~ /^http(s)?/ 
      rescue Errno::ENOENT
        puts "File not found"
        exit 1
      end
    end
  else
    puts "Usage: csvlint {file or URL} or {input} | csvlint"
    exit 1
  end
end

validator = Csvlint::Validator.new( source )

puts "#{ARGV[0] || "CSV"} is #{validator.valid? ? "VALID".green : "INVALID".red}"

if validator.errors.size > 0
  validator.errors.each_with_index do |error, i|
    print_error(i, error)
  end
end

if validator.warnings.size > 0
  validator.warnings.each_with_index do |error, i|
    print_error(i, error, :yellow)
  end
end

exit 1 unless validator.valid?

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csvlint-0.1.0 bin/csvlint
csvlint-0.0.1 bin/csvlint