From 76f49a532f7bb636cee346f4b87c20568832cc4f Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 3 Oct 2022 13:28:04 +0200 Subject: [PATCH] added shell mode output --- TODO | 4 ---- cmd/root.go | 6 ++++-- lib/common.go | 3 ++- lib/helpers.go | 3 +++ lib/printer.go | 47 +++++++++++++++++++++++++++++++++++++---------- tablizer.pod | 15 +++++++++++++-- 6 files changed, 59 insertions(+), 19 deletions(-) diff --git a/TODO b/TODO index 73b8d16..8b13789 100644 --- a/TODO +++ b/TODO @@ -1,5 +1 @@ -Add a mode like FreeBSD stat(1): - -stat -s dead.letter -st_dev=170671546954750497 st_ino=159667 st_mode=0100644 st_nlink=1 st_uid=1001 st_gid=1001 st_rdev=18446744073709551615 st_size=573 st_atime=1661994007 st_mtime=1661961878 st_ctime=1661961878 st_birthtime=1658394900 st_blksize=4096 st_blocks=3 st_flags=2048 diff --git a/cmd/root.go b/cmd/root.go index 6d047eb..2b20534 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,11 +65,13 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&lib.OutflagExtended, "extended", "X", false, "Enable extended 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.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl") + rootCmd.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output") + rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell") rootCmd.Flags().MarkHidden("extended") rootCmd.Flags().MarkHidden("orgtbl") rootCmd.Flags().MarkHidden("markdown") + rootCmd.Flags().MarkHidden("shell") // same thing but more common, takes precedence over above group - rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, ascii(default)") + rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)") } diff --git a/lib/common.go b/lib/common.go index 104d452..9e6cd85 100644 --- a/lib/common.go +++ b/lib/common.go @@ -28,7 +28,8 @@ var Separator string var OutflagExtended bool var OutflagMarkdown bool var OutflagOrgtable bool +var OutflagShell bool var OutputMode string -var Version = "v1.0.2" +var Version = "v1.0.3" var validOutputmodes = "(orgtbl|markdown|extended|ascii)" diff --git a/lib/helpers.go b/lib/helpers.go index 9498b1a..6d047a0 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -57,6 +57,9 @@ func PrepareModeFlags() error { OutputMode = "markdown" case OutflagOrgtable: OutputMode = "orgtbl" + case OutflagShell: + OutputMode = "shell" + NoNumbering = true default: OutputMode = "ascii" } diff --git a/lib/printer.go b/lib/printer.go index c045c43..937757d 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -27,20 +27,22 @@ import ( func printData(data *Tabdata) { // prepare headers: add numbers to headers - if !NoNumbering { - numberedHeaders := []string{} - for i, head := range data.headers { - if len(Columns) > 0 { - // -c specified - if !contains(UseColumns, i+1) { - // ignore this one - continue - } + numberedHeaders := []string{} + for i, head := range data.headers { + if len(Columns) > 0 { + // -c specified + if !contains(UseColumns, i+1) { + // ignore this one + continue } + } + if NoNumbering { + numberedHeaders = append(numberedHeaders, head) + } else { numberedHeaders = append(numberedHeaders, fmt.Sprintf("%s(%d)", head, i+1)) } - data.headers = numberedHeaders } + data.headers = numberedHeaders // prepare data if len(Columns) > 0 { @@ -69,6 +71,8 @@ func printData(data *Tabdata) { printOrgmodeData(data) case "markdown": printMarkdownData(data) + case "shell": + printShellData(data) default: printAsciiData(data) } @@ -177,3 +181,26 @@ func printExtendedData(data *Tabdata) { } } } + +/* + Shell output, ready to be eval'd. Just like FreeBSD stat(1) +*/ +func printShellData(data *Tabdata) { + if len(data.entries) > 0 { + var idx int + for _, entry := range data.entries { + idx = 0 + for i, value := range entry { + if len(Columns) > 0 { + if !contains(UseColumns, i+1) { + continue + } + } + + fmt.Printf("%s=\"%s\" ", data.headers[idx], value) + idx++ + } + fmt.Println() + } + } +} diff --git a/tablizer.pod b/tablizer.pod index 4808812..0451bbc 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -74,7 +74,7 @@ The numbering can be suppressed by using the B<-n> option. Finally the B<-d> option enables debugging output which is mostly usefull for the developer. -?head2 OUTPUT MODES +=head2 OUTPUT MODES There might be cases when the tabular output of a program is way too large for your current terminal but you still need to see every @@ -83,7 +83,7 @@ usefull which enables I. In this mode, each row will be printed vertically, header left, value right, aligned by the field widths. Here's an example: - kubectl get pods | ./tablizer -X + kubectl get pods | ./tablizer -o extended NAME: repldepl-7bcd8d5b64-7zq4l READY: 1/1 STATUS: Running @@ -93,6 +93,17 @@ widths. Here's an example: You can of course still use a regex to reduce the number of rows displayed. +The option B<-o shell> can be used if the output has to be processed +by the shell, it prints variable assignments for each cell, one line +per row: + + kubectl get pods | ./tablizer -o extended ./tablizer -o shell + NAME="repldepl-7bcd8d5b64-7zq4l" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h" + NAME="repldepl-7bcd8d5b64-m48n8" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h" + NAME="repldepl-7bcd8d5b64-q2bf4" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h" + +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.