2017-07-08 13:34:23 +00:00
|
|
|
package gs
|
|
|
|
|
|
|
|
import (
|
2023-04-15 08:25:45 +00:00
|
|
|
"os"
|
2017-07-08 13:34:23 +00:00
|
|
|
"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"
|
2023-04-21 19:51:58 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-08 13:34:23 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2023-02-27 06:53:25 +00:00
|
|
|
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
|
|
|
|
Region string `option:"region" help:"region to create the bucket in (default: us)"`
|
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,
|
2023-02-27 06:53:25 +00:00
|
|
|
Region: "us",
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
options.Register("gs", Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the string s and extracts the gcs config. The
|
|
|
|
// supported configuration format is gs:bucketName:/[prefix].
|
2023-04-21 19:35:34 +00:00
|
|
|
func ParseConfig(s string) (*Config, error) {
|
2017-08-05 15:15:59 +00:00
|
|
|
if !strings.HasPrefix(s, "gs:") {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("gs: invalid format")
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
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
|
2022-11-27 17:09:59 +00:00
|
|
|
bucket, prefix, colon := strings.Cut(s, ":")
|
|
|
|
if !colon {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("gs: invalid format: bucket name or path not found")
|
2017-08-05 15:15:59 +00:00
|
|
|
}
|
2017-07-08 13:34:23 +00:00
|
|
|
|
2022-11-27 17:09:59 +00:00
|
|
|
prefix = strings.TrimPrefix(path.Clean(prefix), "/")
|
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
|
2022-11-27 17:09:59 +00:00
|
|
|
cfg.Prefix = prefix
|
2023-04-21 19:35:34 +00:00
|
|
|
return &cfg, nil
|
2017-07-08 13:34:23 +00:00
|
|
|
}
|
2023-04-15 08:25:45 +00:00
|
|
|
|
2023-04-21 19:51:58 +00:00
|
|
|
var _ restic.ApplyEnvironmenter = &Config{}
|
|
|
|
|
2023-04-15 08:25:45 +00:00
|
|
|
// ApplyEnvironment saves values from the environment to the config.
|
2023-04-21 19:51:58 +00:00
|
|
|
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
2023-04-15 08:25:45 +00:00
|
|
|
if cfg.ProjectID == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.ProjectID = os.Getenv(prefix + "GOOGLE_PROJECT_ID")
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|