From 9945a67bfffb7fb3738b8d509c79fec693f6522e Mon Sep 17 00:00:00 2001 From: Bas Harenslak Date: Mon, 2 Oct 2017 22:25:51 +0200 Subject: [PATCH] Replace columnize by stdlib tabwriter --- cmd/tsdb/main.go | 4 ++-- db.go | 15 ++++++++------- tabwriter.go | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 tabwriter.go diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 514a0bed19..e7e77d0d2b 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -44,7 +44,7 @@ func main() { benchWriteNumMetrics = benchWriteCmd.Flag("metrics", "number of metrics to read").Default("10000").Int() benchSamplesFile = benchWriteCmd.Arg("file", "input file with samples data, default is (../../testdata/20k.series)").Default("../../testdata/20k.series").String() listCmd = cli.Command("ls", "list db blocks") - listPath = listCmd.Arg("db path", "database path").Default("benchout/storage").String() + listPath = listCmd.Arg("db path", "database path (default is benchout/storage)").Default("benchout/storage").String() ) switch kingpin.MustParse(cli.Parse(os.Args[1:])) { @@ -60,7 +60,7 @@ func main() { if err != nil { exitWithError(err) } - fmt.Println(db.PrintBlocks()) + db.PrintBlocks() } flag.CommandLine.Set("log.level", "debug") } diff --git a/db.go b/db.go index d353040ca9..f72c9e7e3b 100644 --- a/db.go +++ b/db.go @@ -38,7 +38,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/labels" - "github.com/ryanuber/columnize" ) // DefaultOptions used for the DB. They are sane for setups using @@ -229,23 +228,25 @@ func (db *DB) Dir() string { return db.dir } -func (db *DB) PrintBlocks() string { +func (db *DB) PrintBlocks() { db.mtx.RLock() defer db.mtx.RUnlock() - var output []string - output = append(output, "BLOCK ULID | MIN TIME | MAX TIME | NUM SAMPLES | NUM CHUNKS | NUM SERIES") + tw := GetNewTabWriter(os.Stdout) + defer tw.Flush() + + fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") for _, b := range db.blocks { - output = append(output, fmt.Sprintf("%v | %v | %v | %v | %v | %v", + fmt.Fprintf(tw, + "%v\t%v\t%v\t%v\t%v\t%v\n", b.Meta().ULID, b.Meta().MinTime, b.Meta().MaxTime, b.Meta().Stats.NumSamples, b.Meta().Stats.NumChunks, b.Meta().Stats.NumSeries, - )) + ) } - return columnize.SimpleFormat(output) } func (db *DB) run() { diff --git a/tabwriter.go b/tabwriter.go new file mode 100644 index 0000000000..8e84a9c67a --- /dev/null +++ b/tabwriter.go @@ -0,0 +1,18 @@ +package tsdb + +import ( + "io" + "text/tabwriter" +) + +const ( + minwidth = 0 + tabwidth = 0 + padding = 2 + padchar = ' ' + flags = 0 +) + +func GetNewTabWriter(output io.Writer) *tabwriter.Writer { + return tabwriter.NewWriter(output, minwidth, tabwidth, padding, padchar, flags) +}