diff --git a/TODO.md b/TODO.md index 8af36a1..5327317 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,7 @@ - rm printYamlData() log.Fatal(), maybe return error on all printers? -- refactor parser, there's some duplicate code +- refactor parser, there's some duplicate code, remove pattern from parser args ## Features to be implemented @@ -14,6 +14,4 @@ - add --no-headers option -- add input parsing support for CSV including unquoting of stuff - like: `"xxx","1919 b"` etc, maybe an extra option for unquoting diff --git a/cfg/config.go b/cfg/config.go index d14808d..25ea36d 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -17,9 +17,11 @@ along with this program. If not, see . package cfg import ( + "errors" "fmt" "github.com/gookit/color" "os" + "regexp" ) const DefaultSeparator string = `(\s\s+|\t)` @@ -36,6 +38,7 @@ type Config struct { OutputMode int InvertMatch bool Pattern string + PatternR *regexp.Regexp SortMode string SortDescending bool @@ -163,3 +166,15 @@ func (c *Config) ApplyDefaults() { c.NoNumbering = true } } + +func (c *Config) PreparePattern() error { + PatternR, err := regexp.Compile(c.Pattern) + + if err != nil { + return errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", c.Pattern, err)) + } + + c.PatternR = PatternR + + return nil +} diff --git a/cmd/root.go b/cmd/root.go index bcf7c51..9600cf2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -73,6 +73,11 @@ func Execute() { conf.CheckEnv() conf.PrepareModeFlags(modeflag) conf.PrepareSortFlags(sortmode) + + if err := conf.PreparePattern(); err != nil { + return err + } + conf.ApplyDefaults() // actual execution starts here diff --git a/lib/parser.go b/lib/parser.go index ed81bb0..031ef32 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -21,6 +21,7 @@ import ( "bufio" "encoding/csv" "errors" + "fmt" "github.com/alecthomas/repr" "github.com/tlinden/tablizer/cfg" "io"