added yaml output mode support (-o yaml or -Y)

This commit is contained in:
2022-10-16 19:44:26 +02:00
parent a77e4dbc5a
commit 76930ab45a
10 changed files with 79 additions and 11 deletions

View File

@@ -100,11 +100,13 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagMarkdown, "markdown", "M", false, "Enable markdown table 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.OutflagOrgtable, "orgtbl", "O", false, "Enable org-mode table output")
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output") rootCmd.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output")
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell") rootCmd.PersistentFlags().BoolVarP(&lib.OutflagYaml, "yaml", "Y", false, "Enable yaml output")
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell", "yaml")
rootCmd.Flags().MarkHidden("extended") rootCmd.Flags().MarkHidden("extended")
rootCmd.Flags().MarkHidden("orgtbl") rootCmd.Flags().MarkHidden("orgtbl")
rootCmd.Flags().MarkHidden("markdown") rootCmd.Flags().MarkHidden("markdown")
rootCmd.Flags().MarkHidden("shell") rootCmd.Flags().MarkHidden("shell")
rootCmd.Flags().MarkHidden("yaml")
// 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)")

View File

@@ -16,7 +16,7 @@ SYNOPSIS
-m, --man Display manual page -m, --man Display manual page
-n, --no-numbering Disable header numbering -n, --no-numbering Disable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default) -o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
-X, --extended Enable extended output -X, --extended Enable extended output
-M, --markdown Enable markdown table output -M, --markdown Enable markdown table output
-O, --orgtbl Enable org-mode table output -O, --orgtbl Enable org-mode table output
@@ -178,7 +178,8 @@ DESCRIPTION
Beside normal ascii mode (the default) and extended mode there are more Beside normal ascii mode (the default) and extended mode there are more
output modes available: orgtbl which prints an Emacs org-mode table and output modes available: orgtbl which prints an Emacs org-mode table and
markdown which prints a Markdown table. markdown which prints a Markdown table and yaml, which prints yaml
encoding.
BUGS BUGS
In order to report a bug, unexpected behavior, feature requests or to In order to report a bug, unexpected behavior, feature requests or to

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/gookit/color v1.5.2 github.com/gookit/color v1.5.2
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.5.0 github.com/spf13/cobra v1.5.0
gopkg.in/yaml.v3 v3.0.1
) )
require ( require (

1
go.sum
View File

@@ -35,6 +35,7 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHg
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -36,6 +36,7 @@ var (
OutflagMarkdown bool OutflagMarkdown bool
OutflagOrgtable bool OutflagOrgtable bool
OutflagShell bool OutflagShell bool
OutflagYaml bool
OutputMode string OutputMode string
InvertMatch bool InvertMatch bool
Pattern string Pattern string
@@ -65,7 +66,7 @@ var (
} }
// used for validation // used for validation
validOutputmodes = "(orgtbl|markdown|extended|ascii)" validOutputmodes = "(orgtbl|markdown|extended|ascii|yaml)"
// main program version // main program version
Version = "v1.0.11" Version = "v1.0.11"

View File

@@ -154,6 +154,9 @@ func PrepareModeFlags() error {
case OutflagShell: case OutflagShell:
OutputMode = "shell" OutputMode = "shell"
NoNumbering = true NoNumbering = true
case OutflagYaml:
OutputMode = "yaml"
NoNumbering = true
default: default:
OutputMode = "ascii" OutputMode = "ascii"
} }

View File

@@ -21,7 +21,11 @@ import (
"fmt" "fmt"
"github.com/gookit/color" "github.com/gookit/color"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v3"
"log"
"os"
"regexp" "regexp"
"strconv"
"strings" "strings"
) )
@@ -50,6 +54,8 @@ func printData(data *Tabdata) {
printMarkdownData(data) printMarkdownData(data)
case "shell": case "shell":
printShellData(data) printShellData(data)
case "yaml":
printYamlData(data)
default: default:
printAsciiData(data) printAsciiData(data)
} }
@@ -174,10 +180,47 @@ func printShellData(data *Tabdata) {
} }
} }
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"", data.headers[idx], value)) shentries = append(shentries, fmt.Sprintf("%s=\"%s\"",
data.headers[idx], value))
idx++ idx++
} }
fmt.Println(strings.Join(shentries, " ")) fmt.Println(strings.Join(shentries, " "))
} }
} }
} }
func printYamlData(data *Tabdata) {
type D struct {
Entries []map[string]interface{} `yaml:"entries"`
}
d := D{}
for _, entry := range data.entries {
ml := map[string]interface{}{}
for i, entry := range entry {
style := yaml.TaggedStyle
_, err := strconv.Atoi(entry)
if err != nil {
style = yaml.DoubleQuotedStyle
}
ml[strings.ToLower(data.headers[i])] =
&yaml.Node{
Kind: yaml.ScalarNode,
Style: style,
Value: entry}
}
d.Entries = append(d.Entries, ml)
}
yamlstr, err := yaml.Marshal(&d)
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(yamlstr)
}

