github.com/mckael/restic@v0.8.3/internal/backend/gs/config.go (about)

     1  package gs
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  
     7  	"github.com/restic/restic/internal/errors"
     8  	"github.com/restic/restic/internal/options"
     9  )
    10  
    11  // Config contains all configuration necessary to connect to a Google Cloud
    12  // Storage bucket.
    13  type Config struct {
    14  	ProjectID   string
    15  	JSONKeyPath string
    16  	Bucket      string
    17  	Prefix      string
    18  
    19  	Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 20)"`
    20  }
    21  
    22  // NewConfig returns a new Config with the default values filled in.
    23  func NewConfig() Config {
    24  	return Config{
    25  		Connections: 5,
    26  	}
    27  }
    28  
    29  func init() {
    30  	options.Register("gs", Config{})
    31  }
    32  
    33  // ParseConfig parses the string s and extracts the gcs config. The
    34  // supported configuration format is gs:bucketName:/[prefix].
    35  func ParseConfig(s string) (interface{}, error) {
    36  	if !strings.HasPrefix(s, "gs:") {
    37  		return nil, errors.New("gs: invalid format")
    38  	}
    39  
    40  	// strip prefix "gs:"
    41  	s = s[3:]
    42  
    43  	// use the first entry of the path as the bucket name and the
    44  	// remainder as prefix
    45  	data := strings.SplitN(s, ":", 2)
    46  	if len(data) < 2 {
    47  		return nil, errors.New("gs: invalid format: bucket name or path not found")
    48  	}
    49  
    50  	bucket, path := data[0], path.Clean(data[1])
    51  
    52  	if strings.HasPrefix(path, "/") {
    53  		path = path[1:]
    54  	}
    55  
    56  	cfg := NewConfig()
    57  	cfg.Bucket = bucket
    58  	cfg.Prefix = path
    59  	return cfg, nil
    60  }