forked from restic/restic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Google Cloud Storage repositories using the existin…
…g s3 support to address restic#211. Added a new package to handle GCS configuration. GCS repositories get specified using gs://bucketname/prefix syntax, similar to gsutil. Added tests for the various cases to config_test and location_test.
- Loading branch information
Showing
5 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gcs | ||
|
||
import ( | ||
"errors" | ||
"path" | ||
"restic/backend/s3" | ||
"strings" | ||
) | ||
|
||
// The endpoint for all GCS operations. | ||
const gcsEndpoint = "storage.googleapis.com" | ||
|
||
const defaultPrefix = "restic" | ||
|
||
// ParseConfig parses the string s and extracts the gcs config. The two | ||
// supported configuration formats are gcs://bucketname/prefix and | ||
// gcs:bucketname/prefix. | ||
func ParseConfig(s string) (interface{}, error) { | ||
switch { | ||
case strings.HasPrefix(s, "gs://"): | ||
s = s[5:] | ||
case strings.HasPrefix(s, "gs:"): | ||
s = s[3:] | ||
default: | ||
return nil, errors.New(`gcs: config does not start with "gs"`) | ||
} | ||
p := strings.SplitN(s, "/", 2) | ||
var prefix string | ||
switch { | ||
case len(p) < 1: | ||
return nil, errors.New("gcs: invalid format: bucket name not found") | ||
case len(p) == 1 || p[1] == "": | ||
prefix = defaultPrefix | ||
default: | ||
prefix = path.Clean(p[1]) | ||
} | ||
return s3.Config{ | ||
Endpoint: gcsEndpoint, | ||
UseHTTP: false, | ||
Bucket: p[0], | ||
Prefix: prefix, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package gcs | ||
|
||
import ( | ||
"restic/backend/s3" | ||
"testing" | ||
) | ||
|
||
var configTests = []struct { | ||
s string | ||
cfg s3.Config | ||
}{ | ||
{"gs://bucketname", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "restic", | ||
}}, | ||
{"gs://bucketname/", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "restic", | ||
}}, | ||
{"gs://bucketname/prefix/dir", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "prefix/dir", | ||
}}, | ||
{"gs://bucketname/prefix/dir/", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "prefix/dir", | ||
}}, | ||
{"gs:bucketname", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "restic", | ||
}}, | ||
{"gs:bucketname/", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "restic", | ||
}}, | ||
{"gs:bucketname/prefix/dir", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "prefix/dir", | ||
}}, | ||
{"gs:bucketname/prefix/dir/", s3.Config{ | ||
Endpoint: "storage.googleapis.com", | ||
Bucket: "bucketname", | ||
Prefix: "prefix/dir", | ||
}}, | ||
} | ||
|
||
func TestParseConfig(t *testing.T) { | ||
for i, test := range configTests { | ||
cfg, err := ParseConfig(test.s) | ||
if err != nil { | ||
t.Errorf("test %d:%s failed: %v", i, test.s, err) | ||
continue | ||
} | ||
|
||
if cfg != test.cfg { | ||
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", | ||
i, test.s, test.cfg, cfg) | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters