restic/cmd/restic/cmd_cat.go

125 lines
2.0 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
2014-10-05 12:44:59 +00:00
)
2014-12-07 15:30:52 +00:00
type CmdCat struct{}
2014-11-30 21:39:58 +00:00
func init() {
2014-12-07 15:30:52 +00:00
_, err := parser.AddCommand("cat",
"dump something",
"The cat command dumps data structures or data from a repository",
&CmdCat{})
if err != nil {
panic(err)
}
}
func (cmd CmdCat) Usage() string {
2015-03-22 14:42:46 +00:00
return "[data|tree|snapshot|key|lock] ID"
2014-11-30 21:39:58 +00:00
}
2014-12-07 15:30:52 +00:00
func (cmd CmdCat) Execute(args []string) error {
2014-10-05 12:44:59 +00:00
if len(args) != 2 {
2014-12-07 15:30:52 +00:00
return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage())
}
2014-12-21 17:10:19 +00:00
s, err := OpenRepo()
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-10-05 12:44:59 +00:00
}
tpe := args[0]
id, err := backend.ParseID(args[1])
if err != nil {
id = nil
if tpe != "snapshot" {
return err
}
// find snapshot id with prefix
2014-12-21 17:10:19 +00:00
id, err = s.FindSnapshot(args[1])
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
}
switch tpe {
2015-03-22 14:42:46 +00:00
case "data":
// try storage id
data, err := s.LoadID(backend.Data, id)
2014-10-05 12:44:59 +00:00
if err == nil {
_, err = os.Stdout.Write(data)
return err
}
_, err = os.Stdout.Write(data)
2014-10-05 12:44:59 +00:00
return err
case "tree":
// try storage id
tree := &restic.Tree{}
err := s.LoadJSONID(backend.Tree, id, tree)
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
buf, err := json.MarshalIndent(&tree, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "snapshot":
sn := &restic.Snapshot{}
err = s.LoadJSONID(backend.Snapshot, id, sn)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&sn, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "key":
2014-12-21 17:10:19 +00:00
data, err := s.Get(backend.Key, id)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2014-12-05 20:45:49 +00:00
var key restic.Key
2014-10-05 12:44:59 +00:00
err = json.Unmarshal(data, &key)
if err != nil {
return err
}
buf, err := json.MarshalIndent(&key, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "lock":
return errors.New("not yet implemented")
default:
return errors.New("invalid type")
}
}