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

     1  package azure
     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 an azure compatible
    12  // server.
    13  type Config struct {
    14  	AccountName string
    15  	AccountKey  string
    16  	Container   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("azure", Config{})
    31  }
    32  
    33  // ParseConfig parses the string s and extracts the azure config. The
    34  // configuration format is azure:containerName:/[prefix].
    35  func ParseConfig(s string) (interface{}, error) {
    36  	if !strings.HasPrefix(s, "azure:") {
    37  		return nil, errors.New("azure: invalid format")
    38  	}
    39  
    40  	// strip prefix "azure:"
    41  	s = s[6:]
    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("azure: invalid format: bucket name or path not found")
    48  	}
    49  	container, path := data[0], path.Clean(data[1])
    50  	if strings.HasPrefix(path, "/") {
    51  		path = path[1:]
    52  	}
    53  	cfg := NewConfig()
    54  	cfg.Container = container
    55  	cfg.Prefix = path
    56  	return cfg, nil
    57  }