diff --git a/cfg/config.go b/cfg/config.go index 9e89adb..e5f0ef4 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -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" diff --git a/cmd/root.go b/cmd/root.go index 38cf2ed..c493a4d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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)) }, } diff --git a/lib/helpers.go b/lib/helpers.go index 2f04ad2..9ce52e8 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -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 { diff --git a/lib/io.go b/lib/io.go index 90e98a5..3fe28c7 100644 --- a/lib/io.go +++ b/lib/io.go @@ -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