don't use die() anymore, butt dtd go errros

This commit is contained in:
2022-10-03 12:52:26 +02:00
parent 07b65bcff5
commit 65cbaddd5f
5 changed files with 22 additions and 16 deletions

View File

@@ -20,17 +20,11 @@ package lib
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
) )
func die(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}
func contains(s []int, e int) bool { func contains(s []int, e int) bool {
for _, a := range s { for _, a := range s {
if a == e { if a == e {

View File

@@ -31,7 +31,11 @@ func ProcessFiles(args []string) error {
} }
for _, fd := range fds { for _, fd := range fds {
printData(parseFile(fd, pattern)) data, err := parseFile(fd, pattern)
if err != nil {
return err
}
printData(data)
} }
return nil return nil

View File

@@ -19,6 +19,7 @@ package lib
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"github.com/alecthomas/repr" "github.com/alecthomas/repr"
"io" "io"
@@ -43,7 +44,7 @@ type Tabdata struct {
way we can turn "tabular data" (with fields containing whitespaces) way we can turn "tabular data" (with fields containing whitespaces)
into real tabular data. We re-tabulate our input if you will. into real tabular data. We re-tabulate our input if you will.
*/ */
func parseFile(input io.Reader, pattern string) Tabdata { func parseFile(input io.Reader, pattern string) (Tabdata, error) {
data := Tabdata{} data := Tabdata{}
var scanner *bufio.Scanner var scanner *bufio.Scanner
@@ -65,7 +66,7 @@ func parseFile(input io.Reader, pattern string) Tabdata {
patternR, err := regexp.Compile(pattern) patternR, err := regexp.Compile(pattern)
if err != nil { if err != nil {
die(err) return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, err))
} }
if !hadFirst { if !hadFirst {
@@ -145,20 +146,17 @@ func parseFile(input io.Reader, pattern string) Tabdata {
idx++ idx++
} }
if Debug {
fmt.Println()
}
data.entries = append(data.entries, values) data.entries = append(data.entries, values)
} }
} }
if scanner.Err() != nil { if scanner.Err() != nil {
die(scanner.Err()) return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, scanner.Err()))
} }
if Debug { if Debug {
repr.Print(data) repr.Print(data)
} }
return data return data, nil
} }

View File

@@ -70,7 +70,12 @@ asd igig cxxxncnc
19191 EDD 1 X` 19191 EDD 1 X`
readFd := strings.NewReader(table) readFd := strings.NewReader(table)
gotdata := parseFile(readFd, "") gotdata, err := parseFile(readFd, "")
if err != nil {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
}
if !reflect.DeepEqual(data, gotdata) { if !reflect.DeepEqual(data, gotdata) {
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata) t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
} }

View File

@@ -54,7 +54,12 @@ asd igig cxxxncnc
for mode, expect := range expects { for mode, expect := range expects {
OutputMode = mode OutputMode = mode
fd := strings.NewReader(table) fd := strings.NewReader(table)
data := parseFile(fd, "") data, err := parseFile(fd, "")
if err != nil {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, data)
}
printData(data) printData(data)
buf := make([]byte, 1024) buf := make([]byte, 1024)