From 76930ab45a630002a223a6b210bfb41a8390c7be Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sun, 16 Oct 2022 19:44:26 +0200 Subject: [PATCH] added yaml output mode support (`-o yaml` or `-Y`) --- cmd/root.go | 4 +++- cmd/tablizer.go | 5 +++-- go.mod | 1 + go.sum | 1 + lib/common.go | 3 ++- lib/helpers.go | 3 +++ lib/printer.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- lib/printer_test.go | 16 +++++++++++++++- tablizer.1 | 7 ++++--- tablizer.pod | 5 +++-- 10 files changed, 79 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 68fff44..e2d3421 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -100,11 +100,13 @@ func init() { 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.PersistentFlags().BoolVarP(&lib.OutflagYaml, "yaml", "Y", false, "Enable yaml output") + rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell", "yaml") rootCmd.Flags().MarkHidden("extended") rootCmd.Flags().MarkHidden("orgtbl") rootCmd.Flags().MarkHidden("markdown") rootCmd.Flags().MarkHidden("shell") + rootCmd.Flags().MarkHidden("yaml") // 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)") diff --git a/cmd/tablizer.go b/cmd/tablizer.go index db0fbea..332edef 100644 --- a/cmd/tablizer.go +++ b/cmd/tablizer.go @@ -16,7 +16,7 @@ SYNOPSIS -m, --man Display manual page -n, --no-numbering Disable header numbering -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 -M, --markdown Enable markdown 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 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 In order to report a bug, unexpected behavior, feature requests or to diff --git a/go.mod b/go.mod index c87d4d8..88d842e 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/gookit/color v1.5.2 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.5.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( diff --git a/go.sum b/go.sum index 3cc1acc..82aca9f 100644 --- a/go.sum +++ b/go.sum @@ -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= 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= +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/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= diff --git a/lib/common.go b/lib/common.go index 3cba0a0..4a9341b 100644 --- a/lib/common.go +++ b/lib/common.go @@ -36,6 +36,7 @@ var ( OutflagMarkdown bool OutflagOrgtable bool OutflagShell bool + OutflagYaml bool OutputMode string InvertMatch bool Pattern string @@ -65,7 +66,7 @@ var ( } // used for validation - validOutputmodes = "(orgtbl|markdown|extended|ascii)" + validOutputmodes = "(orgtbl|markdown|extended|ascii|yaml)" // main program version Version = "v1.0.11" diff --git a/lib/helpers.go b/lib/helpers.go index 89150cf..e73ee59 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -154,6 +154,9 @@ func PrepareModeFlags() error { case OutflagShell: OutputMode = "shell" NoNumbering = true + case OutflagYaml: + OutputMode = "yaml" + NoNumbering = true default: OutputMode = "ascii" } diff --git a/lib/printer.go b/lib/printer.go index 6eb8178..bcb749b 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -21,7 +21,11 @@ import ( "fmt" "github.com/gookit/color" "github.com/olekukonko/tablewriter" + "gopkg.in/yaml.v3" + "log" + "os" "regexp" + "strconv" "strings" ) @@ -50,6 +54,8 @@ func printData(data *Tabdata) { printMarkdownData(data) case "shell": printShellData(data) + case "yaml": + printYamlData(data) default: 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++ } 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) +} diff --git a/lib/printer_test.go b/lib/printer_test.go index bc3d16d..2ff0b1e 100644 --- a/lib/printer_test.go +++ b/lib/printer_test.go @@ -88,6 +88,13 @@ THREE(3): cxxxncnc ONE(1): 19191 TWO(2): EDD 1 THREE(3): X`, + "yaml": `entries: + - one: "asd" + three: "cxxxncnc" + two: "igig" + - one: 19191 + three: "X" + two: "EDD 1"`, } NoColor = true @@ -100,6 +107,13 @@ THREE(3): X`, t.Run(testname, func(t *testing.T) { OutputMode = mode + + if mode == "yaml" { + NoNumbering = true + } else { + NoNumbering = false + } + // we need to reset our mock data, since it's being // modified in printData() data := startdata @@ -122,7 +136,7 @@ THREE(3): X`, // Restore os.Stdout = origStdout - + NoNumbering = false } func TestSortPrinter(t *testing.T) { diff --git a/tablizer.1 b/tablizer.1 index 9b491b2..e867e0b 100644 --- a/tablizer.1 +++ b/tablizer.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .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 .\" way too many mistakes in technical documents. .if n .ad l @@ -154,7 +154,7 @@ tablizer \- Manipulate tabular output of other programs \& \-m, \-\-man Display manual page \& \-n, \-\-no\-numbering Disable header numbering \& \-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 \& \-M, \-\-markdown Enable markdown table output \& \-O, \-\-orgtbl Enable org\-mode table output @@ -345,7 +345,8 @@ You can use this in an eval loop. .PP Beside normal ascii mode (the default) and extended mode there are 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" .IX Header "BUGS" In order to report a bug, unexpected behavior, feature requests diff --git a/tablizer.pod b/tablizer.pod index 44aca7a..b2091e4 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -15,7 +15,7 @@ tablizer - Manipulate tabular output of other programs -m, --man Display manual page -n, --no-numbering Disable header numbering -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 -M, --markdown Enable markdown 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 more output modes available: B which prints an Emacs org-mode -table and B which prints a Markdown table. +table and B which prints a Markdown table and B, which +prints yaml encoding. =head1 BUGS