restic/src/cmds/restic/cmd_list.go

66 lines
1.1 KiB
Go
Raw Normal View History

2014-08-01 22:20:28 +02:00
package main
2016-09-01 22:17:37 +02:00
import (
"restic"
"restic/errors"
)
2014-08-01 22:20:28 +02:00
2015-05-10 02:41:16 +02:00
type CmdList struct {
global *GlobalOptions
2015-05-10 02:41:16 +02:00
}
2014-12-07 16:30:52 +01:00
2014-11-30 22:39:58 +01:00
func init() {
2014-12-07 16:30:52 +01:00
_, err := parser.AddCommand("list",
"lists data",
"The list command lists structures or data of a repository",
&CmdList{global: &globalOpts})
2014-12-07 16:30:52 +01:00
if err != nil {
panic(err)
}
2014-11-30 22:39:58 +01:00
}
2014-12-07 16:30:52 +01:00
func (cmd CmdList) Usage() string {
return "[blobs|packs|index|snapshots|keys|locks]"
2014-12-07 16:30:52 +01:00
}
func (cmd CmdList) Execute(args []string) error {
2014-10-05 14:44:59 +02:00
if len(args) != 1 {
2016-09-01 22:17:37 +02:00
return errors.Fatalf("type not specified, Usage: %s", cmd.Usage())
2014-12-07 16:30:52 +01:00
}
repo, err := cmd.global.OpenRepository()
2014-12-07 16:30:52 +01:00
if err != nil {
return err
2014-10-05 14:44:59 +02:00
}
2014-08-01 22:20:28 +02:00
2015-11-13 12:33:59 +01:00
if !cmd.global.NoLock {
lock, err := lockRepo(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
2015-06-27 14:40:18 +02:00
}
2016-09-01 16:04:29 +02:00
var t restic.FileType
2014-10-05 14:44:59 +02:00
switch args[0] {
case "packs":
2016-09-01 16:04:29 +02:00
t = restic.DataFile
case "index":
2016-09-01 16:04:29 +02:00
t = restic.IndexFile
2014-10-05 14:44:59 +02:00
case "snapshots":
2016-09-01 16:04:29 +02:00
t = restic.SnapshotFile
2014-10-05 14:44:59 +02:00
case "keys":
2016-09-01 16:04:29 +02:00
t = restic.KeyFile
2014-10-05 14:44:59 +02:00
case "locks":
2016-09-01 16:04:29 +02:00
t = restic.LockFile
2014-10-05 14:44:59 +02:00
default:
2016-09-01 22:17:37 +02:00
return errors.Fatal("invalid type")
2014-10-05 14:44:59 +02:00
}
2014-08-01 22:20:28 +02:00
for id := range repo.List(t, nil) {
2015-06-21 13:25:26 +02:00
cmd.global.Printf("%s\n", id)
2015-03-28 11:50:23 +01:00
}
return nil
2014-08-01 22:20:28 +02:00
}