github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/prefixer/prefixer.go (about)

     1  package prefixer
     2  
     3  // Prefixer interface describes a handle for a specific instance by its domain
     4  // and a specific and unique prefix.
     5  type Prefixer interface {
     6  	DBCluster() int
     7  	DBPrefix() string
     8  	DomainName() string
     9  }
    10  
    11  type prefixer struct {
    12  	cluster int
    13  	domain  string
    14  	prefix  string
    15  }
    16  
    17  // UnknownDomainName represents the human-readable string of an empty domain
    18  // name of a prefixer struct
    19  const UnknownDomainName string = "<unknown>"
    20  
    21  func (p *prefixer) DBCluster() int { return p.cluster }
    22  
    23  func (p *prefixer) DBPrefix() string { return p.prefix }
    24  
    25  func (p *prefixer) DomainName() string {
    26  	if p.domain == "" {
    27  		return UnknownDomainName
    28  	}
    29  	return p.domain
    30  }
    31  
    32  // NewPrefixer returns a prefixer with the specified domain and prefix values.
    33  func NewPrefixer(cluster int, domain, prefix string) Prefixer {
    34  	return &prefixer{
    35  		cluster: cluster,
    36  		domain:  domain,
    37  		prefix:  prefix,
    38  	}
    39  }
    40  
    41  // GlobalCouchCluster is the index of the CouchDB cluster for the global and
    42  // secrets databases.
    43  const GlobalCouchCluster = -1
    44  
    45  // GlobalPrefixer returns a global prefixer with the wildcard '*' as prefix.
    46  var GlobalPrefixer = NewPrefixer(GlobalCouchCluster, "", "global")
    47  
    48  // SecretsPrefixer is the the prefix used for db which hold
    49  // a cozy stack secrets.
    50  var SecretsPrefixer = NewPrefixer(GlobalCouchCluster, "", "secrets")