feat: read from stdin when supplying `-` as filepath

pull/4/head
Dominique Merle 3 years ago
parent aa2516a62c
commit aa30a38fb5

@ -5,7 +5,7 @@
> the features described in this README are not all implemented.
> We're merely doc-driving this tool, and our doc is ambitious.
- [ ] Read from stdin with `-`
- [x] Read from stdin with `-`
- [x] Read `CSV` file
- [x] `--sort`
- [x] `--format text`

@ -17,10 +17,12 @@ limitations under the License.
package cmd
import (
"bufio"
"encoding/csv"
"fmt"
"github.com/MieuxVoter/majority-judgment-cli/formatter"
"github.com/spf13/cobra"
"io"
"strings"
"os"
@ -89,13 +91,24 @@ multiply them beforehand by a big factor like 1 000 000 000.
proposalsTallies := make([]*judgment.ProposalTally, 0, 10)
csvFile, err := os.Open(args[0])
if err != nil {
fmt.Println(err)
fileParameter := strings.TrimSpace(args[0])
var csvReader io.Reader
if "-" == fileParameter {
csvReader = bufio.NewReader(os.Stdin)
} else {
csvFile, err := os.Open(fileParameter)
if err != nil {
fmt.Println(err)
}
defer func(csvFile *os.File) {
err := csvFile.Close()
if err != nil {
fmt.Println(err)
}
}(csvFile)
csvReader = csvFile
}
defer csvFile.Close()
csvRows, err := csv.NewReader(csvFile).ReadAll()
csvRows, err := csv.NewReader(csvReader).ReadAll()
if err != nil {
fmt.Println("Failed to read input CSV:", err)
os.Exit(2)

Loading…
Cancel
Save