Skip to content

Commit

Permalink
Validate args and allow multiple files as input
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Larsson committed Nov 21, 2020
1 parent 613b41f commit 6cb75a9
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -56,17 +57,28 @@ reads serialized .tfrecord files and outputs results as JSON on standard output.
}
}
}`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 || isInputFromPipe() {
return nil
}
return errors.New("requires argument or stdin")
},
RunE: func(cmd *cobra.Command, args []string) error {
var buffers []io.Reader
if isInputFromPipe() {
return parseRecords(os.Stdin)
} else {
file, e := os.Open(args[0])
buffers = append(buffers, os.Stdin)
}

for _, path := range args {
file, e := os.Open(path)
if e != nil {
return e
}
defer file.Close()
return parseRecords(file)
buffers = append(buffers, file)
}
multi := io.MultiReader(buffers...)
return parseRecords(multi)
},
}

Expand Down

0 comments on commit 6cb75a9

Please sign in to comment.