github.com/Iqoqo/consul@v1.4.5/command/snapshot/inspect/snapshot_inspect.go (about)

     1  package inspect
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"text/tabwriter"
     9  
    10  	"github.com/hashicorp/consul/command/flags"
    11  	"github.com/hashicorp/consul/snapshot"
    12  	"github.com/mitchellh/cli"
    13  )
    14  
    15  func New(ui cli.Ui) *cmd {
    16  	c := &cmd{UI: ui}
    17  	c.init()
    18  	return c
    19  }
    20  
    21  type cmd struct {
    22  	UI    cli.Ui
    23  	flags *flag.FlagSet
    24  	help  string
    25  }
    26  
    27  func (c *cmd) init() {
    28  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    29  	c.help = flags.Usage(help, c.flags)
    30  }
    31  
    32  func (c *cmd) Run(args []string) int {
    33  	if err := c.flags.Parse(args); err != nil {
    34  		return 1
    35  	}
    36  
    37  	var file string
    38  
    39  	args = c.flags.Args()
    40  	switch len(args) {
    41  	case 0:
    42  		c.UI.Error("Missing FILE argument")
    43  		return 1
    44  	case 1:
    45  		file = args[0]
    46  	default:
    47  		c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
    48  		return 1
    49  	}
    50  
    51  	// Open the file.
    52  	f, err := os.Open(file)
    53  	if err != nil {
    54  		c.UI.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
    55  		return 1
    56  	}
    57  	defer f.Close()
    58  
    59  	meta, err := snapshot.Verify(f)
    60  	if err != nil {
    61  		c.UI.Error(fmt.Sprintf("Error verifying snapshot: %s", err))
    62  		return 1
    63  	}
    64  
    65  	var b bytes.Buffer
    66  	tw := tabwriter.NewWriter(&b, 0, 2, 6, ' ', 0)
    67  	fmt.Fprintf(tw, "ID\t%s\n", meta.ID)
    68  	fmt.Fprintf(tw, "Size\t%d\n", meta.Size)
    69  	fmt.Fprintf(tw, "Index\t%d\n", meta.Index)
    70  	fmt.Fprintf(tw, "Term\t%d\n", meta.Term)
    71  	fmt.Fprintf(tw, "Version\t%d\n", meta.Version)
    72  	if err = tw.Flush(); err != nil {
    73  		c.UI.Error(fmt.Sprintf("Error rendering snapshot info: %s", err))
    74  		return 1
    75  	}
    76  
    77  	c.UI.Info(b.String())
    78  
    79  	return 0
    80  }
    81  
    82  func (c *cmd) Synopsis() string {
    83  	return synopsis
    84  }
    85  
    86  func (c *cmd) Help() string {
    87  	return c.help
    88  }
    89  
    90  const synopsis = "Displays information about a Consul snapshot file"
    91  const help = `
    92  Usage: consul snapshot inspect [options] FILE
    93  
    94    Displays information about a snapshot file on disk.
    95  
    96    To inspect the file "backup.snap":
    97  
    98      $ consul snapshot inspect backup.snap
    99  
   100    For a full list of options and examples, please see the Consul documentation.
   101  `