fix #27: check if parsed headers and columns match

This commit is contained in:
2024-11-04 11:13:53 +01:00
parent ef5211e45f
commit 4dc87ac22e
4 changed files with 38 additions and 17 deletions

View File

@@ -29,7 +29,7 @@ import (
) )
const DefaultSeparator string = `(\s\s+|\t)` const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.2.1" const Version string = "v1.2.2"
const MAXPARTS = 2 const MAXPARTS = 2
var DefaultLoadPath = os.Getenv("HOME") + "/.config/tablizer/lisp" var DefaultLoadPath = os.Getenv("HOME") + "/.config/tablizer/lisp"

View File

@@ -63,6 +63,14 @@ func completion(cmd *cobra.Command, mode string) error {
} }
} }
// we die with exit 1 if there's an error
func wrapE(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func Execute() { func Execute() {
var ( var (
conf cfg.Config conf cfg.Config
@@ -77,48 +85,43 @@ func Execute() {
Use: "tablizer [regex] [file, ...]", Use: "tablizer [regex] [file, ...]",
Short: "[Re-]tabularize tabular data", Short: "[Re-]tabularize tabular data",
Long: `Manipulate tabular output of other programs`, Long: `Manipulate tabular output of other programs`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
if ShowVersion { if ShowVersion {
fmt.Println(cfg.Getversion()) fmt.Println(cfg.Getversion())
return nil return
} }
if ShowManual { if ShowManual {
man() man()
return nil return
} }
if len(ShowCompletion) > 0 { if len(ShowCompletion) > 0 {
return completion(cmd, ShowCompletion) wrapE(completion(cmd, ShowCompletion))
return
} }
// Setup // Setup
err := conf.ParseConfigfile() wrapE(conf.ParseConfigfile())
if err != nil {
return err
}
conf.CheckEnv() conf.CheckEnv()
conf.PrepareModeFlags(modeflag) conf.PrepareModeFlags(modeflag)
conf.PrepareSortFlags(sortmode) conf.PrepareSortFlags(sortmode)
if err = conf.PrepareFilters(); err != nil { wrapE(conf.PrepareFilters())
return err
}
conf.DetermineColormode() conf.DetermineColormode()
conf.ApplyDefaults() conf.ApplyDefaults()
// setup lisp env, load plugins etc // setup lisp env, load plugins etc
err = lib.SetupLisp(&conf) wrapE(lib.SetupLisp(&conf))
if err != nil {
return err
}
// actual execution starts here // actual execution starts here
return lib.ProcessFiles(&conf, args) wrapE(lib.ProcessFiles(&conf, args))
}, },
} }

View File

@@ -40,6 +40,20 @@ func contains(s []int, e int) bool {
return false return false
} }
// validate the consitency of parsed data
func ValidateConsistency(data *Tabdata) error {
expectedfields := len(data.headers)
for idx, row := range data.entries {
if len(row) != expectedfields {
return fmt.Errorf("row %d does not contain expected %d elements, but %d",
idx, expectedfields, len(row))
}
}
return nil
}
// parse columns list given with -c, modifies config.UseColumns based // parse columns list given with -c, modifies config.UseColumns based
// on eventually given regex // on eventually given regex
func PrepareColumns(conf *cfg.Config, data *Tabdata) error { func PrepareColumns(conf *cfg.Config, data *Tabdata) error {

View File

@@ -45,6 +45,10 @@ func ProcessFiles(conf *cfg.Config, args []string) error {
return err return err
} }
if err = ValidateConsistency(&data); err != nil {
return err
}
err = PrepareColumns(conf, &data) err = PrepareColumns(conf, &data)
if err != nil { if err != nil {
return err return err