2014-10-05 12:44:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 19:29:31 +00:00
|
|
|
"context"
|
2014-10-05 12:44:59 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2014-10-05 12:44:59 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdLs = &cobra.Command{
|
2017-03-08 19:29:31 +00:00
|
|
|
Use: "ls [flags] [snapshot-ID ...]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "List files in a snapshot",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
|
|
|
The "ls" command allows listing files and directories in a snapshot.
|
2017-01-12 11:24:08 +00:00
|
|
|
|
|
|
|
The special snapshot-ID "latest" can be used to list files and directories of the latest snapshot in the repository.
|
2016-09-17 10:36:05 +00:00
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 10:36:05 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-03-08 19:29:31 +00:00
|
|
|
return runLs(lsOptions, globalOptions, args)
|
2016-09-17 10:36:05 +00:00
|
|
|
},
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
2014-12-07 15:30:52 +00:00
|
|
|
|
2017-01-14 03:19:47 +00:00
|
|
|
// LsOptions collects all options for the ls command.
|
|
|
|
type LsOptions struct {
|
|
|
|
ListLong bool
|
|
|
|
Host string
|
2017-07-09 10:45:49 +00:00
|
|
|
Tags restic.TagLists
|
2017-01-14 03:19:47 +00:00
|
|
|
Paths []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var lsOptions LsOptions
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdLs)
|
|
|
|
|
2017-01-14 03:19:47 +00:00
|
|
|
flags := cmdLs.Flags()
|
|
|
|
flags.BoolVarP(&lsOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode")
|
|
|
|
|
2017-03-08 19:29:31 +00:00
|
|
|
flags.StringVarP(&lsOptions.Host, "host", "H", "", "only consider snapshots for this `host`, when no snapshot ID is given")
|
2017-07-09 10:45:49 +00:00
|
|
|
flags.Var(&lsOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot ID is given")
|
2017-07-07 01:19:06 +00:00
|
|
|
flags.StringArrayVar(&lsOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot ID is given")
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 10:48:39 +00:00
|
|
|
func printTree(ctx context.Context, repo *repository.Repository, id *restic.ID, prefix string) error {
|
|
|
|
tree, err := repo.LoadTree(ctx, *id)
|
2014-10-05 12:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, entry := range tree.Nodes {
|
2017-06-10 10:17:07 +00:00
|
|
|
Printf("%s\n", formatNode(prefix, entry, lsOptions.ListLong))
|
2014-10-05 12:44:59 +00:00
|
|
|
|
2016-09-01 19:20:03 +00:00
|
|
|
if entry.Type == "dir" && entry.Subtree != nil {
|
2018-03-30 20:35:18 +00:00
|
|
|
entryPath := prefix + string(filepath.Separator) + entry.Name
|
|
|
|
if err = printTree(ctx, repo, entry.Subtree, entryPath); err != nil {
|
2014-10-05 12:44:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:29:31 +00:00
|
|
|
func runLs(opts LsOptions, gopts GlobalOptions, args []string) error {
|
|
|
|
if len(args) == 0 && opts.Host == "" && len(opts.Tags) == 0 && len(opts.Paths) == 0 {
|
|
|
|
return errors.Fatal("Invalid arguments, either give one or more snapshot IDs or set filters.")
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
repo, err := OpenRepository(gopts)
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 10:48:39 +00:00
|
|
|
if err = repo.LoadIndex(gopts.ctx); err != nil {
|
2015-05-06 01:50:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:29:31 +00:00
|
|
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
|
|
|
defer cancel()
|
2017-07-09 10:45:49 +00:00
|
|
|
for sn := range FindFilteredSnapshots(ctx, repo, opts.Host, opts.Tags, opts.Paths, args) {
|
2017-03-08 19:29:31 +00:00
|
|
|
Verbosef("snapshot %s of %v at %s):\n", sn.ID().Str(), sn.Paths, sn.Time)
|
2017-01-12 11:24:08 +00:00
|
|
|
|
2018-03-30 20:35:18 +00:00
|
|
|
if err = printTree(gopts.ctx, repo, sn.Tree, ""); err != nil {
|
2017-03-08 19:29:31 +00:00
|
|
|
return err
|
2017-01-12 11:24:08 +00:00
|
|
|
}
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
2017-03-08 19:29:31 +00:00
|
|
|
return nil
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|