56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package config
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"path/filepath"
|
||
|
|
"runtime"
|
||
|
|
)
|
||
|
|
|
||
|
|
// fileName ist der Name der Config-Datei im Config-Verzeichnis.
|
||
|
|
const fileName = "config.toml"
|
||
|
|
|
||
|
|
// ErrNoHome meldet, dass das Heimatverzeichnis nicht bestimmt werden kann
|
||
|
|
// (keine HOME- bzw. APPDATA-Umgebungsvariable).
|
||
|
|
var ErrNoHome = errors.New("Config-Verzeichnis nicht bestimmbar (HOME bzw. APPDATA fehlt)")
|
||
|
|
|
||
|
|
// Dir liefert das platform-native Config-Verzeichnis von talk auf dem
|
||
|
|
// aktuellen System: Linux ~/.config/talk (respektive $XDG_CONFIG_HOME/talk),
|
||
|
|
// macOS ~/Library/Application Support/talk, Windows %APPDATA%\talk.
|
||
|
|
func Dir(getenv func(string) string) (string, error) {
|
||
|
|
return dirFor(runtime.GOOS, getenv)
|
||
|
|
}
|
||
|
|
|
||
|
|
// dirFor ist die testbare Plattformlogik hinter Dir: goos wird als
|
||
|
|
// Parameter übergeben, damit die Pfade für alle Betriebssysteme vom
|
||
|
|
// Dev-Host aus tischgesteuert geprüft werden können.
|
||
|
|
//
|
||
|
|
// XDG_CONFIG_HOME wird nur berücksichtigt, wenn es einen absoluten Pfad
|
||
|
|
// enthält (XDG-Spezifikation); relative Werte werden ignoriert.
|
||
|
|
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
|
||
|
|
default: // linux und sonstige unixartige Systeme
|
||
|
|
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
|
||
|
|
}
|
||
|
|
}
|