mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
added -v flag, replace 'help' subcommand wich -m, more tests
This commit is contained in:
26
cmd/root.go
26
cmd/root.go
@@ -18,18 +18,17 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/tlinden/tablizer/lib"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/tlinden/tablizer/lib"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var helpCmd = &cobra.Command{
|
var ShowManual = false
|
||||||
Use: "help",
|
|
||||||
Short: "Show documentation",
|
func man() {
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
man := exec.Command("less", "-")
|
man := exec.Command("less", "-")
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
@@ -44,7 +43,6 @@ var helpCmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
@@ -57,6 +55,11 @@ var rootCmd = &cobra.Command{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ShowManual {
|
||||||
|
man()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
err := lib.PrepareColumns()
|
err := lib.PrepareColumns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -81,7 +84,9 @@ func Execute() {
|
|||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.Debug, "debug", "d", false, "Enable debugging")
|
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.NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.ShowVersion, "version", "v", false, "Print program version")
|
rootCmd.PersistentFlags().BoolVarP(&lib.ShowVersion, "version", "V", false, "Print program version")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&lib.InvertMatch, "invert-match", "v", false, "select non-matching rows")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&ShowManual, "man", "m", false, "Display manual page")
|
||||||
rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", "", "Custom field separator")
|
rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", "", "Custom field separator")
|
||||||
rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
||||||
|
|
||||||
@@ -98,11 +103,4 @@ func init() {
|
|||||||
|
|
||||||
// same thing but more common, takes precedence over above group
|
// 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)")
|
rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)")
|
||||||
|
|
||||||
rootCmd.AddCommand(helpCmd)
|
|
||||||
|
|
||||||
rootCmd.SetHelpCommand(&cobra.Command{
|
|
||||||
Use: "no-help",
|
|
||||||
Hidden: true,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ SYNOPSIS
|
|||||||
-c, --columns string Only show the speficied columns (separated by ,)
|
-c, --columns string Only show the speficied columns (separated by ,)
|
||||||
-d, --debug Enable debugging
|
-d, --debug Enable debugging
|
||||||
-h, --help help for tablizer
|
-h, --help help for tablizer
|
||||||
|
-v, --invert-match select non-matching rows
|
||||||
|
-m, --man Display manual page
|
||||||
-n, --no-numbering Disable header numbering
|
-n, --no-numbering Disable header numbering
|
||||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||||
-X, --extended Enable extended output
|
-X, --extended Enable extended output
|
||||||
@@ -38,7 +40,8 @@ DESCRIPTION
|
|||||||
|
|
||||||
Without any options it reads its input from "STDIN", but you can also
|
Without any options it reads its input from "STDIN", but you can also
|
||||||
specify a file as a parameter. If you want to reduce the output by some
|
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:
|
regular expression, just specify it as its first parameter. You may also
|
||||||
|
use the -v option to exclude all rows which match the pattern. Hence:
|
||||||
|
|
||||||
# read from STDIN
|
# read from STDIN
|
||||||
kubectl get pods | tablizer
|
kubectl get pods | tablizer
|
||||||
|
|||||||
@@ -31,12 +31,13 @@ var (
|
|||||||
OutflagOrgtable bool
|
OutflagOrgtable bool
|
||||||
OutflagShell bool
|
OutflagShell bool
|
||||||
OutputMode string
|
OutputMode string
|
||||||
|
InvertMatch bool
|
||||||
|
|
||||||
// used for validation
|
// used for validation
|
||||||
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
||||||
|
|
||||||
// main program version
|
// main program version
|
||||||
Version = "v1.0.4"
|
Version = "v1.0.5"
|
||||||
|
|
||||||
// generated version string, used by -v contains lib.Version on
|
// generated version string, used by -v contains lib.Version on
|
||||||
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
||||||
|
|||||||
@@ -114,7 +114,11 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
|||||||
} else {
|
} else {
|
||||||
// data processing
|
// data processing
|
||||||
if len(pattern) > 0 {
|
if len(pattern) > 0 {
|
||||||
if !patternR.MatchString(line) {
|
if patternR.MatchString(line) == InvertMatch {
|
||||||
|
// by default -v is false, so if a line does NOT
|
||||||
|
// match the pattern, we will ignore it. However,
|
||||||
|
// if the user specified -v, the matching is inverted,
|
||||||
|
// so we ignore all lines, which DO match.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,7 +155,7 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if scanner.Err() != nil {
|
if scanner.Err() != nil {
|
||||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, scanner.Err()))
|
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -80,3 +81,57 @@ asd igig cxxxncnc
|
|||||||
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
|
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n", data, gotdata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParserPatternmatching(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
entries [][]string
|
||||||
|
pattern string
|
||||||
|
invert bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
entries: [][]string{
|
||||||
|
[]string{
|
||||||
|
"asd",
|
||||||
|
"igig",
|
||||||
|
"cxxxncnc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pattern: "ig",
|
||||||
|
invert: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
entries: [][]string{
|
||||||
|
[]string{
|
||||||
|
"19191",
|
||||||
|
"EDD 1",
|
||||||
|
"X",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pattern: "ig",
|
||||||
|
invert: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
table := `ONE TWO THREE
|
||||||
|
asd igig cxxxncnc
|
||||||
|
19191 EDD 1 X`
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("parse-with-inverted-pattern-%t", tt.invert)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
InvertMatch = tt.invert
|
||||||
|
|
||||||
|
readFd := strings.NewReader(table)
|
||||||
|
gotdata, err := parseFile(readFd, tt.pattern)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(tt.entries, gotdata.entries) {
|
||||||
|
t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n",
|
||||||
|
tt.pattern, tt.invert, tt.entries, gotdata.entries)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -133,7 +133,7 @@
|
|||||||
.\" ========================================================================
|
.\" ========================================================================
|
||||||
.\"
|
.\"
|
||||||
.IX Title "TABLIZER 1"
|
.IX Title "TABLIZER 1"
|
||||||
.TH TABLIZER 1 "2022-10-04" "1" "User Commands"
|
.TH TABLIZER 1 "2022-10-05" "1" "User Commands"
|
||||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||||
.\" way too many mistakes in technical documents.
|
.\" way too many mistakes in technical documents.
|
||||||
.if n .ad l
|
.if n .ad l
|
||||||
@@ -150,6 +150,8 @@ tablizer \- Manipulate tabular output of other programs
|
|||||||
\& \-c, \-\-columns string Only show the speficied columns (separated by ,)
|
\& \-c, \-\-columns string Only show the speficied columns (separated by ,)
|
||||||
\& \-d, \-\-debug Enable debugging
|
\& \-d, \-\-debug Enable debugging
|
||||||
\& \-h, \-\-help help for tablizer
|
\& \-h, \-\-help help for tablizer
|
||||||
|
\& \-v, \-\-invert\-match select non\-matching rows
|
||||||
|
\& \-m, \-\-man Display manual page
|
||||||
\& \-n, \-\-no\-numbering Disable header numbering
|
\& \-n, \-\-no\-numbering Disable header numbering
|
||||||
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default)
|
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default)
|
||||||
\& \-X, \-\-extended Enable extended output
|
\& \-X, \-\-extended Enable extended output
|
||||||
@@ -179,8 +181,9 @@ positions.
|
|||||||
.PP
|
.PP
|
||||||
Without any options it reads its input from \f(CW\*(C`STDIN\*(C'\fR, but you can also
|
Without any options it reads its input from \f(CW\*(C`STDIN\*(C'\fR, but you can also
|
||||||
specify a file as a parameter. If you want to reduce the output by
|
specify a file as a parameter. If you want to reduce the output by
|
||||||
some regular expression, just specify it as its first
|
some regular expression, just specify it as its first parameter. You
|
||||||
parameters. Hence:
|
may also use the \fB\-v\fR option to exclude all rows which match the
|
||||||
|
pattern. Hence:
|
||||||
.PP
|
.PP
|
||||||
.Vb 2
|
.Vb 2
|
||||||
\& # read from STDIN
|
\& # read from STDIN
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ tablizer - Manipulate tabular output of other programs
|
|||||||
-c, --columns string Only show the speficied columns (separated by ,)
|
-c, --columns string Only show the speficied columns (separated by ,)
|
||||||
-d, --debug Enable debugging
|
-d, --debug Enable debugging
|
||||||
-h, --help help for tablizer
|
-h, --help help for tablizer
|
||||||
|
-v, --invert-match select non-matching rows
|
||||||
|
-m, --man Display manual page
|
||||||
-n, --no-numbering Disable header numbering
|
-n, --no-numbering Disable header numbering
|
||||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||||
-X, --extended Enable extended output
|
-X, --extended Enable extended output
|
||||||
@@ -41,8 +43,9 @@ positions.
|
|||||||
|
|
||||||
Without any options it reads its input from C<STDIN>, but you can also
|
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
|
specify a file as a parameter. If you want to reduce the output by
|
||||||
some regular expression, just specify it as its first
|
some regular expression, just specify it as its first parameter. You
|
||||||
parameters. Hence:
|
may also use the B<-v> option to exclude all rows which match the
|
||||||
|
pattern. Hence:
|
||||||
|
|
||||||
# read from STDIN
|
# read from STDIN
|
||||||
kubectl get pods | tablizer
|
kubectl get pods | tablizer
|
||||||
|
|||||||
Reference in New Issue
Block a user