Skip to content

Commit

Permalink
fix #27: check if parsed headers and columns match
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein committed Nov 4, 2024
1 parent ef5211e commit 4dc87ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.2.1"
const Version string = "v1.2.2"
const MAXPARTS = 2

var DefaultLoadPath = os.Getenv("HOME") + "/.config/tablizer/lisp"
Expand Down
35 changes: 19 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
var (
conf cfg.Config
Expand All @@ -77,48 +85,43 @@ func Execute() {
Use: "tablizer [regex] [file, ...]",
Short: "[Re-]tabularize tabular data",
Long: `Manipulate tabular output of other programs`,
RunE: func(cmd *cobra.Command, args []string) error {

Run: func(cmd *cobra.Command, args []string) {
if ShowVersion {
fmt.Println(cfg.Getversion())

return nil
return
}

if ShowManual {
man()

return nil
return
}

if len(ShowCompletion) > 0 {
return completion(cmd, ShowCompletion)
wrapE(completion(cmd, ShowCompletion))

return
}

// Setup
err := conf.ParseConfigfile()
if err != nil {
return err
}
wrapE(conf.ParseConfigfile())

conf.CheckEnv()
conf.PrepareModeFlags(modeflag)
conf.PrepareSortFlags(sortmode)

if err = conf.PrepareFilters(); err != nil {
return err
}
wrapE(conf.PrepareFilters())

conf.DetermineColormode()
conf.ApplyDefaults()

// setup lisp env, load plugins etc
err = lib.SetupLisp(&conf)
if err != nil {
return err
}
wrapE(lib.SetupLisp(&conf))

// actual execution starts here
return lib.ProcessFiles(&conf, args)
wrapE(lib.ProcessFiles(&conf, args))
},
}

Expand Down
14 changes: 14 additions & 0 deletions lib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ func contains(s []int, e int) bool {
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
// on eventually given regex
func PrepareColumns(conf *cfg.Config, data *Tabdata) error {
Expand Down
4 changes: 4 additions & 0 deletions lib/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func ProcessFiles(conf *cfg.Config, args []string) error {
return err
}

if err = ValidateConsistency(&data); err != nil {
return err
}

err = PrepareColumns(conf, &data)
if err != nil {
return err
Expand Down

0 comments on commit 4dc87ac

Please sign in to comment.