2017-07-08 13:34:23 +00:00
|
|
|
package gs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-05 15:15:59 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-08 13:34:23 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
|
|
|
)
|
|
|
|
|
2018-01-12 17:25:02 +00:00
|
|
|
// Config contains all configuration necessary to connect to a Google Cloud Storage
|
|
|
|
// bucket. We use Google's default application credentials to acquire an access token, so
|
|
|
|
// we don't require that calling code supply any authentication material here.
|
2017-07-08 13:34:23 +00:00
|
|
|
type Config struct {
|
2018-01-12 17:25:02 +00:00
|
|
|
ProjectID string
|
|
|
|
Bucket string
|
|
|
|
Prefix string
|
2017-07-08 13:34:23 +00:00
|
|
|
|
2021-05-15 21:08:51 +00:00
|
|
|
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig returns a new Config with the default values filled in.
|
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Connections: 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
options.Register("gs", Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the string s and extracts the gcs config. The
|
|
|
|
// supported configuration format is gs:bucketName:/[prefix].
|
|
|
|
func ParseConfig(s string) (interface{}, error) {
|
2017-08-05 15:15:59 +00:00
|
|
|
if !strings.HasPrefix(s, "gs:") {
|
2017-07-08 13:34:23 +00:00
|
|
|
return nil, errors.New("gs: invalid format")
|
|
|
|
}
|
2017-08-05 15:15:59 +00:00
|
|
|
|
|
|
|
// strip prefix "gs:"
|
|
|
|
s = s[3:]
|
|
|
|
|
2017-07-08 13:34:23 +00:00
|
|
|
// use the first entry of the path as the bucket name and the
|
|
|
|
// remainder as prefix
|
2017-08-05 15:15:59 +00:00
|
|
|
data := strings.SplitN(s, ":", 2)
|
|
|
|
if len(data) < 2 {
|
|
|
|
return nil, errors.New("gs: invalid format: bucket name or path not found")
|
|
|
|
}
|
2017-07-08 13:34:23 +00:00
|
|
|
|
2017-08-05 15:15:59 +00:00
|
|
|
bucket, path := data[0], path.Clean(data[1])
|
|
|
|
|
2019-06-30 20:34:47 +00:00
|
|
|
path = strings.TrimPrefix(path, "/")
|
2017-08-05 15:15:59 +00:00
|
|
|
|
2017-07-08 13:34:23 +00:00
|
|
|
cfg := NewConfig()
|
2017-08-05 15:15:59 +00:00
|
|
|
cfg.Bucket = bucket
|
|
|
|
cfg.Prefix = path
|
2017-07-08 13:34:23 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|