From 770bfdc4d3e4ce203ca1d5e6dfdd5225488f99ae Mon Sep 17 00:00:00 2001 From: Imran Hassan Date: Thu, 5 Mar 2026 13:54:34 +0500 Subject: [PATCH] feat(others): Added functionality to output in csv --- cmd/root.go | 2 +- internal/format.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index c93b323..5bd3b86 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -118,7 +118,7 @@ func NewRootCmd() *cobra.Command { cmd.PersistentFlags().String("pattern", "", "Custom pattern (experimental)") cmd.PersistentFlags().Bool("debug", false, "Debug") _ = cmd.PersistentFlags().MarkHidden("debug") - cmd.PersistentFlags().String("format", "text", "Output format (experimental)") + cmd.PersistentFlags().String("format", "text", "Output format: text, ndjson, csv (experimental)") cmd.PersistentFlags().String("include", "", "Filter tables to scan (comma-separated, supports wildcards)") cmd.PersistentFlags().String("exclude", "", "Exclude tables from scan (comma-separated, supports wildcards)") cmd.PersistentFlags().StringP("output", "o", "", "Output file path (defaults to stdout)") diff --git a/internal/format.go b/internal/format.go index 7dbc7e3..666ca44 100644 --- a/internal/format.go +++ b/internal/format.go @@ -1,9 +1,11 @@ package internal import ( + "encoding/csv" "encoding/json" "fmt" "io" + "strconv" "strings" "github.com/fatih/color" @@ -19,6 +21,7 @@ type Formatter interface { var Formatters = map[string]Formatter{ "text": TextFormatter{}, "ndjson": JSONFormatter{}, + "csv": &CSVFormatter{}, } // TextFormatter prints the result as human readable text. @@ -106,3 +109,36 @@ func (f JSONFormatter) PrintMatch(writer io.Writer, match matchInfo) error { return encoder.Encode(entry) } } + +// CSVFormatter prints the result as CSV rows. +type CSVFormatter struct { + headerWritten bool +} + +func (f *CSVFormatter) PrintMatch(writer io.Writer, match matchInfo) error { + w := csv.NewWriter(writer) + + if !f.headerWritten { + if err := w.Write([]string{"source", "data_type", "row_count", "sample_value"}); err != nil { + return err + } + f.headerWritten = true + } + + rowCount := strconv.Itoa(match.LineCount) + + if len(match.Values) > 0 { + for _, val := range match.Values { + if err := w.Write([]string{match.Identifier, match.DisplayName, rowCount, val}); err != nil { + return err + } + } + } else { + if err := w.Write([]string{match.Identifier, match.DisplayName, rowCount, ""}); err != nil { + return err + } + } + + w.Flush() + return w.Error() +}