restic/cmd/restic/cmd_list.go

83 lines
1.5 KiB
Go
Raw Normal View History

2014-08-01 20:20:28 +00:00
package main
2016-09-01 20:17:37 +00:00
import (
2017-06-04 09:16:55 +00:00
"context"
2017-01-30 09:28:17 +00:00
"fmt"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/index"
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{
Use: "list [blobs|packs|index|snapshots|keys|locks]",
Short: "list objects in the repository",
2016-09-17 10:36:05 +00:00
Long: `
The "list" command allows listing objects in the repository based on type.
2016-09-17 10:36:05 +00:00
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return runList(globalOptions, args)
},
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
}
2016-09-17 10:36:05 +00:00
func runList(opts GlobalOptions, args []string) error {
2014-10-05 12:44:59 +00:00
if len(args) != 1 {
return errors.Fatal("type not specified")
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 {
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] {
case "packs":
2016-09-01 14:04:29 +00:00
t = restic.DataFile
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":
2017-06-04 09:16:55 +00:00
idx, err := index.Load(context.TODO(), repo, nil)
2017-01-30 09:28:17 +00:00
if err != nil {
return err
}
for _, pack := range idx.Packs {
for _, entry := range pack.Entries {
fmt.Printf("%v %v\n", entry.Type, entry.ID)
}
}
return nil
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
2017-06-04 09:16:55 +00:00
for id := range repo.List(context.TODO(), t) {
2016-09-17 10:36:05 +00:00
Printf("%s\n", id)
2015-03-28 10:50:23 +00:00
}
return nil
2014-08-01 20:20:28 +00:00
}