From 6b6f7451a7e96ea16e084191393a63b11b3cef59 Mon Sep 17 00:00:00 2001 From: Christian Kemper Date: Wed, 10 Feb 2016 23:06:10 -0800 Subject: [PATCH] Introduced Scheme constants for the different repository schemes. --- backend/local/config.go | 2 ++ backend/s3/config.go | 2 ++ backend/sftp/config.go | 2 ++ cmd/restic/global.go | 12 ++++++------ location/location.go | 10 +++++----- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backend/local/config.go b/backend/local/config.go index 456f3c428..59a7b1c89 100644 --- a/backend/local/config.go +++ b/backend/local/config.go @@ -5,6 +5,8 @@ import ( "strings" ) +const Scheme = "local" + // ParseConfig parses a local backend config. func ParseConfig(cfg string) (interface{}, error) { if !strings.HasPrefix(cfg, "local:") { diff --git a/backend/s3/config.go b/backend/s3/config.go index c02709ea1..750abd1ca 100644 --- a/backend/s3/config.go +++ b/backend/s3/config.go @@ -16,6 +16,8 @@ type Config struct { Prefix string } +const Scheme = "s3" + const defaultPrefix = "restic" // ParseConfig parses the string s and extracts the s3 config. The two diff --git a/backend/sftp/config.go b/backend/sftp/config.go index ee241facb..7f7eeb5a0 100644 --- a/backend/sftp/config.go +++ b/backend/sftp/config.go @@ -11,6 +11,8 @@ type Config struct { User, Host, Dir string } +const Scheme = "sftp" + // ParseConfig extracts all information for the sftp connection from the string s. func ParseConfig(s string) (interface{}, error) { if strings.HasPrefix(s, "sftp://") { diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 5d9875fce..04ed35544 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -186,13 +186,13 @@ func open(s string) (backend.Backend, error) { } switch loc.Scheme { - case "local": + case local.Scheme: debug.Log("open", "opening local repository at %#v", loc.Config) return local.Open(loc.Config.(string)) - case "sftp": + case sftp.Scheme: debug.Log("open", "opening sftp repository at %#v", loc.Config) return sftp.OpenWithConfig(loc.Config.(sftp.Config)) - case "s3": + case s3.Scheme: cfg := loc.Config.(s3.Config) if cfg.KeyID == "" { cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID") @@ -219,13 +219,13 @@ func create(s string) (backend.Backend, error) { } switch loc.Scheme { - case "local": + case local.Scheme: debug.Log("open", "create local repository at %#v", loc.Config) return local.Create(loc.Config.(string)) - case "sftp": + case sftp.Scheme: debug.Log("open", "create sftp repository at %#v", loc.Config) return sftp.CreateWithConfig(loc.Config.(sftp.Config)) - case "s3": + case s3.Scheme: cfg := loc.Config.(s3.Config) if cfg.KeyID == "" { cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID") diff --git a/location/location.go b/location/location.go index 1f3aba1a4..14cdc97f0 100644 --- a/location/location.go +++ b/location/location.go @@ -24,9 +24,9 @@ type parser struct { // parsers is a list of valid config parsers for the backends. The first parser // is the fallback and should always be set to the local backend. var parsers = []parser{ - {"local", local.ParseConfig}, - {"sftp", sftp.ParseConfig}, - {"s3", s3.ParseConfig}, + {local.Scheme, local.ParseConfig}, + {sftp.Scheme, sftp.ParseConfig}, + {s3.Scheme, s3.ParseConfig}, } // Parse extracts repository location information from the string s. If s @@ -51,8 +51,8 @@ func Parse(s string) (u Location, err error) { } // try again, with the local parser and the prefix "local:" - u.Scheme = "local" - u.Config, err = local.ParseConfig("local:" + s) + u.Scheme = local.Scheme + u.Config, err = local.ParseConfig(local.Scheme + ":" + s) if err != nil { return Location{}, err }