feat(config): geschichtete Konfigurationsauflösung mit injizierbaren Pfaden
Defaults → optionale config.toml → Env-Overrides (TALK_AUDIO_SINK); Validierungsfehler sind klar, nie still. Plattform-Pfadlogik (linux/darwin/windows, XDG_CONFIG_HOME, HOME, APPDATA) mit goos als Parameter — alle Plattformen tischgesteuert vom Dev-Host testbar. Erste externe Dependency: github.com/BurntSushi/toml v1.6.0 (MIT, pure Go).
This commit is contained in:
1 parent
de1ef64b8f
commit
36ad1dfa4e
6 files changed
+363
-3
No files matched your search
@@ -0,0 +1,56 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user