restic/cmd/restic/cmd_cat.go

143 lines
2.4 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"
2015-04-26 12:46:15 +00:00
"github.com/restic/restic/server"
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 {
return "[data|tree|snapshot|key|masterkey|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 {
if len(args) < 1 || (args[0] != "masterkey" && 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]
var id backend.ID
if tpe != "masterkey" {
id, err = backend.ParseID(args[1])
if err != nil {
id = nil
if tpe != "snapshot" {
return err
}
// find snapshot id with prefix
2015-03-28 10:50:23 +00:00
name, err := s.FindSnapshot(args[1])
if err != nil {
return err
}
id, err = backend.ParseID(name)
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":
rd, err := s.Backend().Get(backend.Key, id.String())
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2015-03-28 10:50:23 +00:00
dec := json.NewDecoder(rd)
2015-04-26 12:46:15 +00:00
var key server.Key
2015-03-28 10:50:23 +00:00
err = dec.Decode(&key)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&key, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "masterkey":
buf, err := json.MarshalIndent(s.Key().Master(), "", " ")
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
fmt.Println(string(buf))
2014-10-05 12:44:59 +00:00
return nil
case "lock":
return errors.New("not yet implemented")
default:
return errors.New("invalid type")
}
}