2017-10-17 11:06:40 +00:00
|
|
|
package inspect
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-17 07:03:41 +00:00
|
|
|
"flag"
|
2016-10-31 23:37:27 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2016-11-04 04:36:25 +00:00
|
|
|
"github.com/hashicorp/consul/snapshot"
|
2017-10-17 07:03:41 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2016-10-31 23:37:27 +00:00
|
|
|
)
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
usage string
|
|
|
|
}
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.usage = flags.Usage(usage, c.flags, nil, nil)
|
|
|
|
}
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return "Displays information about a Consul snapshot file"
|
|
|
|
}
|
2016-10-31 23:37:27 +00:00
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.usage
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var file string
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
args = c.flags.Args()
|
2016-10-31 23:37:27 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Missing FILE argument")
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
case 1:
|
|
|
|
file = args[0]
|
|
|
|
default:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file.
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
|
2016-10-31 23:37:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
meta, err := snapshot.Verify(f)
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error verifying snapshot: %s", err))
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
tw := tabwriter.NewWriter(&b, 0, 2, 6, ' ', 0)
|
|
|
|
fmt.Fprintf(tw, "ID\t%s\n", meta.ID)
|
|
|
|
fmt.Fprintf(tw, "Size\t%d\n", meta.Size)
|
|
|
|
fmt.Fprintf(tw, "Index\t%d\n", meta.Index)
|
|
|
|
fmt.Fprintf(tw, "Term\t%d\n", meta.Term)
|
|
|
|
fmt.Fprintf(tw, "Version\t%d\n", meta.Version)
|
|
|
|
if err = tw.Flush(); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error rendering snapshot info: %s", err))
|
2016-10-31 23:37:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Info(b.String())
|
2016-10-31 23:37:27 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-17 07:03:41 +00:00
|
|
|
const usage = `Usage: consul snapshot inspect [options] FILE
|
|
|
|
|
|
|
|
Displays information about a snapshot file on disk.
|
|
|
|
|
|
|
|
To inspect the file "backup.snap":
|
|
|
|
|
|
|
|
$ consul snapshot inspect backup.snap
|
|
|
|
|
|
|
|
For a full list of options and examples, please see the Consul documentation.`
|