feat(configuration): name the instance with ARCHIE_INSTANCE and scope default paths by it
This commit is contained in:
9 files changed
+75
-26
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// DefaultInstance is the instance name when ARCHIE_INSTANCE is unset. Every
|
||||
// host-global name derived from it is unchanged from a single-instance host.
|
||||
const DefaultInstance = "archie"
|
||||
|
||||
var instancePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,30}$`)
|
||||
|
||||
// InstanceName returns the instance this process belongs to, from
|
||||
// ARCHIE_INSTANCE. It names the config, data, state and cache directories, so
|
||||
// it cannot live in the config file it selects. Loading rejects an invalid
|
||||
// name, so callers past startup can use it as a path segment.
|
||||
func InstanceName() string {
|
||||
if name := os.Getenv("ARCHIE_INSTANCE"); name != "" {
|
||||
return name
|
||||
}
|
||||
return DefaultInstance
|
||||
}
|
||||
|
||||
func validateInstanceName(name string) error {
|
||||
if !instancePattern.MatchString(name) {
|
||||
return fmt.Errorf("ARCHIE_INSTANCE %q: must match %s", name, instancePattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultCacheDir returns the instance's user cache directory.
|
||||
func DefaultCacheDir() (string, error) {
|
||||
cache, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(cache, InstanceName()), nil
|
||||
}
|
||||
|
||||
// DefaultUpdateStateDir returns $XDG_STATE_HOME/<instance>/update.
|
||||
func DefaultUpdateStateDir() string {
|
||||
dir := os.Getenv("XDG_STATE_HOME")
|
||||
if dir == "" {
|
||||
home, _ := os.UserHomeDir()
|
||||
dir = filepath.Join(home, ".local", "state")
|
||||
}
|
||||
return filepath.Join(dir, InstanceName(), "update")
|
||||
}
|
||||
Reference in new issue
Block a user