Skip to content

Commit

Permalink
adds new tsv writer
Browse files Browse the repository at this point in the history
Signed-off-by: someshkoli <kolisomesh27@gmail.com>
  • Loading branch information
someshkoli committed May 13, 2021
1 parent ce8f8c5 commit ae26d9e
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions cmd/thanos/tools_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -69,6 +70,7 @@ var (
},
}
inspectColumns = []string{"ULID", "FROM", "UNTIL", "RANGE", "UNTIL-DOWN", "#SERIES", "#SAMPLES", "#CHUNKS", "COMP-LEVEL", "COMP-FAILED", "LABELS", "RESOLUTION", "SOURCE"}
outputTypes = []string{"table", "tsv", "csv"}
)

type outputType string
Expand Down Expand Up @@ -281,8 +283,7 @@ func registerBucketInspect(app extkingpin.AppClause, objStoreConfig *extflag.Pat
Default("FROM", "UNTIL").Enums(inspectColumns...)
timeout := cmd.Flag("timeout", "Timeout to download metadata from remote storage").Default("5m").Duration()

output := cmd.Flag("output", "Output format for result. Currently supports table, cvs, and, tsv.").Default("table").String()
opType := outputType(*output)
output := cmd.Flag("output", "Output format for result. Currently supports table, cvs, and, tsv.").Default("table").Enum(outputTypes...)

cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ <-chan struct{}, _ bool) error {

Expand Down Expand Up @@ -325,15 +326,18 @@ func registerBucketInspect(app extkingpin.AppClause, objStoreConfig *extflag.Pat
for _, meta := range metas {
blockMetas = append(blockMetas, meta)
}
if opType == TABLE {
return printBlockData(blockMetas, selectorLabels, *sortBy, printTable)
} else if opType == TSV {
return printBlockData(blockMetas, selectorLabels, *sortBy, printTSV)
} else if opType == CSV {
return printBlockData(blockMetas, selectorLabels, *sortBy, printCSV)
} else {
return fmt.Errorf("Invalid output type %s.", *output)

var opPrinter tablePrinter
op := outputType(*output)
switch op {
case TABLE:
opPrinter = printTable
case TSV:
opPrinter = printTSV
case CSV:
opPrinter = printCSV
}
return printBlockData(blockMetas, selectorLabels, *sortBy, opPrinter)
})
}

Expand Down Expand Up @@ -664,18 +668,23 @@ func printCSV(t Table) error {
return nil
}

func newTSVWriter(w io.Writer) *csv.Writer {
writer := csv.NewWriter(w)
writer.Comma = rune('\t')
return writer
}

func printTSV(t Table) error {
csv := csv.NewWriter(os.Stdout)
csv.Comma = '\t'
err := csv.Write(t.Header)
tsv := newTSVWriter(os.Stdout)
err := tsv.Write(t.Header)
if err != nil {
return err
}
err = csv.WriteAll(t.Lines)
err = tsv.WriteAll(t.Lines)
if err != nil {
return err
}
csv.Flush()
tsv.Flush()
return nil
}

Expand Down

0 comments on commit ae26d9e

Please sign in to comment.