View File

@@ -88,6 +88,13 @@ THREE(3): cxxxncnc
ONE(1): 19191 ONE(1): 19191
TWO(2): EDD 1 TWO(2): EDD 1
THREE(3): X`, THREE(3): X`,
"yaml": `entries:
- one: "asd"
three: "cxxxncnc"
two: "igig"
- one: 19191
three: "X"
two: "EDD 1"`,
} }
NoColor = true NoColor = true
@@ -100,6 +107,13 @@ THREE(3): X`,
t.Run(testname, func(t *testing.T) { t.Run(testname, func(t *testing.T) {
OutputMode = mode OutputMode = mode
if mode == "yaml" {
NoNumbering = true
} else {
NoNumbering = false
}
// we need to reset our mock data, since it's being // we need to reset our mock data, since it's being
// modified in printData() // modified in printData()
data := startdata data := startdata
@@ -122,7 +136,7 @@ THREE(3): X`,
// Restore // Restore
os.Stdout = origStdout os.Stdout = origStdout
NoNumbering = false
} }
func TestSortPrinter(t *testing.T) { func TestSortPrinter(t *testing.T) {

View File

@@ -133,7 +133,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "TABLIZER 1" .IX Title "TABLIZER 1"
.TH TABLIZER 1 "2022-10-15" "1" "User Commands" .TH TABLIZER 1 "2022-10-16" "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
@@ -154,7 +154,7 @@ tablizer \- Manipulate tabular output of other programs
\& \-m, \-\-man Display manual page \& \-m, \-\-man Display manual page
\& \-n, \-\-no\-numbering Disable header numbering \& \-n, \-\-no\-numbering Disable header numbering
\& \-N, \-\-no\-color Disable pattern highlighting \& \-N, \-\-no\-color Disable pattern highlighting
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default) \& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, yaml, ascii(default)
\& \-X, \-\-extended Enable extended output \& \-X, \-\-extended Enable extended output
\& \-M, \-\-markdown Enable markdown table output \& \-M, \-\-markdown Enable markdown table output
\& \-O, \-\-orgtbl Enable org\-mode table output \& \-O, \-\-orgtbl Enable org\-mode table output
@@ -345,7 +345,8 @@ You can use this in an eval loop.
.PP .PP
Beside normal ascii mode (the default) and extended mode there are Beside normal ascii mode (the default) and extended mode there are
more output modes available: \fBorgtbl\fR which prints an Emacs org-mode more output modes available: \fBorgtbl\fR which prints an Emacs org-mode
table and \fBmarkdown\fR which prints a Markdown table. table and \fBmarkdown\fR which prints a Markdown table and \fByaml\fR, which
prints yaml encoding.
.SH "BUGS" .SH "BUGS"
.IX Header "BUGS" .IX Header "BUGS"
In order to report a bug, unexpected behavior, feature requests In order to report a bug, unexpected behavior, feature requests

View File

@@ -15,7 +15,7 @@ tablizer - Manipulate tabular output of other programs
-m, --man Display manual page -m, --man Display manual page
-n, --no-numbering Disable header numbering -n, --no-numbering Disable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default) -o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
-X, --extended Enable extended output -X, --extended Enable extended output
-M, --markdown Enable markdown table output -M, --markdown Enable markdown table output
-O, --orgtbl Enable org-mode table output -O, --orgtbl Enable org-mode table output
@@ -198,7 +198,8 @@ You can use this in an eval loop.
Beside normal ascii mode (the default) and extended mode there are Beside normal ascii mode (the default) and extended mode there are
more output modes available: B<orgtbl> which prints an Emacs org-mode more output modes available: B<orgtbl> which prints an Emacs org-mode
table and B<markdown> which prints a Markdown table. table and B<markdown> which prints a Markdown table and B<yaml>, which
prints yaml encoding.
=head1 BUGS =head1 BUGS