restic/src/cmds/restic/cmd_list.go

79 lines
1.3 KiB
Go
Raw Normal View History

2014-08-01 22:20:28 +02:00
package main
import (
"restic"
"restic/backend"
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 {
return restic.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
}
2015-02-17 23:05:23 +01:00
var t backend.Type
2014-10-05 14:44:59 +02:00
switch args[0] {
case "blobs":
err = repo.LoadIndex()
if err != nil {
return err
}
2015-10-12 22:34:12 +02:00
for _, idx := range repo.Index().All() {
for blob := range idx.Each(nil) {
cmd.global.Printf("%s\n", blob.ID)
}
}
return nil
case "packs":
t = backend.Data
case "index":
t = backend.Index
2014-10-05 14:44:59 +02:00
case "snapshots":
t = backend.Snapshot
case "keys":
t = backend.Key
case "locks":
t = backend.Lock
default:
return restic.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
}