mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-18 13:01:11 +01:00
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2acd2c1b1 | ||
| 76f49a532f | |||
| 3fd2e6ac2f | |||
| 65cbaddd5f | |||
| 9f5fc6924e | |||
| 07b65bcff5 | |||
|
|
2f46716a7a | ||
| e6723a6951 | |||
| 66c4b68036 | |||
| 4ca3a56280 | |||
|
|
487470818c | ||
| 1b1b63caa3 | |||
| d38bae0dd1 | |||
| 282e87d8cc | |||
| a9979714ba | |||
| f4dc6c62e6 | |||
| c8ebf7fde2 | |||
| eda702c914 | |||
| 19dabb7385 | |||
| e617e52127 | |||
| a09f7b59c2 | |||
| 8fc831537e | |||
| 61f6e05515 | |||
| f4e8e92a6e | |||
| 54babec276 | |||
| f32ac18cdd | |||
| 4fc3beec31 | |||
| 2d8127dd67 | |||
| b8059eb676 | |||
|
|
febb0b13d7 | ||
|
|
cfc02f01a8 |
38
.circleci/config.yml
Normal file
38
.circleci/config.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
version: 2.1
|
||||
|
||||
jobs:
|
||||
compile:
|
||||
docker:
|
||||
- image: cimg/go:1.18
|
||||
steps:
|
||||
- checkout
|
||||
- run: make
|
||||
|
||||
test:
|
||||
parameters:
|
||||
go_version:
|
||||
type: string
|
||||
run_test:
|
||||
type: boolean
|
||||
default: true
|
||||
docker:
|
||||
- image: cimg/go:<< parameters.go_version >>
|
||||
steps:
|
||||
- checkout
|
||||
- run: make test
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
unit-test:
|
||||
jobs:
|
||||
- compile
|
||||
- test:
|
||||
name: testing
|
||||
matrix:
|
||||
parameters:
|
||||
go_version:
|
||||
- "1.16"
|
||||
- "1.17"
|
||||
- "1.18"
|
||||
- "1.19"
|
||||
50
Makefile
50
Makefile
@@ -1,18 +1,48 @@
|
||||
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
#
|
||||
# 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:
|
||||
@echo "Type 'make install' to install $(tool)"
|
||||
all: buildlocal $(tool).1
|
||||
|
||||
install:
|
||||
install -m 755 -d $(bindir)
|
||||
install -m 755 -d $(linkdir)
|
||||
install -m 755 $(tool) $(bindir)/$(tool)-$(version)
|
||||
ln -sf $(bindir)/$(tool)-$(version) $(linkdir)/$(tool)
|
||||
%.1: %.pod
|
||||
pod2man -c "User Commands" -r 1 -s 1 $*.pod > $*.1
|
||||
|
||||
buildlocal:
|
||||
go build
|
||||
|
||||
release:
|
||||
mkdir -p releases
|
||||
$(foreach arch,$(archs), GOOS=$(arch) GOARCH=amd64 go build -x -o releases/$(tool)-$(arch)-amd64-$(version); sha256sum releases/$(tool)-$(arch)-amd64-$(version) | cut -d' ' -f1 > releases/$(tool)-$(arch)-amd64-$(version).sha256sum;)
|
||||
./mkrel.sh $(tool) $(version)
|
||||
|
||||
install: buildlocal
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/man/man1
|
||||
install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/
|
||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||
|
||||
clean:
|
||||
rm -rf $(tool) $(tool).1 releases
|
||||
|
||||
test:
|
||||
go test -v ./...
|
||||
|
||||
41
README.md
41
README.md
@@ -1,3 +1,5 @@
|
||||
[](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
|
||||
@@ -68,12 +70,45 @@ repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m
|
||||
|
||||
## Installation
|
||||
|
||||
Download the latest release file for your architecture and put it into
|
||||
a directory within your `$PATH`.
|
||||
There are multiple ways to install **tablizer**:
|
||||
|
||||
- Go to the [latest release page](https://github.com/muesli/mango/releases/latest),
|
||||
locate the binary for your operating system and platform.
|
||||
|
||||
Download it and put it into some directory within your `$PATH` variable.
|
||||
|
||||
- The release page also contains a tarball for every supported platform. Unpack it
|
||||
to some temporary directory, extract it and execute the following command inside:
|
||||
```
|
||||
sudo make install
|
||||
```
|
||||
|
||||
- You can also install from source. Issue the following commands in your shell:
|
||||
```
|
||||
git clone https://github.com/TLINDEN/tablizer.git
|
||||
cd tablizer
|
||||
make
|
||||
sudo make install
|
||||
```
|
||||
|
||||
If you do not find a binary release for your platform, please don't
|
||||
hesitate to ask me about it, I'll add it.
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation is provided as a unix man-page. It will be
|
||||
automatically installed if you install from source. However, you can
|
||||
read the man-page online:
|
||||
|
||||
https://github.com/TLINDEN/tablizer/blob/main/tablizer.pod
|
||||
|
||||
Or if you cloned the repository you can read it this way (perl needs
|
||||
to be installed though): `perldoc tablizer.pod`.
|
||||
|
||||
|
||||
## Getting help
|
||||
|
||||
Although I'm happy to hear from udpxd users in private email,
|
||||
Although I'm happy to hear from tablizer users in private email,
|
||||
that's the best way for me to forget to do something.
|
||||
|
||||
In order to report a bug, unexpected behavior, feature requests
|
||||
|
||||
3
TODO
3
TODO
@@ -1,4 +1 @@
|
||||
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
|
||||
|
||||
109
cmd/printer.go
109
cmd/printer.go
@@ -1,109 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func printTable(data Tabdata) {
|
||||
if XtendedOut {
|
||||
printExtended(data)
|
||||
return
|
||||
}
|
||||
|
||||
// needed for data output
|
||||
var formats []string
|
||||
|
||||
if len(data.entries) > 0 {
|
||||
// headers
|
||||
for i, head := range data.headers {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// calculate column width
|
||||
var width int
|
||||
var iwidth int
|
||||
var format string
|
||||
|
||||
// generate format string
|
||||
if len(head) > data.maxwidthPerCol[i] {
|
||||
width = len(head)
|
||||
} else {
|
||||
width = data.maxwidthPerCol[i]
|
||||
}
|
||||
|
||||
if NoNumbering {
|
||||
iwidth = 0
|
||||
} else {
|
||||
iwidth = len(fmt.Sprintf("%d", i)) // in case i > 9
|
||||
}
|
||||
|
||||
format = fmt.Sprintf("%%-%ds", 3+iwidth+width)
|
||||
|
||||
if NoNumbering {
|
||||
fmt.Printf(format, fmt.Sprintf("%s ", head))
|
||||
} else {
|
||||
fmt.Printf(format, fmt.Sprintf("%s(%d) ", head, i+1))
|
||||
}
|
||||
|
||||
// register
|
||||
formats = append(formats, format)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
We simulate the \x command of psql (the PostgreSQL client)
|
||||
*/
|
||||
func printExtended(data Tabdata) {
|
||||
// needed for data output
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set
|
||||
|
||||
if len(data.entries) > 0 {
|
||||
var idx int
|
||||
for _, entry := range data.entries {
|
||||
idx = 0
|
||||
for i, value := range entry {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf(format, data.headers[idx], value)
|
||||
idx++
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
93
cmd/root.go
93
cmd/root.go
@@ -17,80 +17,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"daemon.de/tablizer/lib"
|
||||
"fmt"
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var version = "v1.0.0"
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "tablizer [regex] [file, ...]",
|
||||
Short: "[Re-]tabularize tabular data",
|
||||
Long: `Manipulate tabular output of other programs`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if Version {
|
||||
fmt.Printf("This is tablizer version %s\n", version)
|
||||
return
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if lib.ShowVersion {
|
||||
fmt.Printf("This is tablizer version %s\n", lib.Version)
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
err := lib.PrepareColumns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
if _, err := os.Stat(args[0]); err != nil {
|
||||
pattern = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
for _, file := range args {
|
||||
fd, err := os.OpenFile(file, os.O_RDONLY, 0755)
|
||||
if err != nil {
|
||||
die(err)
|
||||
}
|
||||
|
||||
data := parseFile(fd, pattern)
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
printTable(data)
|
||||
}
|
||||
havefiles = true
|
||||
}
|
||||
err = lib.PrepareModeFlags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !havefiles {
|
||||
data := parseFile(os.Stdin, pattern)
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
printTable(data)
|
||||
}
|
||||
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 {
|
||||
@@ -99,10 +55,23 @@ 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.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, 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.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output")
|
||||
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell")
|
||||
rootCmd.Flags().MarkHidden("extended")
|
||||
rootCmd.Flags().MarkHidden("orgtbl")
|
||||
rootCmd.Flags().MarkHidden("markdown")
|
||||
rootCmd.Flags().MarkHidden("shell")
|
||||
|
||||
// same thing but more common, takes precedence over above group
|
||||
rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)")
|
||||
}
|
||||
|
||||
8
go.mod
8
go.mod
@@ -2,11 +2,15 @@ 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/olekukonko/tablewriter v0.0.5
|
||||
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/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/testify v1.8.0 // indirect
|
||||
)
|
||||
|
||||
4
go.sum
4
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=
|
||||
|
||||
35
lib/common.go
Normal file
35
lib/common.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
// command line flags
|
||||
var Debug bool
|
||||
var XtendedOut bool
|
||||
var NoNumbering bool
|
||||
var ShowVersion bool
|
||||
var Columns string
|
||||
var UseColumns []int
|
||||
var Separator string
|
||||
var OutflagExtended bool
|
||||
var OutflagMarkdown bool
|
||||
var OutflagOrgtable bool
|
||||
var OutflagShell bool
|
||||
var OutputMode string
|
||||
|
||||
var Version = "v1.0.3"
|
||||
var validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
||||
91
lib/helpers.go
Normal file
91
lib/helpers.go
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func contains(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func PrepareColumns() error {
|
||||
if len(Columns) > 0 {
|
||||
for _, use := range strings.Split(Columns, ",") {
|
||||
usenum, err := strconv.Atoi(use)
|
||||
if err != nil {
|
||||
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 {
|
||||
if len(OutputMode) == 0 {
|
||||
switch {
|
||||
case OutflagExtended:
|
||||
OutputMode = "extended"
|
||||
case OutflagMarkdown:
|
||||
OutputMode = "markdown"
|
||||
case OutflagOrgtable:
|
||||
OutputMode = "orgtbl"
|
||||
case OutflagShell:
|
||||
OutputMode = "shell"
|
||||
NoNumbering = true
|
||||
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
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
return fixedrow
|
||||
}
|
||||
73
lib/helpers_test.go
Normal file
73
lib/helpers_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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("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 {
|
||||
t.Errorf("got %t, want %t", answer, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
83
lib/io.go
Normal file
83
lib/io.go
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ProcessFiles(args []string) error {
|
||||
fds, pattern, err := determineIO(args)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, fd := range fds {
|
||||
data, err := parseFile(fd, pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printData(&data)
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
fds = append(fds, fd)
|
||||
}
|
||||
havefiles = true
|
||||
}
|
||||
}
|
||||
|
||||
if !havefiles {
|
||||
stat, _ := os.Stdin.Stat()
|
||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||
fds = append(fds, os.Stdin)
|
||||
} else {
|
||||
return nil, "", errors.New("No file specified and nothing to read on stdin!")
|
||||
}
|
||||
}
|
||||
|
||||
return fds, pattern, nil
|
||||
}
|
||||
@@ -1,10 +1,28 @@
|
||||
package cmd
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/alecthomas/repr"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -19,11 +37,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
|
||||
@@ -31,7 +44,7 @@ func die(v ...interface{}) {
|
||||
way we can turn "tabular data" (with fields containing whitespaces)
|
||||
into real tabular data. We re-tabulate our input if you will.
|
||||
*/
|
||||
func parseFile(input io.Reader, pattern string) Tabdata {
|
||||
func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
||||
data := Tabdata{}
|
||||
|
||||
var scanner *bufio.Scanner
|
||||
@@ -48,12 +61,12 @@ 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)
|
||||
if err != nil {
|
||||
die(err)
|
||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, err))
|
||||
}
|
||||
|
||||
if !hadFirst {
|
||||
@@ -98,22 +111,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 {
|
||||
@@ -133,20 +142,21 @@ 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++
|
||||
}
|
||||
if Debug {
|
||||
fmt.Println()
|
||||
}
|
||||
data.entries = append(data.entries, values)
|
||||
}
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
die(scanner.Err())
|
||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, scanner.Err()))
|
||||
}
|
||||
|
||||
return data
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
82
lib/parser_test.go
Normal file
82
lib/parser_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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, err := parseFile(readFd, "")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, gotdata) {
|
||||
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
|
||||
}
|
||||
}
|
||||
206
lib/printer.go
Normal file
206
lib/printer.go
Normal file
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func printData(data *Tabdata) {
|
||||
// prepare headers: add numbers to headers
|
||||
numberedHeaders := []string{}
|
||||
for i, head := range data.headers {
|
||||
if len(Columns) > 0 {
|
||||
// -c specified
|
||||
if !contains(UseColumns, i+1) {
|
||||
// ignore this one
|
||||
continue
|
||||
}
|
||||
}
|
||||
if NoNumbering {
|
||||
numberedHeaders = append(numberedHeaders, head)
|
||||
} else {
|
||||
numberedHeaders = append(numberedHeaders, fmt.Sprintf("%s(%d)", head, i+1))
|
||||
}
|
||||
}
|
||||
data.headers = numberedHeaders
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
reducedEntry = append(reducedEntry, value)
|
||||
}
|
||||
reducedEntries = append(reducedEntries, reducedEntry)
|
||||
}
|
||||
data.entries = reducedEntries
|
||||
}
|
||||
|
||||
switch OutputMode {
|
||||
case "extended":
|
||||
printExtendedData(data)
|
||||
case "ascii":
|
||||
printAsciiData(data)
|
||||
case "orgtbl":
|
||||
printOrgmodeData(data)
|
||||
case "markdown":
|
||||
printMarkdownData(data)
|
||||
case "shell":
|
||||
printShellData(data)
|
||||
default:
|
||||
printAsciiData(data)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Emacs org-mode compatible table (also orgtbl-mode)
|
||||
*/
|
||||
func printOrgmodeData(data *Tabdata) {
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
/*
|
||||
We simulate the \x command of psql (the PostgreSQL client)
|
||||
*/
|
||||
func printExtendedData(data *Tabdata) {
|
||||
// needed for data output
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set
|
||||
|
||||
if len(data.entries) > 0 {
|
||||
var idx int
|
||||
for _, entry := range data.entries {
|
||||
idx = 0
|
||||
for i, value := range entry {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf(format, data.headers[idx], value)
|
||||
idx++
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
||||
*/
|
||||
func printShellData(data *Tabdata) {
|
||||
if len(data.entries) > 0 {
|
||||
var idx int
|
||||
for _, entry := range data.entries {
|
||||
idx = 0
|
||||
for i, value := range entry {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("%s=\"%s\" ", data.headers[idx], value)
|
||||
idx++
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
81
lib/printer_test.go
Normal file
81
lib/printer_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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, err := parseFile(fd, "")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, data)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
17
main.go
17
main.go
@@ -1,3 +1,20 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
65
mkrel.sh
Executable file
65
mkrel.sh
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
# get list with: go tool dist list
|
||||
DIST="darwin/amd64
|
||||
freebsd/amd64
|
||||
linux/amd64
|
||||
netbsd/amd64
|
||||
openbsd/amd64
|
||||
windows/amd64"
|
||||
|
||||
tool="$1"
|
||||
version="$2"
|
||||
|
||||
if test -z "$version"; then
|
||||
echo "Usage: $0 <tool name> <release version>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm -rf releases
|
||||
mkdir -p releases
|
||||
|
||||
|
||||
for D in $DIST; do
|
||||
os=${D/\/*/}
|
||||
arch=${D/*\//}
|
||||
binfile="releases/${tool}-${os}-${arch}-${version}"
|
||||
tardir="${tool}-${os}-${arch}-${version}"
|
||||
tarfile="releases/${tool}-${os}-${arch}-${version}.tar.gz"
|
||||
set -x
|
||||
GOOS=${os} GOARCH=${arch} go build -o ${binfile}
|
||||
mkdir -p ${tardir}
|
||||
cp ${binfile} README.md LICENSE ${tardir}/
|
||||
echo 'tool = tablizer
|
||||
PREFIX = /usr/local
|
||||
UID = root
|
||||
GID = 0
|
||||
|
||||
install:
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/man/man1
|
||||
install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/
|
||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/' > ${tardir}/Makefile
|
||||
tar cpzf ${tarfile} ${tardir}
|
||||
sha256sum ${binfile} | cut -d' ' -f1 > ${binfile}.sha256
|
||||
sha256sum ${tarfile} | cut -d' ' -f1 > ${tarfile}.sha256
|
||||
rm -rf ${tardir}
|
||||
set +x
|
||||
done
|
||||
|
||||
142
tablizer.pod
Normal file
142
tablizer.pod
Normal file
@@ -0,0 +1,142 @@
|
||||
=head1 NAME
|
||||
|
||||
tablizer - Manipulate tabular output of other programs
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
Usage:
|
||||
tablizer [regex] [file, ...] [flags]
|
||||
|
||||
Flags:
|
||||
-c, --columns string Only show the speficied columns (separated by ,)
|
||||
-d, --debug Enable debugging
|
||||
-h, --help help for tablizer
|
||||
-n, --no-numbering Disable header numbering
|
||||
-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
|
||||
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Many programs generate tabular output. But sometimes you need to
|
||||
post-process these tables, you may need to remove one or more columns
|
||||
or you may want to filter for some pattern or you may need the output
|
||||
in another program and need to parse it somehow. Standard unix tools
|
||||
such as awk(1), grep(1) or column(1) may help, but sometimes it's a
|
||||
tedious business.
|
||||
|
||||
Let's take the output of the tool kubectl. It contains cells with
|
||||
withespace and they do not separate columns by TAB characters. This is
|
||||
not easy to process.
|
||||
|
||||
You can use B<tablizer> to do these and more things.
|
||||
|
||||
B<tablizer> analyses the header fiels of a table, registers the column
|
||||
positions of each header field and separates columns by those
|
||||
positions.
|
||||
|
||||
Without any options it reads its input from C<STDIN>, but you can also
|
||||
specify a file as a parameter. If you want to reduce the output by
|
||||
some regular expression, just specify it as its first
|
||||
parameters. Hence:
|
||||
|
||||
# read from STDIN
|
||||
kubectl get pods | tablizer
|
||||
|
||||
# read a file
|
||||
tablizer filename
|
||||
|
||||
# search for pattern in a file (works like grep)
|
||||
tablizer regex filename
|
||||
|
||||
# search for pattern in STDIN
|
||||
kubectl get pods | tablizer regex
|
||||
|
||||
The output looks like the original one but every header field will
|
||||
have a numer associated with it, e.g.:
|
||||
|
||||
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
|
||||
|
||||
These numbers denote the column and you can use them to specify which
|
||||
columns you want to have in your output:
|
||||
|
||||
kubectl get pods | tablizer -c1,3
|
||||
|
||||
You can specify the numbers in any order but output will always follow
|
||||
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<-o extended> or B<-X> option can be
|
||||
usefull which enables I<extended mode>. 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 -o extended
|
||||
NAME: repldepl-7bcd8d5b64-7zq4l
|
||||
READY: 1/1
|
||||
STATUS: Running
|
||||
RESTARTS: 1 (71m ago)
|
||||
AGE: 5h28m
|
||||
|
||||
You can of course still use a regex to reduce the number of rows
|
||||
displayed.
|
||||
|
||||
The option B<-o shell> can be used if the output has to be processed
|
||||
by the shell, it prints variable assignments for each cell, one line
|
||||
per row:
|
||||
|
||||
kubectl get pods | ./tablizer -o extended ./tablizer -o shell
|
||||
NAME="repldepl-7bcd8d5b64-7zq4l" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-m48n8" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-q2bf4" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
|
||||
You can use this in an eval loop.
|
||||
|
||||
Beside normal ascii mode (the default) and extended mode there are
|
||||
more output modes available: B<orgtbl> which prints an Emacs org-mode
|
||||
table and B<markdown> which prints a Markdown table.
|
||||
|
||||
=head1 BUGS
|
||||
|
||||
In order to report a bug, unexpected behavior, feature requests
|
||||
or to submit a patch, please open an issue on github:
|
||||
L<https://github.com/TLINDEN/tablizer/issues>.
|
||||
|
||||
=head1 LICENSE
|
||||
|
||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
||||
|
||||
Copyright (c) 2022 by Thomas von Dein
|
||||
|
||||
This software uses the following GO libraries:
|
||||
|
||||
=over 4
|
||||
|
||||
=item repr (https://github.com/alecthomas/repr)
|
||||
|
||||
Released under the MIT License, Copyright (c) 2016 Alec Thomas
|
||||
|
||||
=item cobra (https://github.com/spf13/cobra)
|
||||
|
||||
Released under the Apache 2.0 license, Copyright 2013-2022 The Cobra Authors
|
||||
|
||||
=back
|
||||
|
||||
=head1 AUTHORS
|
||||
|
||||
Thomas von Dein B<tom AT vondein DOT org>
|
||||
|
||||
=cut
|
||||
|
||||
Reference in New Issue
Block a user