2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00

use new backend open with config

This commit is contained in:
Alexander Neumann 2015-12-28 18:23:20 +01:00
parent 2b0b44c5ce
commit 7d5f8214cf

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/url"
"os" "os"
"strings" "strings"
@ -13,6 +12,7 @@ import (
"github.com/restic/restic/backend/local" "github.com/restic/restic/backend/local"
"github.com/restic/restic/backend/s3" "github.com/restic/restic/backend/s3"
"github.com/restic/restic/backend/sftp" "github.com/restic/restic/backend/sftp"
"github.com/restic/restic/location"
"github.com/restic/restic/repository" "github.com/restic/restic/repository"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -164,78 +164,58 @@ func (o GlobalOptions) OpenRepository() (*repository.Repository, error) {
return s, nil return s, nil
} }
// Open the backend specified by URI. // Open the backend specified by a location config.
// Valid formats are: func open(s string) (backend.Backend, error) {
// * /foo/bar -> local repository at /foo/bar loc, err := location.Parse(s)
// * s3://region/bucket -> amazon s3 bucket
// * 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
// * c:\temp -> local repository at c:\temp - the path must exist
func open(u string) (backend.Backend, error) {
// check if the url is a directory that exists
fi, err := os.Stat(u)
if err == nil && fi.IsDir() {
return local.Open(u)
}
url, err := url.Parse(u)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if url.Scheme == "" { switch loc.Scheme {
return local.Open(url.Path) case "local":
return local.Open(loc.Config.(string))
case "sftp":
return sftp.OpenWithConfig(loc.Config.(sftp.Config))
case "s3":
cfg := loc.Config.(s3.Config)
if cfg.KeyID == "" {
cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID")
}
if cfg.Secret == "" {
cfg.Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
}
return s3.Open(loc.Config.(s3.Config))
} }
if len(url.Path) < 1 { return nil, fmt.Errorf("invalid scheme %q", loc.Scheme)
return nil, fmt.Errorf("unable to parse url %v", url)
}
if url.Scheme == "s3" {
return s3.Open(url.Host, url.Path[1:])
}
args := []string{url.Host}
if url.User != nil && url.User.Username() != "" {
args = append(args, "-l")
args = append(args, url.User.Username())
}
args = append(args, "-s")
args = append(args, "sftp")
return sftp.Open(url.Path[1:], "ssh", args...)
} }
// Create the backend specified by URI. // Create the backend specified by URI.
func create(u string) (backend.Backend, error) { func create(s string) (backend.Backend, error) {
// check if the url is a directory that exists loc, err := location.Parse(s)
fi, err := os.Stat(u)
if err == nil && fi.IsDir() {
return local.Create(u)
}
url, err := url.Parse(u)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if url.Scheme == "" { switch loc.Scheme {
return local.Create(url.Path) case "local":
return local.Create(loc.Config.(string))
case "sftp":
return sftp.CreateWithConfig(loc.Config.(sftp.Config))
case "s3":
cfg := loc.Config.(s3.Config)
if cfg.KeyID == "" {
cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID")
}
if cfg.Secret == "" {
cfg.Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
}
return s3.Open(loc.Config.(s3.Config))
} }
if len(url.Path) < 1 { return nil, fmt.Errorf("invalid scheme %q", loc.Scheme)
return nil, fmt.Errorf("unable to parse url %v", url)
}
if url.Scheme == "s3" {
return s3.Open(url.Host, url.Path[1:])
}
args := []string{url.Host}
if url.User != nil && url.User.Username() != "" {
args = append(args, "-l")
args = append(args, url.User.Username())
}
args = append(args, "-s")
args = append(args, "sftp")
return sftp.Create(url.Path[1:], "ssh", args...)
} }