diff --git a/Makefile b/Makefile index 8add8d8..2cda54b 100644 --- a/Makefile +++ b/Makefile @@ -18,16 +18,16 @@ # # no need to modify anything below tool = tablizer -version = $(shell egrep "^var version = " cmd/root.go | cut -d'=' -f2 | cut -d'"' -f 2) +version = $(shell egrep "^var Version = " lib/common.go | cut -d'=' -f2 | cut -d'"' -f 2) archs = android darwin freebsd linux netbsd openbsd windows PREFIX = /usr/local UID = root GID = 0 -all: buildlocal man +all: buildlocal $(tool).1 -man: - pod2man -c "User Commands" -r 1 -s 1 $(tool).pod > $(tool).1 +%.1: %.pod + pod2man -c "User Commands" -r 1 -s 1 $*.pod > $*.1 buildlocal: go build @@ -43,4 +43,4 @@ install: buildlocal install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/ clean: - rm -f $(tool) $(tool).1 + rm -rf $(tool) $(tool).1 releases diff --git a/cmd/root.go b/cmd/root.go index 002441d..4ab2b73 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,35 +17,26 @@ along with this program. If not, see . package cmd import ( + "daemon.de/tablizer/lib" "fmt" "github.com/spf13/cobra" "os" ) -var version = "v1.0.1" - var rootCmd = &cobra.Command{ Use: "tablizer [regex] [file, ...]", Short: "[Re-]tabularize tabular data", Long: `Manipulate tabular output of other programs`, RunE: func(cmd *cobra.Command, args []string) error { - if Version { - fmt.Printf("This is tablizer version %s\n", version) + if lib.ShowVersion { + fmt.Printf("This is tablizer version %s\n", lib.Version) return nil } - return process(args) + return lib.ProcessFiles(args) }, } -var Debug bool -var XtendedOut bool -var NoNumbering bool -var Version bool -var Columns string -var UseColumns []int -var Separator string - func Execute() { err := rootCmd.Execute() if err != nil { @@ -54,10 +45,10 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "Enable debugging") - rootCmd.PersistentFlags().BoolVarP(&XtendedOut, "extended", "x", false, "Enable extended output") - rootCmd.PersistentFlags().BoolVarP(&NoNumbering, "no-numbering", "n", false, "Disable header numbering") - rootCmd.PersistentFlags().BoolVarP(&Version, "version", "v", false, "Print program version") - rootCmd.PersistentFlags().StringVarP(&Separator, "separator", "s", "", "Custom field separator") - rootCmd.PersistentFlags().StringVarP(&Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") + rootCmd.PersistentFlags().BoolVarP(&lib.Debug, "debug", "d", false, "Enable debugging") + rootCmd.PersistentFlags().BoolVarP(&lib.XtendedOut, "extended", "x", false, "Enable extended output") + rootCmd.PersistentFlags().BoolVarP(&lib.NoNumbering, "no-numbering", "n", false, "Disable header numbering") + rootCmd.PersistentFlags().BoolVarP(&lib.ShowVersion, "version", "v", false, "Print program version") + rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", "", "Custom field separator") + rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") } diff --git a/go.mod b/go.mod index 4016ac5..1e8b3fa 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,13 @@ module daemon.de/tablizer go 1.18 -require github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 +require ( + github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 + github.com/spf13/cobra v1.5.0 +) require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/spf13/cobra v1.5.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.8.0 // indirect ) diff --git a/lib/common.go b/lib/common.go new file mode 100644 index 0000000..980d54f --- /dev/null +++ b/lib/common.go @@ -0,0 +1,28 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package lib + +var Debug bool +var XtendedOut bool +var NoNumbering bool +var ShowVersion bool +var Columns string +var UseColumns []int +var Separator string + +var Version = "v1.0.2" diff --git a/lib/helpers.go b/lib/helpers.go new file mode 100644 index 0000000..08ab0de --- /dev/null +++ b/lib/helpers.go @@ -0,0 +1,51 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package lib + +import ( + "fmt" + "os" + "strconv" + "strings" +) + +func die(v ...interface{}) { + fmt.Fprintln(os.Stderr, v...) + os.Exit(1) +} + +func contains(s []int, e int) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + +func prepareColumns() { + if len(Columns) > 0 { + for _, use := range strings.Split(Columns, ",") { + usenum, err := strconv.Atoi(use) + if err != nil { + die(err) + } + UseColumns = append(UseColumns, usenum) + } + } +} diff --git a/cmd/processinput.go b/lib/io.go similarity index 81% rename from cmd/processinput.go rename to lib/io.go index 4800f45..1bb5caf 100644 --- a/cmd/processinput.go +++ b/lib/io.go @@ -15,29 +15,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package cmd +package lib import ( "errors" "github.com/alecthomas/repr" "os" - "strconv" - "strings" ) -func process(args []string) error { +func ProcessFiles(args []string) error { var pattern string havefiles := false - if len(Columns) > 0 { - for _, use := range strings.Split(Columns, ",") { - usenum, err := strconv.Atoi(use) - if err != nil { - die(err) - } - UseColumns = append(UseColumns, usenum) - } - } + prepareColumns() if len(args) > 0 { if _, err := os.Stat(args[0]); err != nil { @@ -56,7 +46,7 @@ func process(args []string) error { if Debug { repr.Print(data) } - printTable(data) + printData(data) } havefiles = true } @@ -69,7 +59,7 @@ func process(args []string) error { if Debug { repr.Print(data) } - printTable(data) + printData(data) } else { return errors.New("No file specified and nothing to read on stdin!") } diff --git a/cmd/parser.go b/lib/parser.go similarity index 97% rename from cmd/parser.go rename to lib/parser.go index c27566d..24fac74 100644 --- a/cmd/parser.go +++ b/lib/parser.go @@ -15,13 +15,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package cmd +package lib import ( "bufio" "fmt" "io" - "os" "regexp" "strings" ) @@ -36,11 +35,6 @@ type Tabdata struct { entries [][]string } -func die(v ...interface{}) { - fmt.Fprintln(os.Stderr, v...) - os.Exit(1) -} - /* Parse tabular input. We split the header (first line) by 2 or more spaces, remember the positions of the header fields. We then split diff --git a/cmd/printer.go b/lib/printer.go similarity index 91% rename from cmd/printer.go rename to lib/printer.go index c6e9c3e..2f1233b 100644 --- a/cmd/printer.go +++ b/lib/printer.go @@ -15,19 +15,22 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package cmd +package lib import ( "fmt" "strings" ) -func printTable(data Tabdata) { +func printData(data Tabdata) { if XtendedOut { - printExtended(data) - return + printExtendedData(data) + } else { + printTabularData(data) } +} +func printTabularData(data Tabdata) { // needed for data output var formats []string @@ -93,7 +96,7 @@ func printTable(data Tabdata) { /* We simulate the \x command of psql (the PostgreSQL client) */ -func printExtended(data Tabdata) { +func printExtendedData(data Tabdata) { // needed for data output format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set @@ -115,12 +118,3 @@ func printExtended(data Tabdata) { } } } - -func contains(s []int, e int) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -}