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" // instancePattern keeps every instance under the archie namespace: the name is // used bare as XDG directory, compose project and container prefix, so // "archie-dev" cannot collide with another program's "dev". var instancePattern = regexp.MustCompile(`^archie(-[a-z0-9][a-z0-9-]{0,23})?$`) // 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//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") } // UpdateStateDir is the one resolution of the host's update state directory: // ARCHIE_UPDATE_STATE_DIR when set, else the default. The daemon's // transactions and the dashboard's update snapshot must read the same one. func UpdateStateDir() string { if dir := os.Getenv("ARCHIE_UPDATE_STATE_DIR"); dir != "" { return dir } return DefaultUpdateStateDir() }