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
+25 -8
View File
@@ -43,6 +43,9 @@ type App struct {
win fyne.Window
desk desktop.App
// configPath overrides the default config location ("" = default).
configPath string
cfg config.Config
eng *flinger.Engine
lock bool // serialises start/stop against UI actions
@@ -63,9 +66,10 @@ type App struct {
statsDone chan struct{}
}
// Run starts the GUI and blocks until the app exits.
func Run() {
g := &App{}
// Run starts the GUI and blocks until the app exits. configPath selects a
// non-default settings file ("" uses the default location).
func Run(configPath string) {
g := &App{configPath: configPath}
g.fyneApp = app.NewWithID(appID)
g.win = g.fyneApp.NewWindow("TeleportFling")
@@ -74,7 +78,7 @@ func Run() {
g.iconActive = newTrayResource(color.NRGBA{R: 46, G: 125, B: 50, A: 255}, color.NRGBA{R: 150, G: 220, B: 140, A: 255})
// Load persisted settings (falling back to defaults).
g.cfg = mustLoadConfig()
g.cfg = g.mustLoadConfig()
g.buildUI()
@@ -89,9 +93,18 @@ func Run() {
g.win.ShowAndRun()
}
// mustLoadConfig loads the config, logging and falling back to defaults.
func mustLoadConfig() config.Config {
c, err := config.Load()
// mustLoadConfig loads the config (from the configured path, or the default),
// logging and falling back to defaults on error.
func (g *App) mustLoadConfig() config.Config {
var (
c config.Config
err error
)
if g.configPath != "" {
c, err = config.LoadFrom(g.configPath)
} else {
c, err = config.Load()
}
if err != nil {
log.Printf("gui: config load: %v (using defaults)", err)
return config.Default()
@@ -201,7 +214,11 @@ func (g *App) start() {
StreamIndex: g.cfg.StreamIndex,
Announce: &announce,
}
if err := config.Save(g.cfg); err != nil {
if g.configPath != "" {
if err := config.SaveTo(g.configPath, g.cfg); err != nil {
log.Printf("gui: config save: %v", err)
}
} else if err := config.Save(g.cfg); err != nil {
log.Printf("gui: config save: %v", err)
}