2014-08-01 20:20:28 +00:00
|
|
|
package main
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
import (
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2020-06-14 07:50:11 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-09-17 10:36:05 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2016-09-01 20:17:37 +00:00
|
|
|
)
|
2014-08-01 20:20:28 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdList = &cobra.Command{
|
2020-08-27 03:29:43 +00:00
|
|
|
Use: "list [flags] [blobs|packs|index|snapshots|keys|locks]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "List objects in the repository",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
2017-02-13 15:06:27 +00:00
|
|
|
The "list" command allows listing objects in the repository based on type.
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
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 {
|
2018-05-17 17:41:56 +00:00
|
|
|
return runList(cmd, globalOptions, args)
|
2016-09-17 10:36:05 +00:00
|
|
|
},
|
2014-11-30 21:39:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdList)
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 17:41:56 +00:00
|
|
|
func runList(cmd *cobra.Command, opts GlobalOptions, args []string) error {
|
2014-10-05 12:44:59 +00:00
|
|
|
if len(args) != 1 {
|
2018-05-17 17:41:56 +00:00
|
|
|
return errors.Fatal("type not specified, usage: " + cmd.Use)
|
2014-12-07 15:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
repo, err := OpenRepository(opts)
|
2014-12-07 15:30:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
2014-08-01 20:20:28 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if !opts.NoLock {
|
2015-11-10 21:13:53 +00:00
|
|
|
lock, err := lockRepo(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:29 +00:00
|
|
|
var t restic.FileType
|
2014-10-05 12:44:59 +00:00
|
|
|
switch args[0] {
|
2015-04-26 15:44:38 +00:00
|
|
|
case "packs":
|
2020-08-16 09:16:38 +00:00
|
|
|
t = restic.PackFile
|
2015-04-26 15:44:38 +00:00
|
|
|
case "index":
|
2016-09-01 14:04:29 +00:00
|
|
|
t = restic.IndexFile
|
2014-10-05 12:44:59 +00:00
|
|
|
case "snapshots":
|
2016-09-01 14:04:29 +00:00
|
|
|
t = restic.SnapshotFile
|
2014-10-05 12:44:59 +00:00
|
|
|
case "keys":
|
2016-09-01 14:04:29 +00:00
|
|
|
t = restic.KeyFile
|
2014-10-05 12:44:59 +00:00
|
|
|
case "locks":
|
2016-09-01 14:04:29 +00:00
|
|
|
t = restic.LockFile
|
2017-01-30 09:28:17 +00:00
|
|
|
case "blobs":
|
2020-06-14 07:50:11 +00:00
|
|
|
return repo.List(opts.ctx, restic.IndexFile, func(id restic.ID, size int64) error {
|
|
|
|
idx, err := repository.LoadIndex(opts.ctx, repo, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-30 09:28:17 +00:00
|
|
|
}
|
2020-06-14 07:50:11 +00:00
|
|
|
for blobs := range idx.Each(opts.ctx) {
|
|
|
|
Printf("%v %v\n", blobs.Type, blobs.ID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-01-30 09:28:17 +00:00
|
|
|
|
2014-10-05 12:44:59 +00:00
|
|
|
default:
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("invalid type")
|
2014-10-05 12:44:59 +00:00
|
|
|
}
|
2014-08-01 20:20:28 +00:00
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
return repo.List(opts.ctx, t, func(id restic.ID, size int64) error {
|
2016-09-17 10:36:05 +00:00
|
|
|
Printf("%s\n", id)
|
2018-01-21 16:25:36 +00:00
|
|
|
return nil
|
|
|
|
})
|
2014-08-01 20:20:28 +00:00
|
|
|
}
|