feat: support custom --config path in CLI and GUI

Add config.LoadFrom/SaveTo for arbitrary paths and thread a --config
flag through both entry points:
- CLI: --config loads a file first; explicit flags override file values
- GUI: --config selects the settings file used for load and save
- tests for LoadFrom/SaveTo and default fallback
This commit is contained in:
2026-09-19 08:35:07 +01:00
parent b9a539e133
commit 34efbaf26d
5 changed files with 128 additions and 27 deletions
+17 -8
View File
@@ -71,15 +71,20 @@ var pathVar = func() string {
return filepath.Join(dir, "teleportfling", "config.json")
}()
// Path returns the config file location.
// Path returns the default config file location.
func Path() string {
return pathVar
}
// Load reads the config file, returning Default when it does not exist.
// Load reads the default config file, returning Default when it does not exist.
func Load() (Config, error) {
p := Path()
data, err := os.ReadFile(p)
return LoadFrom(Path())
}
// LoadFrom reads the config file at path, returning Default when it does not
// exist. This lets the CLI and GUI support custom --config paths.
func LoadFrom(path string) (Config, error) {
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return Default(), nil
@@ -113,15 +118,19 @@ func Load() (Config, error) {
return c, nil
}
// Save writes the config file, creating the directory if needed.
// Save writes the default config file, creating the directory if needed.
func Save(c Config) error {
p := Path()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return SaveTo(Path(), c)
}
// SaveTo writes the config file at path, creating the directory if needed.
func SaveTo(path string, c Config) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return os.WriteFile(p, data, 0o600)
return os.WriteFile(path, data, 0o600)
}