Introduce type Server

This commit is contained in:
Alexander Neumann
2014-12-21 17:37:29 +01:00
parent 661c1e9aa1
commit cc147c002e
15 changed files with 206 additions and 106 deletions
+1 -1
View File
@@ -110,7 +110,7 @@ func (c CmdFind) findInTree(ch *restic.ContentHandler, id backend.ID, path strin
return results, nil
}
func (c CmdFind) findInSnapshot(be backend.Server, key *restic.Key, id backend.ID) error {
func (c CmdFind) findInSnapshot(be restic.Server, key *restic.Key, id backend.ID) error {
debug("searching in snapshot %s\n for entries within [%s %s]", id, c.oldest, c.newest)
ch, err := restic.NewContentHandler(be, key)
+1 -1
View File
@@ -75,7 +75,7 @@ func fsckTree(ch *restic.ContentHandler, id backend.ID) error {
return nil
}
func fsck_snapshot(be backend.Server, key *restic.Key, id backend.ID) error {
func fsck_snapshot(be restic.Server, key *restic.Key, id backend.ID) error {
debug("checking snapshot %v\n", id)
ch, err := restic.NewContentHandler(be, key)
+4 -4
View File
@@ -22,7 +22,7 @@ func init() {
}
}
func list_keys(be backend.Server, key *restic.Key) error {
func list_keys(be restic.Server, key *restic.Key) error {
tab := NewTable()
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
tab.RowFormat = "%s%-10s %-10s %-10s %s"
@@ -54,7 +54,7 @@ func list_keys(be backend.Server, key *restic.Key) error {
return nil
}
func add_key(be backend.Server, key *restic.Key) error {
func add_key(be restic.Server, key *restic.Key) error {
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
@@ -72,7 +72,7 @@ func add_key(be backend.Server, key *restic.Key) error {
return nil
}
func delete_key(be backend.Server, key *restic.Key, id backend.ID) error {
func delete_key(be restic.Server, key *restic.Key, id backend.ID) error {
if id.Equal(key.ID()) {
return errors.New("refusing to remove key currently used to access repository")
}
@@ -86,7 +86,7 @@ func delete_key(be backend.Server, key *restic.Key, id backend.ID) error {
return nil
}
func change_password(be backend.Server, key *restic.Key) error {
func change_password(be restic.Server, key *restic.Key) error {
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
+6 -5
View File
@@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"github.com/restic/restic/backend"
)
@@ -27,22 +28,22 @@ func (cmd CmdList) Execute(args []string) error {
return fmt.Errorf("type not specified, Usage: %s", cmd.Usage())
}
be, key, err := OpenRepo()
be, _, err := OpenRepo()
if err != nil {
return err
}
var (
t backend.Type
each func(backend.Server, backend.Type, func(backend.ID, []byte, error)) error = backend.Each
each func(backend.Type, func(backend.ID, []byte, error)) error = be.Each
)
switch args[0] {
case "data":
t = backend.Data
each = key.Each
each = be.EachDecrypted
case "trees":
t = backend.Tree
each = key.Each
each = be.EachDecrypted
case "snapshots":
t = backend.Snapshot
case "maps":
@@ -55,7 +56,7 @@ func (cmd CmdList) Execute(args []string) error {
return errors.New("invalid type")
}
return each(be, t, func(id backend.ID, data []byte, err error) {
return each(t, func(id backend.ID, data []byte, err error) {
if t == backend.Data || t == backend.Tree {
fmt.Printf("%s %s\n", id, backend.Hash(data))
} else {
+1 -1
View File
@@ -63,7 +63,7 @@ func (cmd CmdLs) Usage() string {
return "ls snapshot-ID [DIR]"
}
func (cmd CmdLs) Execute(be backend.Server, key *restic.Key, args []string) error {
func (cmd CmdLs) Execute(be restic.Server, key *restic.Key, args []string) error {
if len(args) < 1 || len(args) > 2 {
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
}
+13 -9
View File
@@ -70,13 +70,15 @@ func (cmd CmdInit) Execute(args []string) error {
os.Exit(1)
}
_, err = restic.CreateKey(be, pw)
s := restic.NewServer(be)
_, err = restic.CreateKey(s, pw)
if err != nil {
fmt.Fprintf(os.Stderr, "creating key in backend at %s failed: %v\n", opts.Repo, err)
os.Exit(1)
}
fmt.Printf("created restic backend at %s\n", be.Location())
fmt.Printf("created restic backend at %s\n", opts.Repo)
return nil
}
@@ -86,7 +88,7 @@ func (cmd CmdInit) Execute(args []string) error {
// * /foo/bar -> local repository at /foo/bar
// * sftp://user@host/foo/bar -> remote sftp repository on host for user at path foo/bar
// * sftp://host//tmp/backup -> remote sftp repository on host at path /tmp/backup
func open(u string) (backend.Server, error) {
func open(u string) (backend.Backend, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
@@ -107,7 +109,7 @@ func open(u string) (backend.Server, error) {
}
// Create the backend specified by URI.
func create(u string) (backend.Server, error) {
func create(u string) (backend.Backend, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
@@ -127,18 +129,20 @@ func create(u string) (backend.Server, error) {
return backend.CreateSFTP(url.Path[1:], "ssh", args...)
}
func OpenRepo() (backend.Server, *restic.Key, error) {
func OpenRepo() (restic.Server, *restic.Key, error) {
be, err := open(opts.Repo)
if err != nil {
return nil, nil, err
return restic.Server{}, nil, err
}
key, err := restic.SearchKey(be, readPassword("RESTIC_PASSWORD", "Enter Password for Repository: "))
s := restic.NewServer(be)
key, err := restic.SearchKey(s, readPassword("RESTIC_PASSWORD", "Enter Password for Repository: "))
if err != nil {
return nil, nil, fmt.Errorf("unable to open repo: %v", err)
return restic.Server{}, nil, fmt.Errorf("unable to open repo: %v", err)
}
return be, key, nil
return s, key, nil
}
func init() {