2026-10-18 15:38:56 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-24 11:15:35 +02:00
|
|
|
// fileName is the config file's name inside the config directory.
|
2026-10-18 15:38:56 +02:00
|
|
|
const fileName = "config.toml"
|
|
|
|
|
|
2026-10-24 11:15:35 +02:00
|
|
|
// ErrNoHome reports that the home directory cannot be determined (no HOME or
|
|
|
|
|
// APPDATA environment variable).
|
|
|
|
|
var ErrNoHome = errors.New("config directory unknown (HOME or APPDATA not set)")
|
2026-10-18 15:38:56 +02:00
|
|
|
|
2026-10-24 11:15:35 +02:00
|
|
|
// Dir returns talk's platform-native config directory: ~/.config/talk on
|
|
|
|
|
// Linux (or $XDG_CONFIG_HOME/talk), ~/Library/Application Support/talk on
|
|
|
|
|
// macOS, %APPDATA%\talk on Windows.
|
2026-10-18 15:38:56 +02:00
|
|
|
func Dir(getenv func(string) string) (string, error) {
|
|
|
|
|
return dirFor(runtime.GOOS, getenv)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 11:15:35 +02:00
|
|
|
// dirFor is the testable platform logic behind Dir: goos is a parameter so
|
|
|
|
|
// all platforms can be table-tested from one host.
|
2026-10-18 15:38:56 +02:00
|
|
|
//
|
2026-10-24 11:15:35 +02:00
|
|
|
// XDG_CONFIG_HOME is honoured only when it holds an absolute path (XDG
|
|
|
|
|
// spec); relative values are ignored.
|
2026-10-18 15:38:56 +02:00
|
|
|
func dirFor(goos string, getenv func(string) string) (string, error) {
|
|
|
|
|
if getenv == nil {
|
|
|
|
|
return "", ErrNoHome
|
|
|
|
|
}
|
|
|
|
|
switch goos {
|
|
|
|
|
case "windows":
|
|
|
|
|
appdata := getenv("APPDATA")
|
|
|
|
|
if appdata == "" {
|
|
|
|
|
return "", ErrNoHome
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(appdata, "talk"), nil
|
|
|
|
|
case "darwin":
|
|
|
|
|
home := getenv("HOME")
|
|
|
|
|
if home == "" {
|
|
|
|
|
return "", ErrNoHome
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(home, "Library", "Application Support", "talk"), nil
|
2026-10-24 11:15:35 +02:00
|
|
|
default: // linux and other unix-likes
|
2026-10-18 15:38:56 +02:00
|
|
|
if xdg := getenv("XDG_CONFIG_HOME"); xdg != "" && filepath.IsAbs(xdg) {
|
|
|
|
|
return filepath.Join(xdg, "talk"), nil
|
|
|
|
|
}
|
|
|
|
|
home := getenv("HOME")
|
|
|
|
|
if home == "" {
|
|
|
|
|
return "", ErrNoHome
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(home, ".config", "talk"), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-24 10:08:44 +02:00
|
|
|
|
2026-10-24 11:15:35 +02:00
|
|
|
// Path returns the full path of config.toml in the config directory
|
|
|
|
|
// (diagnostics, setup).
|
2026-10-24 10:08:44 +02:00
|
|
|
func Path(getenv func(string) string) (string, error) {
|
|
|
|
|
dir, err := Dir(getenv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(dir, fileName), nil
|
|
|
|
|
}
|