From e617e521279754d2c096bdc263b1d975d9b65d9b Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 18:36:15 +0200 Subject: [PATCH 01/12] added checks --- mkrel.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mkrel.sh b/mkrel.sh index c93afa7..cb07229 100755 --- a/mkrel.sh +++ b/mkrel.sh @@ -27,6 +27,11 @@ windows/amd64" tool="$1" version="$2" +if test -z "$version"; then + echo "Usage: $0 " + exit 1 +fi + rm -rf releases mkdir -p releases From 19dabb7385857d8602116e1ebce8ff2e25da5e39 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:14:58 +0200 Subject: [PATCH 02/12] reorganized directory and package structure --- Makefile | 10 +++---- cmd/root.go | 29 +++++++----------- go.mod | 6 ++-- lib/common.go | 28 ++++++++++++++++++ lib/helpers.go | 51 ++++++++++++++++++++++++++++++++ cmd/processinput.go => lib/io.go | 20 ++++--------- {cmd => lib}/parser.go | 8 +---- {cmd => lib}/printer.go | 22 +++++--------- 8 files changed, 112 insertions(+), 62 deletions(-) create mode 100644 lib/common.go create mode 100644 lib/helpers.go rename cmd/processinput.go => lib/io.go (81%) rename {cmd => lib}/parser.go (97%) rename {cmd => lib}/printer.go (91%) 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 -} From eda702c914f32cdea1929272c383dbefe396ea53 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:24:00 +0200 Subject: [PATCH 03/12] added circleci --- .circleci/config.yml | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..7b0ac79 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,52 @@ +--- +version: 2.1 + +jobs: + compile: + docker: + - image: cimg/go:1.18 + steps: + - checkout + - run: make + + test: + parameters: + go_version: + type: string + os: + type: string + run_test: + type: boolean + default: true + docker: + - image: circleci/golang:<< parameters.go_version >> + environment: + GOOS: "<< parameters.os >>" + steps: + - checkout + - run: make test + +workflows: + version: 2 + procfs: + jobs: + - compile + - test: + name: test-linux + os: linux + matrix: + parameters: + go_version: + - "1.16" + - "1.17" + - "1.18" + - test: + name: test-windows + os: windows + run_test: false + matrix: + parameters: + go_version: + - "1.16" + - "1.17" + - "1.18" From c8ebf7fde24c978f40652f14e85e6b3aa4d5dda2 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:38:54 +0200 Subject: [PATCH 04/12] added tests --- Makefile | 3 +++ lib/helpers_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 lib/helpers_test.go diff --git a/Makefile b/Makefile index 2cda54b..eafedff 100644 --- a/Makefile +++ b/Makefile @@ -44,3 +44,6 @@ install: buildlocal clean: rm -rf $(tool) $(tool).1 releases + +test: + go test -v ./... diff --git a/lib/helpers_test.go b/lib/helpers_test.go new file mode 100644 index 0000000..68fa2c3 --- /dev/null +++ b/lib/helpers_test.go @@ -0,0 +1,43 @@ +/* +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" + "testing" +) + +func TestArrayContains(t *testing.T) { + var tests = []struct { + list []int + search int + want bool + }{ + {[]int{1, 2, 3}, 2, true}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("%d,%d,%t", tt.list, tt.search, tt.want) + t.Run(testname, func(t *testing.T) { + answer := contains(tt.list, tt.search) + if answer != tt.want { + t.Errorf("got %t, want %t", answer, tt.want) + } + }) + } +} From f4dc6c62e6bff6310f3652b0a6b17812f9e16b0f Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:42:29 +0200 Subject: [PATCH 05/12] ci fixes --- .circleci/config.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7b0ac79..de3fc49 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -39,14 +39,12 @@ workflows: go_version: - "1.16" - "1.17" - - "1.18" - test: - name: test-windows - os: windows + name: test-freebsd + os: freebsd run_test: false matrix: parameters: go_version: - "1.16" - "1.17" - - "1.18" From a9979714bac1d32e0bb4a0aaa8d75b7e7d555d90 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:44:47 +0200 Subject: [PATCH 06/12] ci fixes --- .circleci/config.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index de3fc49..ee4f512 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,7 +19,7 @@ jobs: type: boolean default: true docker: - - image: circleci/golang:<< parameters.go_version >> + - image: cimg/golang:<< parameters.go_version >> environment: GOOS: "<< parameters.os >>" steps: @@ -39,6 +39,7 @@ workflows: go_version: - "1.16" - "1.17" + - "1.18" - test: name: test-freebsd os: freebsd @@ -48,3 +49,4 @@ workflows: go_version: - "1.16" - "1.17" + - "1.18" From 282e87d8cc3fcdb08fda9cc48a803a4662db32de Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:45:39 +0200 Subject: [PATCH 07/12] ci fixes --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ee4f512..5f7f012 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,7 +19,7 @@ jobs: type: boolean default: true docker: - - image: cimg/golang:<< parameters.go_version >> + - image: cimg/go:<< parameters.go_version >> environment: GOOS: "<< parameters.os >>" steps: From d38bae0dd1b0eb83b642f5b5ec5ed3a5c61130ce Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:47:27 +0200 Subject: [PATCH 08/12] ci fixes --- .circleci/config.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5f7f012..b0c4c1c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -50,3 +50,13 @@ workflows: - "1.16" - "1.17" - "1.18" + - test: + name: test-windows + os: windows + run_test: false + matrix: + parameters: + go_version: + - "1.16" + - "1.17" + - "1.18" From 1b1b63caa3239f2bae5f81220e905058728d45ec Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 30 Sep 2022 19:52:16 +0200 Subject: [PATCH 09/12] +badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 079d333..eaf7715 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![](https://circleci.com/gh/TLINDEN/tablizer.svg?style=svg)](https://app.circleci.com/pipelines/github/TLINDEN/tablizer) + ## tablizer - Manipulate tabular output of other programs Tablizer can be used to re-format tabular output of other From 487470818c3239c180507fb261aacd2e45e4b42c Mon Sep 17 00:00:00 2001 From: "T.v.Dein" Date: Fri, 30 Sep 2022 22:28:18 +0200 Subject: [PATCH 10/12] No more x compile --- .circleci/config.yml | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b0c4c1c..055e24b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -13,50 +13,26 @@ jobs: parameters: go_version: type: string - os: - type: string run_test: type: boolean default: true docker: - image: cimg/go:<< parameters.go_version >> - environment: - GOOS: "<< parameters.os >>" steps: - checkout - run: make test workflows: version: 2 - procfs: + unit-test: jobs: - compile - test: - name: test-linux - os: linux - matrix: - parameters: - go_version: - - "1.16" - - "1.17" - - "1.18" - - test: - name: test-freebsd - os: freebsd - run_test: false - matrix: - parameters: - go_version: - - "1.16" - - "1.17" - - "1.18" - - test: - name: test-windows - os: windows - run_test: false + name: testing matrix: parameters: go_version: - "1.16" - "1.17" - "1.18" + - "1.19" From 4ca3a56280293f57dbcfd226a520e78ffde54885 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 1 Oct 2022 14:14:53 +0200 Subject: [PATCH 11/12] refactored output printer, now using tablewriter --- cmd/root.go | 17 ++++- go.mod | 2 + go.sum | 4 ++ lib/common.go | 6 ++ lib/helpers.go | 33 +++++++++- lib/io.go | 2 +- lib/parser.go | 2 +- lib/printer.go | 174 ++++++++++++++++++++++++++++++++++--------------- tablizer.pod | 19 ++++-- 9 files changed, 196 insertions(+), 63 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4ab2b73..fd0446d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,13 @@ var rootCmd = &cobra.Command{ return nil } + lib.PrepareColumns() + + err := lib.PrepareModeFlags() + if err != nil { + return err + } + return lib.ProcessFiles(args) }, } @@ -46,9 +53,17 @@ func Execute() { func init() { 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 ,)") + + // output flags, only 1 allowed + rootCmd.PersistentFlags().BoolVarP(&lib.OutflagExtended, "extended", "X", false, "Enable extended output") + rootCmd.PersistentFlags().BoolVarP(&lib.OutflagMarkdown, "markdown", "M", false, "Enable markdown table output") + rootCmd.PersistentFlags().BoolVarP(&lib.OutflagOrgtable, "orgtbl", "O", false, "Enable org-mode table output") + rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl") + + // same thing but more common, takes precedence over above group + rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, ascii(default)") } diff --git a/go.mod b/go.mod index 1e8b3fa..38b9a5b 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,13 @@ go 1.18 require ( github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 + github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.5.0 ) require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/mattn/go-runewidth v0.0.9 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.8.0 // indirect ) diff --git a/go.sum b/go.sum index 2e9690d..6ec3e3c 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/lib/common.go b/lib/common.go index 980d54f..104d452 100644 --- a/lib/common.go +++ b/lib/common.go @@ -17,6 +17,7 @@ along with this program. If not, see . package lib +// command line flags var Debug bool var XtendedOut bool var NoNumbering bool @@ -24,5 +25,10 @@ var ShowVersion bool var Columns string var UseColumns []int var Separator string +var OutflagExtended bool +var OutflagMarkdown bool +var OutflagOrgtable bool +var OutputMode string var Version = "v1.0.2" +var validOutputmodes = "(orgtbl|markdown|extended|ascii)" diff --git a/lib/helpers.go b/lib/helpers.go index 08ab0de..5dab33f 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -18,8 +18,10 @@ along with this program. If not, see . package lib import ( + "errors" "fmt" "os" + "regexp" "strconv" "strings" ) @@ -38,7 +40,7 @@ func contains(s []int, e int) bool { return false } -func prepareColumns() { +func PrepareColumns() { if len(Columns) > 0 { for _, use := range strings.Split(Columns, ",") { usenum, err := strconv.Atoi(use) @@ -49,3 +51,32 @@ func prepareColumns() { } } } + +func PrepareModeFlags() error { + if len(OutputMode) == 0 { + switch { + case OutflagExtended: + OutputMode = "extended" + case OutflagMarkdown: + OutputMode = "markdown" + case OutflagOrgtable: + OutputMode = "orgtbl" + default: + OutputMode = "ascii" + } + } else { + r, err := regexp.Compile(validOutputmodes) + + if err != nil { + return errors.New("Failed to validate output mode spec!") + } + + match := r.MatchString(OutputMode) + + if !match { + return errors.New("Invalid output mode!") + } + } + + return nil +} diff --git a/lib/io.go b/lib/io.go index 1bb5caf..3665dbf 100644 --- a/lib/io.go +++ b/lib/io.go @@ -27,7 +27,7 @@ func ProcessFiles(args []string) error { var pattern string havefiles := false - prepareColumns() + //prepareColumns() if len(args) > 0 { if _, err := os.Stat(args[0]); err != nil { diff --git a/lib/parser.go b/lib/parser.go index 24fac74..eadac31 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -144,7 +144,7 @@ func parseFile(input io.Reader, pattern string) Tabdata { // if Debug { // fmt.Printf("<%s> ", value) // } - values = append(values, value) + values = append(values, strings.TrimSpace(value)) idx++ } diff --git a/lib/printer.go b/lib/printer.go index 2f1233b..2f6a056 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -19,78 +19,146 @@ package lib import ( "fmt" + "github.com/olekukonko/tablewriter" + "os" + "regexp" "strings" ) func printData(data Tabdata) { - if XtendedOut { - printExtendedData(data) - } else { - printTabularData(data) - } -} - -func printTabularData(data Tabdata) { - // needed for data output - var formats []string - - if len(data.entries) > 0 { - // headers + // prepare headers + // FIXME: maybe do this already in parseFile()? + if !NoNumbering { + numberedHeaders := []string{} for i, head := range data.headers { if len(Columns) > 0 { if !contains(UseColumns, i+1) { continue } } + numberedHeaders = append(numberedHeaders, fmt.Sprintf("%s(%d)", head, i+1)) + } + data.headers = numberedHeaders + } - // calculate column width - var width int - var iwidth int - var format string + // prepare data + if len(Columns) > 0 { + reducedEntries := [][]string{} + reducedEntry := []string{} + for _, entry := range data.entries { + reducedEntry = nil + for i, value := range entry { + if !contains(UseColumns, i+1) { + continue + } - // generate format string - if len(head) > data.maxwidthPerCol[i] { - width = len(head) - } else { - width = data.maxwidthPerCol[i] + reducedEntry = append(reducedEntry, value) } + reducedEntries = append(reducedEntries, reducedEntry) + } + data.entries = reducedEntries + } - if NoNumbering { - iwidth = 0 - } else { - iwidth = len(fmt.Sprintf("%d", i)) // in case i > 9 - } + switch OutputMode { + case "extended": + printExtendedData(data) + case "ascii": + printAsciiData(data) + case "orgtbl": + printOrgmodeData(data) + case "markdown": + printMarkdownData(data) + default: + printAsciiData(data) + } +} - format = fmt.Sprintf("%%-%ds", 3+iwidth+width) +func trimRow(row []string) []string { + // FIXME: remove this when we only use Tablewriter and strip in ParseFile()! + var fixedrow []string + for _, cell := range row { + fixedrow = append(fixedrow, strings.TrimSpace(cell)) + } - if NoNumbering { - fmt.Printf(format, fmt.Sprintf("%s ", head)) - } else { - fmt.Printf(format, fmt.Sprintf("%s(%d) ", head, i+1)) - } + return fixedrow +} - // register - formats = append(formats, format) - } - fmt.Println() +/* + Emacs org-mode compatible table (also orgtbl-mode) +*/ +func printOrgmodeData(data Tabdata) { + tableString := &strings.Builder{} + table := tablewriter.NewWriter(tableString) - // entries - var idx int - for _, entry := range data.entries { - idx = 0 - //fmt.Println(entry) - for i, value := range entry { - if len(Columns) > 0 { - if !contains(UseColumns, i+1) { - continue - } - } - fmt.Printf(formats[idx], strings.TrimSpace(value)) - idx++ - } - fmt.Println() - } + table.SetHeader(data.headers) + + for _, row := range data.entries { + table.Append(trimRow(row)) + } + + table.Render() + + /* fix output for org-mode (orgtbl) + tableWriter output: + +------+------+ + | cell | cell | + +------+------+ + + Needed for org-mode compatibility: + |------+------| + | cell | cell | + |------+------| + */ + leftR := regexp.MustCompile("(?m)^\\+") + rightR := regexp.MustCompile("\\+(?m)$") + + fmt.Print(rightR.ReplaceAllString(leftR.ReplaceAllString(tableString.String(), "|"), "|")) +} + +/* + Markdown table +*/ +func printMarkdownData(data Tabdata) { + table := tablewriter.NewWriter(os.Stdout) + + table.SetHeader(data.headers) + + for _, row := range data.entries { + table.Append(trimRow(row)) } + + table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) + table.SetCenterSeparator("|") + + table.Render() +} + +/* + Simple ASCII table without any borders etc, just like the input we expect +*/ +func printAsciiData(data Tabdata) { + table := tablewriter.NewWriter(os.Stdout) + + table.SetHeader(data.headers) + table.AppendBulk(data.entries) + + // for _, row := range data.entries { + // table.Append(trimRow(row)) + // } + + table.SetAutoWrapText(false) + table.SetAutoFormatHeaders(true) + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetCenterSeparator("") + table.SetColumnSeparator("") + table.SetRowSeparator("") + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetTablePadding("\t") // pad with tabs + table.SetNoWhiteSpace(true) + + table.Render() } /* diff --git a/tablizer.pod b/tablizer.pod index b837422..bb11ee0 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -10,9 +10,12 @@ tablizer - Manipulate tabular output of other programs Flags: -c, --columns string Only show the speficied columns (separated by ,) -d, --debug Enable debugging - -x, --extended Enable extended output + -X, --extended Enable extended output -h, --help help for tablizer + -M, --markdown Enable markdown table output -n, --no-numbering Disable header numbering + -O, --orgtbl Enable org-mode table output + -o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default) -s, --separator string Custom field separator -v, --version Print program version @@ -70,12 +73,12 @@ The numbering can be suppressed by using the B<-n> option. There might be cases when the tabular output of a program is way too large for your current terminal but you still need to see every -column. In such cases the B<-x> option can be usefull which enables -I. In this mode, each row will be printed vertically, -header left, value right, aligned by the field widths. Here's an -example: +column. In such cases the B<-X> option (or B<-o extended> can be +usefull which enables I. In this mode, each row will be +printed vertically, header left, value right, aligned by the field +widths. Here's an example: - kubectl get pods | ./tablizer -x + kubectl get pods | ./tablizer -X NAME: repldepl-7bcd8d5b64-7zq4l READY: 1/1 STATUS: Running @@ -85,6 +88,10 @@ example: You can of course still use a regex to reduce the number of rows displayed. +Beside normal ascii mode (the default) and extended mode there more +output modes available: B which prints an Emacs org-mode table +and B which prints a Markdown table. + Finally the B<-d> option enables debugging output which is mostly usefull for the developer. From e6723a6951561eaf262d862ee4b39e603983a858 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sun, 2 Oct 2022 14:22:31 +0200 Subject: [PATCH 12/12] continued refactoring, added more tests, better error handling --- TODO | 1 - cmd/root.go | 12 +++++-- lib/helpers.go | 6 ++-- lib/helpers_test.go | 34 ++++++++++++++++++-- lib/io.go | 44 ++++++++++++++++---------- lib/parser.go | 13 ++++---- lib/parser_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ lib/printer_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++ tablizer.pod | 22 +++++++------ 9 files changed, 244 insertions(+), 41 deletions(-) create mode 100644 lib/parser_test.go create mode 100644 lib/printer_test.go diff --git a/TODO b/TODO index 5b120d5..73b8d16 100644 --- a/TODO +++ b/TODO @@ -3,4 +3,3 @@ Add a mode like FreeBSD stat(1): stat -s dead.letter st_dev=170671546954750497 st_ino=159667 st_mode=0100644 st_nlink=1 st_uid=1001 st_gid=1001 st_rdev=18446744073709551615 st_size=573 st_atime=1661994007 st_mtime=1661961878 st_ctime=1661961878 st_birthtime=1658394900 st_blksize=4096 st_blocks=3 st_flags=2048 -mv UseColumns processing out of process() diff --git a/cmd/root.go b/cmd/root.go index fd0446d..6d047eb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,9 +33,12 @@ var rootCmd = &cobra.Command{ return nil } - lib.PrepareColumns() + err := lib.PrepareColumns() + if err != nil { + return err + } - err := lib.PrepareModeFlags() + err = lib.PrepareModeFlags() if err != nil { return err } @@ -58,11 +61,14 @@ func init() { rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", "", "Custom field separator") rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") - // output flags, only 1 allowed + // output flags, only 1 allowed, hidden, since just short cuts rootCmd.PersistentFlags().BoolVarP(&lib.OutflagExtended, "extended", "X", false, "Enable extended output") rootCmd.PersistentFlags().BoolVarP(&lib.OutflagMarkdown, "markdown", "M", false, "Enable markdown table output") rootCmd.PersistentFlags().BoolVarP(&lib.OutflagOrgtable, "orgtbl", "O", false, "Enable org-mode table output") rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl") + rootCmd.Flags().MarkHidden("extended") + rootCmd.Flags().MarkHidden("orgtbl") + rootCmd.Flags().MarkHidden("markdown") // same thing but more common, takes precedence over above group rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, ascii(default)") diff --git a/lib/helpers.go b/lib/helpers.go index 5dab33f..ddda4ee 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -40,16 +40,18 @@ func contains(s []int, e int) bool { return false } -func PrepareColumns() { +func PrepareColumns() error { if len(Columns) > 0 { for _, use := range strings.Split(Columns, ",") { usenum, err := strconv.Atoi(use) if err != nil { - die(err) + msg := fmt.Sprintf("Could not parse columns list %s: %v", Columns, err) + return errors.New(msg) } UseColumns = append(UseColumns, usenum) } } + return nil } func PrepareModeFlags() error { diff --git a/lib/helpers_test.go b/lib/helpers_test.go index 68fa2c3..b02bb9b 100644 --- a/lib/helpers_test.go +++ b/lib/helpers_test.go @@ -19,20 +19,22 @@ package lib import ( "fmt" + "reflect" "testing" ) -func TestArrayContains(t *testing.T) { +func Testcontains(t *testing.T) { var tests = []struct { list []int search int want bool }{ {[]int{1, 2, 3}, 2, true}, + {[]int{2, 3, 4}, 5, false}, } for _, tt := range tests { - testname := fmt.Sprintf("%d,%d,%t", tt.list, tt.search, tt.want) + testname := fmt.Sprintf("contains-%d,%d,%t", tt.list, tt.search, tt.want) t.Run(testname, func(t *testing.T) { answer := contains(tt.list, tt.search) if answer != tt.want { @@ -41,3 +43,31 @@ func TestArrayContains(t *testing.T) { }) } } + +func TestPrepareColumns(t *testing.T) { + var tests = []struct { + input string + exp []int + wanterror bool // expect error + }{ + {"1,2,3", []int{1, 2, 3}, false}, + {"1,2,", []int{}, true}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("PrepareColumns-%s-%t", tt.input, tt.wanterror) + t.Run(testname, func(t *testing.T) { + Columns = tt.input + err := PrepareColumns() + if err != nil { + if !tt.wanterror { + t.Errorf("got error: %v", err) + } + } else { + if !reflect.DeepEqual(UseColumns, tt.exp) { + t.Errorf("got: %v, expected: %v", UseColumns, tt.exp) + } + } + }) + } +} diff --git a/lib/io.go b/lib/io.go index 3665dbf..4c6a726 100644 --- a/lib/io.go +++ b/lib/io.go @@ -19,34 +19,48 @@ package lib import ( "errors" - "github.com/alecthomas/repr" + "io" "os" ) func ProcessFiles(args []string) error { - var pattern string - havefiles := false + fds, pattern, err := determineIO(args) + + if err != nil { + return err + } - //prepareColumns() + for _, fd := range fds { + printData(parseFile(fd, pattern)) + } + + return nil +} + +func determineIO(args []string) ([]io.Reader, string, error) { + var pattern string + var fds []io.Reader + var havefiles bool if len(args) > 0 { + // threre were args left, take a look if _, err := os.Stat(args[0]); err != nil { + // first one is not a file, consider it as regexp and + // shift arg list pattern = args[0] args = args[1:] } if len(args) > 0 { + // only files for _, file := range args { fd, err := os.OpenFile(file, os.O_RDONLY, 0755) + if err != nil { - die(err) + return nil, "", err } - data := parseFile(fd, pattern) - if Debug { - repr.Print(data) - } - printData(data) + fds = append(fds, fd) } havefiles = true } @@ -55,15 +69,11 @@ func ProcessFiles(args []string) error { if !havefiles { stat, _ := os.Stdin.Stat() if (stat.Mode() & os.ModeCharDevice) == 0 { - data := parseFile(os.Stdin, pattern) - if Debug { - repr.Print(data) - } - printData(data) + fds = append(fds, os.Stdin) } else { - return errors.New("No file specified and nothing to read on stdin!") + return nil, "", errors.New("No file specified and nothing to read on stdin!") } } - return nil + return fds, pattern, nil } diff --git a/lib/parser.go b/lib/parser.go index eadac31..0b19e14 100644 --- a/lib/parser.go +++ b/lib/parser.go @@ -20,6 +20,7 @@ package lib import ( "bufio" "fmt" + "github.com/alecthomas/repr" "io" "regexp" "strings" @@ -59,7 +60,7 @@ func parseFile(input io.Reader, pattern string) Tabdata { scanner = bufio.NewScanner(input) for scanner.Scan() { - line := scanner.Text() + line := strings.TrimSpace(scanner.Text()) values := []string{} patternR, err := regexp.Compile(pattern) @@ -109,22 +110,18 @@ func parseFile(input io.Reader, pattern string) Tabdata { // done hadFirst = true } - // if Debug { - // fmt.Println(data.headerIndices) - // } } else { // data processing if len(pattern) > 0 { - //fmt.Println(patternR.MatchString(line)) if !patternR.MatchString(line) { continue } } idx := 0 // we cannot use the header index, because we could exclude columns - for _, index := range data.headerIndices { value := "" + if index["end"] == 0 { value = string(line[index["beg"]:]) } else { @@ -159,5 +156,9 @@ func parseFile(input io.Reader, pattern string) Tabdata { die(scanner.Err()) } + if Debug { + repr.Print(data) + } + return data } diff --git a/lib/parser_test.go b/lib/parser_test.go new file mode 100644 index 0000000..19218b3 --- /dev/null +++ b/lib/parser_test.go @@ -0,0 +1,77 @@ +/* +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 ( + "reflect" + "strings" + "testing" +) + +func TestParser(t *testing.T) { + data := Tabdata{ + maxwidthHeader: 5, + maxwidthPerCol: []int{ + 5, + 5, + 8, + }, + columns: 3, + headerIndices: []map[string]int{ + map[string]int{ + "beg": 0, + "end": 6, + }, + map[string]int{ + "end": 13, + "beg": 7, + }, + map[string]int{ + "beg": 14, + "end": 0, + }, + }, + headers: []string{ + "ONE", + "TWO", + "THREE", + }, + entries: [][]string{ + []string{ + "asd", + "igig", + "cxxxncnc", + }, + []string{ + "19191", + "EDD 1", + "X", + }, + }, + } + + table := `ONE TWO THREE +asd igig cxxxncnc +19191 EDD 1 X` + + readFd := strings.NewReader(table) + gotdata := parseFile(readFd, "") + if !reflect.DeepEqual(data, gotdata) { + t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata) + } +} diff --git a/lib/printer_test.go b/lib/printer_test.go new file mode 100644 index 0000000..5abb75a --- /dev/null +++ b/lib/printer_test.go @@ -0,0 +1,76 @@ +/* +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 ( + "os" + "strings" + "testing" +) + +func TestPrinter(t *testing.T) { + table := `ONE TWO THREE +asd igig cxxxncnc +19191 EDD 1 X` + + expects := map[string]string{ + "ascii": `ONE(1) TWO(2) THREE(3) +asd igig cxxxncnc +19191 EDD 1 X`, + "orgtbl": `|--------+--------+----------| +| ONE(1) | TWO(2) | THREE(3) | +|--------+--------+----------| +| asd | igig | cxxxncnc | +| 19191 | EDD 1 | X | +|--------+--------+----------|`, + "markdown": `| ONE(1) | TWO(2) | THREE(3) | +|--------|--------|----------| +| asd | igig | cxxxncnc | +| 19191 | EDD 1 | X |`, + } + + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + origStdout := os.Stdout + os.Stdout = w + + for mode, expect := range expects { + OutputMode = mode + fd := strings.NewReader(table) + data := parseFile(fd, "") + printData(data) + + buf := make([]byte, 1024) + n, err := r.Read(buf) + if err != nil { + t.Fatal(err) + } + buf = buf[:n] + output := strings.TrimSpace(string(buf)) + + if output != expect { + t.Errorf("output mode: %s, got:\n%s\nwant:\n%s\n (%d <=> %d)", mode, output, expect, len(output), len(expect)) + } + } + + // Restore + os.Stdout = origStdout + +} diff --git a/tablizer.pod b/tablizer.pod index bb11ee0..4808812 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -10,12 +10,12 @@ tablizer - Manipulate tabular output of other programs Flags: -c, --columns string Only show the speficied columns (separated by ,) -d, --debug Enable debugging - -X, --extended Enable extended output -h, --help help for tablizer - -M, --markdown Enable markdown table output -n, --no-numbering Disable header numbering - -O, --orgtbl Enable org-mode table output -o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default) + -X, --extended Enable extended output + -M, --markdown Enable markdown table output + -O, --orgtbl Enable org-mode table output -s, --separator string Custom field separator -v, --version Print program version @@ -71,9 +71,14 @@ the original order. The numbering can be suppressed by using the B<-n> option. +Finally the B<-d> option enables debugging output which is mostly +usefull for the developer. + +?head2 OUTPUT MODES + There might be cases when the tabular output of a program is way too large for your current terminal but you still need to see every -column. In such cases the B<-X> option (or B<-o extended> can be +column. In such cases the B<-o extended> or B<-X> option can be usefull which enables I. In this mode, each row will be printed vertically, header left, value right, aligned by the field widths. Here's an example: @@ -88,12 +93,9 @@ widths. Here's an example: You can of course still use a regex to reduce the number of rows displayed. -Beside normal ascii mode (the default) and extended mode there more -output modes available: B which prints an Emacs org-mode table -and B which prints a Markdown table. - -Finally the B<-d> option enables debugging output which is mostly -usefull for the developer. +Beside normal ascii mode (the default) and extended mode there are +more output modes available: B which prints an Emacs org-mode +table and B which prints a Markdown table. =head1 BUGS