changed file handling, use -r <file> or nothing to use stdin

This commit is contained in:
2025-01-14 12:21:55 +01:00
committed by T.v.Dein
parent 306f583522
commit 6ca835add1
4 changed files with 86 additions and 35 deletions

View File

@@ -196,8 +196,14 @@ func Execute() {
"config file (default: ~/.config/tablizer/config)") "config file (default: ~/.config/tablizer/config)")
// filters // filters
rootCmd.PersistentFlags().StringArrayVarP(&conf.Rawfilters, "filter", "F", nil, "Filter by field (field=regexp)") rootCmd.PersistentFlags().StringArrayVarP(&conf.Rawfilters,
rootCmd.PersistentFlags().StringArrayVarP(&conf.Transposers, "regex-transposer", "R", nil, "apply /search/replace/ regexp to fields given in -T") "filter", "F", nil, "Filter by field (field=regexp)")
rootCmd.PersistentFlags().StringArrayVarP(&conf.Transposers,
"regex-transposer", "R", nil, "apply /search/replace/ regexp to fields given in -T")
// input
rootCmd.PersistentFlags().StringVarP(&conf.InputFile, "read-file", "r", "",
"Read input data from file")
rootCmd.SetUsageTemplate(strings.TrimSpace(usage) + "\n") rootCmd.SetUsageTemplate(strings.TrimSpace(usage) + "\n")

View File

@@ -121,9 +121,14 @@ func PrepareColumnVars(columns string, data *Tabdata) ([]int, error) {
// is not a regexp (contains no non-word chars) // is not a regexp (contains no non-word chars)
// lc() it so that word searches are case insensitive // lc() it so that word searches are case insensitive
columnpattern = strings.ToLower(columnpattern) columnpattern = strings.ToLower(columnpattern)
}
colPattern, err := regexp.Compile(columnpattern) for i, head := range data.headers {
if columnpattern == strings.ToLower(head) {
usecolumns = append(usecolumns, i+1)
}
}
} else {
colPattern, err := regexp.Compile("(?i)" + columnpattern)
if err != nil { if err != nil {
msg := fmt.Sprintf("Could not parse columns list %s: %v", columns, err) msg := fmt.Sprintf("Could not parse columns list %s: %v", columns, err)
@@ -136,6 +141,7 @@ func PrepareColumnVars(columns string, data *Tabdata) ([]int, error) {
usecolumns = append(usecolumns, i+1) usecolumns = append(usecolumns, i+1)
} }
} }
}
} else { } else {
// we digress from go best practises here, because if // we digress from go best practises here, because if
// a colum spec is not a number, we process them above // a colum spec is not a number, we process them above

View File

@@ -67,8 +67,8 @@ func TestPrepareColumns(t *testing.T) {
}{ }{
{"1,2,3", []int{1, 2, 3}, false}, {"1,2,3", []int{1, 2, 3}, false},
{"1,2,", []int{}, true}, {"1,2,", []int{}, true},
{"T", []int{2, 3}, false}, {"T.", []int{2, 3}, false},
{"T,2,3", []int{2, 3}, false}, {"T.,2,3", []int{2, 3}, false},
{"[a-z,4,5", []int{4, 5}, true}, // invalid regexp {"[a-z,4,5", []int{4, 5}, true}, // invalid regexp
} }
@@ -117,13 +117,13 @@ func TestPrepareTransposerColumns(t *testing.T) {
false, false,
}, },
{ {
"T", // will match [T]WO and [T]HREE "T.", // will match [T]WO and [T]HREE
[]string{`/\d/x/`, `/.//`}, []string{`/\d/x/`, `/.//`},
2, 2,
false, false,
}, },
{ {
"T,2", "TH.,2",
[]string{`/\d/x/`, `/.//`}, []string{`/\d/x/`, `/.//`},
2, 2,
false, false,

View File

@@ -29,7 +29,7 @@ import (
const RWRR = 0755 const RWRR = 0755
func ProcessFiles(conf *cfg.Config, args []string) error { func ProcessFiles(conf *cfg.Config, args []string) error {
fds, pattern, err := determineIO(conf, args) fd, pattern, err := determineIO(conf, args)
if err != nil { if err != nil {
return err return err
@@ -39,7 +39,6 @@ func ProcessFiles(conf *cfg.Config, args []string) error {
return err return err
} }
for _, fd := range fds {
data, err := Parse(*conf, fd) data, err := Parse(*conf, fd)
if err != nil { if err != nil {
return err return err
@@ -55,12 +54,52 @@ func ProcessFiles(conf *cfg.Config, args []string) error {
} }
printData(os.Stdout, *conf, &data) printData(os.Stdout, *conf, &data)
}
return nil return nil
} }
func determineIO(conf *cfg.Config, args []string) ([]io.Reader, string, error) { func determineIO(conf *cfg.Config, args []string) (io.Reader, string, error) {
var filehandle io.Reader
var pattern string
var haveio bool
switch {
case conf.InputFile == "-":
filehandle = os.Stdin
haveio = true
case conf.InputFile != "":
fd, err := os.OpenFile(conf.InputFile, os.O_RDONLY, RWRR)
if err != nil {
return nil, "", fmt.Errorf("failed to read input file %s: %w", conf.InputFile, err)
}
filehandle = fd
haveio = true
}
if !haveio {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// we're reading from STDIN, which takes precedence over file args
filehandle = os.Stdin
haveio = true
}
}
if len(args) > 0 {
pattern = args[0]
conf.Pattern = args[0]
}
if !haveio {
return nil, "", errors.New("no file specified and nothing to read on stdin")
}
return filehandle, pattern, nil
}
func _determineIO(conf *cfg.Config, args []string) ([]io.Reader, string, error) {
var filehandles []io.Reader var filehandles []io.Reader
var pattern string var pattern string
@@ -80,7 +119,7 @@ func determineIO(conf *cfg.Config, args []string) ([]io.Reader, string, error) {
haveio = true haveio = true
} else if len(args) > 0 { } else if len(args) > 0 {
// threre were args left, take a look // there were args left, take a look
if args[0] == "-" { if args[0] == "-" {
// in traditional unix programs a dash denotes STDIN (forced) // in traditional unix programs a dash denotes STDIN (forced)
filehandles = append(filehandles, os.Stdin) filehandles = append(filehandles, os.Stdin)