From b6fdb355d749fc6c3081ef086503bcdbd2a35b7f Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 7 Mar 2016 16:30:19 +0100 Subject: [PATCH] Move dump-heads into its own tool --- cmd/promtool/main.go | 19 -------- storage/local/storagetool/main.go | 76 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 storage/local/storagetool/main.go diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index c5b4efac9..a508073ff 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -24,7 +24,6 @@ import ( "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/promql" - "github.com/prometheus/prometheus/storage/local" "github.com/prometheus/prometheus/util/cli" "github.com/prometheus/prometheus/version" ) @@ -185,19 +184,6 @@ func checkRules(t cli.Term, filename string) (int, error) { return len(rules), nil } -// DumpHeadsCmd dumps metadata of a heads.db file. -func DumpHeadsCmd(t cli.Term, args ...string) int { - if len(args) != 1 { - t.Infof("usage: promtool dump-heads ") - return 2 - } - if err := local.DumpHeads(args[0], t.Out()); err != nil { - t.Errorf(" FAILED: %s", err) - return 1 - } - return 0 -} - var versionInfoTmpl = ` prometheus, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) build user: {{.buildUser}} @@ -230,11 +216,6 @@ func main() { Run: CheckRulesCmd, }) - app.Register("dump-heads", &cli.Command{ - Desc: "dump metadata of a heads.db checkpoint file", - Run: DumpHeadsCmd, - }) - app.Register("version", &cli.Command{ Desc: "print the version of this binary", Run: VersionCmd, diff --git a/storage/local/storagetool/main.go b/storage/local/storagetool/main.go new file mode 100644 index 000000000..10cef1902 --- /dev/null +++ b/storage/local/storagetool/main.go @@ -0,0 +1,76 @@ +// Copyright 2016 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "bytes" + "fmt" + "os" + "strings" + "text/template" + + "github.com/prometheus/prometheus/util/cli" + "github.com/prometheus/prometheus/version" + + "github.com/prometheus/prometheus/storage/local" +) + +// DumpHeadsCmd dumps metadata of a heads.db file. +func DumpHeadsCmd(t cli.Term, args ...string) int { + if len(args) != 1 { + t.Infof("usage: storagetool dump-heads ") + return 2 + } + if err := local.DumpHeads(args[0], t.Out()); err != nil { + t.Errorf(" FAILED: %s", err) + return 1 + } + return 0 +} + +var versionInfoTmpl = ` +prometheus, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) + build user: {{.buildUser}} + build date: {{.buildDate}} + go version: {{.goVersion}} +` + +// VersionCmd prints the binaries version information. +func VersionCmd(t cli.Term, _ ...string) int { + tmpl := template.Must(template.New("version").Parse(versionInfoTmpl)) + + var buf bytes.Buffer + if err := tmpl.ExecuteTemplate(&buf, "version", version.Map); err != nil { + panic(err) + } + fmt.Fprintln(t.Out(), strings.TrimSpace(buf.String())) + return 0 +} + +func main() { + app := cli.NewApp("storagetool") + + app.Register("dump-heads", &cli.Command{ + Desc: "dump metadata of a heads.db checkpoint file", + Run: DumpHeadsCmd, + }) + + app.Register("version", &cli.Command{ + Desc: "print the version of this binary", + Run: VersionCmd, + }) + + t := cli.BasicTerm(os.Stdout, os.Stderr) + os.Exit(app.Run(t, os.Args[1:]...)) +}