mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
reorganized directory and package structure
This commit is contained in:
10
Makefile
10
Makefile
@@ -18,16 +18,16 @@
|
|||||||
#
|
#
|
||||||
# no need to modify anything below
|
# no need to modify anything below
|
||||||
tool = tablizer
|
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
|
archs = android darwin freebsd linux netbsd openbsd windows
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
UID = root
|
UID = root
|
||||||
GID = 0
|
GID = 0
|
||||||
|
|
||||||
all: buildlocal man
|
all: buildlocal $(tool).1
|
||||||
|
|
||||||
man:
|
%.1: %.pod
|
||||||
pod2man -c "User Commands" -r 1 -s 1 $(tool).pod > $(tool).1
|
pod2man -c "User Commands" -r 1 -s 1 $*.pod > $*.1
|
||||||
|
|
||||||
buildlocal:
|
buildlocal:
|
||||||
go build
|
go build
|
||||||
@@ -43,4 +43,4 @@ install: buildlocal
|
|||||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(tool) $(tool).1
|
rm -rf $(tool) $(tool).1 releases
|
||||||
|
|||||||
29
cmd/root.go
29
cmd/root.go
@@ -17,35 +17,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"daemon.de/tablizer/lib"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "v1.0.1"
|
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "tablizer [regex] [file, ...]",
|
Use: "tablizer [regex] [file, ...]",
|
||||||
Short: "[Re-]tabularize tabular data",
|
Short: "[Re-]tabularize tabular data",
|
||||||
Long: `Manipulate tabular output of other programs`,
|
Long: `Manipulate tabular output of other programs`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if Version {
|
if lib.ShowVersion {
|
||||||
fmt.Printf("This is tablizer version %s\n", version)
|
fmt.Printf("This is tablizer version %s\n", lib.Version)
|
||||||
return nil
|
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() {
|
func Execute() {
|
||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -54,10 +45,10 @@ func Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "Enable debugging")
|
rootCmd.PersistentFlags().BoolVarP(&lib.Debug, "debug", "d", false, "Enable debugging")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&XtendedOut, "extended", "x", false, "Enable extended output")
|
rootCmd.PersistentFlags().BoolVarP(&lib.XtendedOut, "extended", "x", false, "Enable extended output")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
rootCmd.PersistentFlags().BoolVarP(&lib.NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&Version, "version", "v", false, "Print program version")
|
rootCmd.PersistentFlags().BoolVarP(&lib.ShowVersion, "version", "v", false, "Print program version")
|
||||||
rootCmd.PersistentFlags().StringVarP(&Separator, "separator", "s", "", "Custom field separator")
|
rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", "", "Custom field separator")
|
||||||
rootCmd.PersistentFlags().StringVarP(&Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
||||||
}
|
}
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -2,11 +2,13 @@ module daemon.de/tablizer
|
|||||||
|
|
||||||
go 1.18
|
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 (
|
require (
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
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/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/stretchr/testify v1.8.0 // indirect
|
github.com/stretchr/testify v1.8.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
28
lib/common.go
Normal file
28
lib/common.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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"
|
||||||
51
lib/helpers.go
Normal file
51
lib/helpers.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,29 +15,19 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cmd
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/alecthomas/repr"
|
"github.com/alecthomas/repr"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func process(args []string) error {
|
func ProcessFiles(args []string) error {
|
||||||
var pattern string
|
var pattern string
|
||||||
havefiles := false
|
havefiles := false
|
||||||
|
|
||||||
if len(Columns) > 0 {
|
prepareColumns()
|
||||||
for _, use := range strings.Split(Columns, ",") {
|
|
||||||
usenum, err := strconv.Atoi(use)
|
|
||||||
if err != nil {
|
|
||||||
die(err)
|
|
||||||
}
|
|
||||||
UseColumns = append(UseColumns, usenum)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
if _, err := os.Stat(args[0]); err != nil {
|
if _, err := os.Stat(args[0]); err != nil {
|
||||||
@@ -56,7 +46,7 @@ func process(args []string) error {
|
|||||||
if Debug {
|
if Debug {
|
||||||
repr.Print(data)
|
repr.Print(data)
|
||||||
}
|
}
|
||||||
printTable(data)
|
printData(data)
|
||||||
}
|
}
|
||||||
havefiles = true
|
havefiles = true
|
||||||
}
|
}
|
||||||
@@ -69,7 +59,7 @@ func process(args []string) error {
|
|||||||
if Debug {
|
if Debug {
|
||||||
repr.Print(data)
|
repr.Print(data)
|
||||||
}
|
}
|
||||||
printTable(data)
|
printData(data)
|
||||||
} else {
|
} else {
|
||||||
return errors.New("No file specified and nothing to read on stdin!")
|
return errors.New("No file specified and nothing to read on stdin!")
|
||||||
}
|
}
|
||||||
@@ -15,13 +15,12 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cmd
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -36,11 +35,6 @@ type Tabdata struct {
|
|||||||
entries [][]string
|
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
|
Parse tabular input. We split the header (first line) by 2 or more
|
||||||
spaces, remember the positions of the header fields. We then split
|
spaces, remember the positions of the header fields. We then split
|
||||||
@@ -15,19 +15,22 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cmd
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printTable(data Tabdata) {
|
func printData(data Tabdata) {
|
||||||
if XtendedOut {
|
if XtendedOut {
|
||||||
printExtended(data)
|
printExtendedData(data)
|
||||||
return
|
} else {
|
||||||
|
printTabularData(data)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printTabularData(data Tabdata) {
|
||||||
// needed for data output
|
// needed for data output
|
||||||
var formats []string
|
var formats []string
|
||||||
|
|
||||||
@@ -93,7 +96,7 @@ func printTable(data Tabdata) {
|
|||||||
/*
|
/*
|
||||||
We simulate the \x command of psql (the PostgreSQL client)
|
We simulate the \x command of psql (the PostgreSQL client)
|
||||||
*/
|
*/
|
||||||
func printExtended(data Tabdata) {
|
func printExtendedData(data Tabdata) {
|
||||||
// needed for data output
|
// needed for data output
|
||||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set
|
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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user