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

Output format is better to be grep like #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions cmd/csvlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func printHelpAndExit(code int) {
func main() {
delimiter := flag.String("delimiter", ",", "field delimiter in the file, for instance '\\t' or '|'")
lazyquotes := flag.Bool("lazyquotes", false, "try to parse improperly escaped quotes")
verbose := flag.Bool("verbose", false, "verbose output")
help := flag.Bool("help", false, "print help and exit")
flag.Parse()

Expand All @@ -31,38 +32,49 @@ func main() {

convertedDelimiter, err := strconv.Unquote(`'` + *delimiter + `'`)
if err != nil {
fmt.Printf("error unquoting delimiter '%s', note that only one-character delimiters are supported\n\n", *delimiter)
fmt.Fprintf(os.Stderr, "error unquoting delimiter '%s', note that only one-character delimiters are supported\n", *delimiter)
printHelpAndExit(1)
}
// don't need to check size since Unquote returns one-character string
comma, _ := utf8.DecodeRuneInString(convertedDelimiter)

if len(flag.Args()) != 1 {
fmt.Print("csvlint accepts a single filepath as an argument\n\n")
if flag.NArg() > 1 {
fmt.Fprint(os.Stderr, "csvlint accepts stdin or a single filepath as an argument\n")
printHelpAndExit(1)
}

f, err := os.Open(flag.Args()[0])
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("file '%s' does not exist\n", flag.Args()[0])
os.Exit(1)
} else {
panic(err)
var f *os.File
var fname string

if flag.NArg() == 0 {
fname = "stdin"
f = os.Stdin
} else {
fname = flag.Arg(0)
f, err = os.Open(fname)
if err != nil {
if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "file '%s' does not exist\n", fname)
os.Exit(1)
} else {
panic(err)
}
}
defer f.Close()
}
defer f.Close()

invalids, halted, err := csvlint.Validate(f, comma, *lazyquotes)
if err != nil {
panic(err)
}
if len(invalids) == 0 {
fmt.Println("file is valid")
if *verbose {
fmt.Println("file is valid")
}
os.Exit(0)
}
for _, invalid := range invalids {
fmt.Println(invalid.Error())
fmt.Fprintf(os.Stderr, "%s:%d:%s\n", fname, invalid.Num, invalid.Error())
}
if halted {
fmt.Println("\nunable to parse any further")
Expand Down