From ac72d5b271443f575c95d22c9c99c71cedb3cf67 Mon Sep 17 00:00:00 2001 From: Rahul Jadhav Date: Sat, 1 Jul 2023 00:49:43 +0530 Subject: [PATCH] added markdown and html export options Signed-off-by: Rahul Jadhav --- pkg/config/config.go | 6 ++++-- pkg/drawtable/csv2table.go | 22 +++++++++++++++++++++- tests/config.yaml | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7293b81..6d81ffb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -25,8 +25,10 @@ import ( ) type TableConfig struct { - Title string `yaml:"title"` - Caption string `yaml:"caption"` + Title string `yaml:"title"` + Caption string `yaml:"caption"` + Markdown string `yaml:"markdown"` + Html string `yaml:"html"` } type Paint struct { diff --git a/pkg/drawtable/csv2table.go b/pkg/drawtable/csv2table.go index 98bcb71..a77c99c 100644 --- a/pkg/drawtable/csv2table.go +++ b/pkg/drawtable/csv2table.go @@ -140,6 +140,25 @@ func getAllColConfigs(hdr []string, cols []config.ColConfig) []table.ColumnConfi return colcfgs } +func write2file(t table.Writer, fname string, ftype string) { + if fname == "" { + return + } + f, err := os.Create(fname) + if err != nil { + log.Fatalf("could not create file %s", fname) + } + defer f.Close() + t.SetOutputMirror(f) + if ftype == "md" { + log.Println("rendering MD") + t.RenderMarkdown() + } else if ftype == "html" { + log.Println("rendering HTML") + t.RenderHTML() + } +} + func Csv2Table(cfg config.Config) { records := readCsvFile(cfg.InFile) if len(records) <= 1 { @@ -190,5 +209,6 @@ func Csv2Table(cfg config.Config) { }) t.SetStyle(table.StyleLight) t.Render() - // t.RenderMarkdown() + write2file(t, cfg.YamlCfg.Table.Markdown, "md") + write2file(t, cfg.YamlCfg.Table.Html, "html") } diff --git a/tests/config.yaml b/tests/config.yaml index 2ff3976..b237b29 100644 --- a/tests/config.yaml +++ b/tests/config.yaml @@ -20,6 +20,8 @@ --- table: title: "Kubernetes Cluster Service Ports status" + markdown: /tmp/out.md + html: /tmp/out.html columns: - name: Name maxwidth: 30