restic/cmd/restic/cmd_list.go

79 lines
1.3 KiB
Go
Raw Normal View History

2014-08-01 20:20:28 +00:00
package main
import (
2014-10-05 12:44:59 +00:00
"errors"
"fmt"
2014-12-21 16:02:49 +00:00
2014-12-05 20:45:49 +00:00
"github.com/restic/restic/backend"
2014-08-01 20:20:28 +00:00
)
2015-05-10 00:41:16 +00:00
type CmdList struct {
global *GlobalOptions
2015-05-10 00:41:16 +00:00
}
2014-12-07 15:30:52 +00:00
2014-11-30 21:39:58 +00:00
func init() {
2014-12-07 15:30:52 +00:00
_, err := parser.AddCommand("list",
"lists data",
"The list command lists structures or data of a repository",
&CmdList{global: &globalOpts})
2014-12-07 15:30:52 +00:00
if err != nil {
panic(err)
}
2014-11-30 21:39:58 +00:00
}
2014-12-07 15:30:52 +00:00
func (cmd CmdList) Usage() string {
return "[blobs|packs|index|snapshots|keys|locks]"
2014-12-07 15:30:52 +00:00
}
func (cmd CmdList) Execute(args []string) error {
2014-10-05 12:44:59 +00:00
if len(args) != 1 {
2014-12-07 15:30:52 +00:00
return fmt.Errorf("type not specified, Usage: %s", cmd.Usage())
}
repo, err := cmd.global.OpenRepository()
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
lock, err := lockRepo(repo)
defer unlockRepo(lock)
2015-06-27 12:40:18 +00:00
if err != nil {
return err
}
2015-02-17 22:05:23 +00:00
var t backend.Type
2014-10-05 12:44:59 +00:00
switch args[0] {
case "blobs":
err = repo.LoadIndex()
if err != nil {
return err
}
2015-10-12 20:34:12 +00: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 12:44:59 +00:00
case "snapshots":
t = backend.Snapshot
case "keys":
t = backend.Key
case "locks":
t = backend.Lock
default:
return errors.New("invalid type")
}
2014-08-01 20:20:28 +00:00
for id := range repo.List(t, nil) {
2015-06-21 11:25:26 +00:00
cmd.global.Printf("%s\n", id)
2015-03-28 10:50:23 +00:00
}
return nil
2014-08-01 20:20:28 +00:00
}