reorganized directory and package structure

This commit is contained in:
2022-09-30 19:14:58 +02:00
parent e617e52127
commit 19dabb7385
8 changed files with 112 additions and 62 deletions

View File

@@ -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

View File

@@ -17,35 +17,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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 ,)")
}

6
go.mod
View File

@@ -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
)

28
lib/common.go Normal file
View 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
View 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)
}
}
}

View File

@@ -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/>.
*/
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!")
}

View File

@@ -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/>.
*/
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

View File

@@ -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/>.
*/
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
}