Rename subcommand catfile to dump

This commit is contained in:
Fabian Wickborn 2017-10-14 11:34:04 +02:00
parent 87d084e18c
commit 814e992c0b

View file

@ -13,11 +13,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var cmdCatFile = &cobra.Command{ var cmdDump = &cobra.Command{
Use: "catfile [flags] file snapshotID", Use: "dump [flags] snapshotID file",
Short: "Print a backed-up file to stdout", Short: "Print a backed-up file to stdout",
Long: ` Long: `
The "catfile" command extracts a single file from a snapshot from the repository and The "dump" command extracts a single file from a snapshot from the repository and
prints its contents to stdout. prints its contents to stdout.
The special snapshot "latest" can be used to use the latest snapshot in the The special snapshot "latest" can be used to use the latest snapshot in the
@ -25,26 +25,26 @@ repository.
`, `,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runCatFile(catFileOptions, globalOptions, args) return runDump(dumpOptions, globalOptions, args)
}, },
} }
// CatFileOptions collects all options for the catfile command. // DumpOptions collects all options for the dump command.
type CatFileOptions struct { type DumpOptions struct {
Host string Host string
Paths []string Paths []string
Tags restic.TagLists Tags restic.TagLists
} }
var catFileOptions CatFileOptions var dumpOptions DumpOptions
func init() { func init() {
cmdRoot.AddCommand(cmdCatFile) cmdRoot.AddCommand(cmdDump)
flags := cmdCatFile.Flags() flags := cmdDump.Flags()
flags.StringVarP(&catFileOptions.Host, "host", "H", "", `only consider snapshots for this host when the snapshot ID is "latest"`) flags.StringVarP(&dumpOptions.Host, "host", "H", "", `only consider snapshots for this host when the snapshot ID is "latest"`)
flags.Var(&catFileOptions.Tags, "tag", "only consider snapshots which include this `taglist` for snapshot ID \"latest\"") flags.Var(&dumpOptions.Tags, "tag", "only consider snapshots which include this `taglist` for snapshot ID \"latest\"")
flags.StringArrayVar(&catFileOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"") flags.StringArrayVar(&dumpOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"")
} }
func splitPath(path string) []string { func splitPath(path string) []string {
@ -56,7 +56,7 @@ func splitPath(path string) []string {
return append(s, f) return append(s, f)
} }
func catNode(ctx context.Context, repo restic.Repository, node *restic.Node) error { func dumpNode(ctx context.Context, repo restic.Repository, node *restic.Node) error {
var buf []byte var buf []byte
for _, id := range node.Content { for _, id := range node.Content {
size, err := repo.LookupBlobSize(id, restic.DataBlob) size, err := repo.LookupBlobSize(id, restic.DataBlob)
@ -99,7 +99,7 @@ func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.Repositor
if node.Name == pathComponents[0] { if node.Name == pathComponents[0] {
switch { switch {
case l == 1 && node.Type == "file": case l == 1 && node.Type == "file":
return catNode(ctx, repo, node) return dumpNode(ctx, repo, node)
case l > 1 && node.Type == "dir": case l > 1 && node.Type == "dir":
subtree, err := repo.LoadTree(ctx, *node.Subtree) subtree, err := repo.LoadTree(ctx, *node.Subtree)
if err != nil { if err != nil {
@ -116,17 +116,17 @@ func printFromTree(ctx context.Context, tree *restic.Tree, repo restic.Repositor
return fmt.Errorf("path %q not found in snapshot", item) return fmt.Errorf("path %q not found in snapshot", item)
} }
func runCatFile(opts CatFileOptions, gopts GlobalOptions, args []string) error { func runDump(opts DumpOptions, gopts GlobalOptions, args []string) error {
ctx := gopts.ctx ctx := gopts.ctx
if len(args) != 2 { if len(args) != 2 {
return errors.Fatal("no file and no snapshot ID specified") return errors.Fatal("no file and no snapshot ID specified")
} }
pathToPrint := args[0] snapshotIDString := args[0]
snapshotIDString := args[1] pathToPrint := args[1]
debug.Log("cat file %q from %q", pathToPrint, snapshotIDString) debug.Log("dump file %q from %q", pathToPrint, snapshotIDString)
splittedPath := splitPath(pathToPrint) splittedPath := splitPath(pathToPrint)
@ -174,7 +174,7 @@ func runCatFile(opts CatFileOptions, gopts GlobalOptions, args []string) error {
err = printFromTree(ctx, tree, repo, "", splittedPath) err = printFromTree(ctx, tree, repo, "", splittedPath)
if err != nil { if err != nil {
Exitf(2, "cannot cat file: %v", err) Exitf(2, "cannot dump file: %v", err)
} }
return nil return nil