restic/cmd/restic/cmd_cat.go

178 lines
3.2 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
2014-10-05 12:44:59 +00:00
"os"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
"github.com/restic/restic/pack"
"github.com/restic/restic/repository"
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-05-03 15:37:21 +00:00
return "[pack|blob|tree|snapshot|key|masterkey|config|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 {
2015-05-03 15:37:21 +00:00
if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && 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
2015-05-03 15:37:21 +00:00
if tpe != "masterkey" && tpe != "config" {
id, err = backend.ParseID(args[1])
if err != nil {
id = nil
if tpe != "snapshot" {
return err
}
// find snapshot id with prefix
id, err = restic.FindSnapshot(s, args[1])
if err != nil {
return err
}
}
2014-10-05 12:44:59 +00:00
}
// handle all types that don't need an index
2014-10-05 12:44:59 +00:00
switch tpe {
2015-05-03 15:37:21 +00:00
case "config":
buf, err := json.MarshalIndent(s.Config, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "index":
buf, err := s.Load(backend.Index, id)
if err != nil {
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return err
2014-10-05 12:44:59 +00:00
case "snapshot":
sn := &restic.Snapshot{}
err = s.LoadJSONUnpacked(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)
var key repository.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(), "", " ")
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")
}
// load index, handle all the other types
err = s.LoadIndex()
if err != nil {
return err
}
switch tpe {
case "pack":
rd, err := s.Backend().Get(backend.Data, id.String())
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, rd)
return err
case "blob":
data, err := s.LoadBlob(pack.Data, id)
if err == nil {
_, err = os.Stdout.Write(data)
return err
}
_, err = os.Stdout.Write(data)
return err
case "tree":
debug.Log("cat", "cat tree %v", id.Str())
tree := restic.NewTree()
err = s.LoadJSONPack(pack.Tree, id, tree)
if err != nil {
debug.Log("cat", "unable to load tree %v: %v", id.Str(), err)
return err
}
buf, err := json.MarshalIndent(&tree, "", " ")
if err != nil {
debug.Log("cat", "error json.MarshalIndent(): %v", err)
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return nil
2014-10-05 12:44:59 +00:00
default:
return errors.New("invalid type")
}
}