Files
funknetz/config_path.go
konradandCursor 87e366e36c Keep a later config flag when extra words are present.
A non-flag word no longer stops parsing, and a missing home directory refuses to create or rewrite a config instead of writing an empty log path.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-07 12:42:01 +02:00

52 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
)
func defaultConfigPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".funknetz.json"), nil
}
// resolveConfigPath picks the config file from -c / --config in args (no program name).
// The default is ~/.funknetz.json. A relative path is resolved against the process CWD.
func resolveConfigPath(args []string) (string, error) {
fs := flag.NewFlagSet("funknetz", flag.ContinueOnError)
fs.SetOutput(io.Discard)
var config string
fs.StringVar(&config, "config", "", "path to the station config file")
fs.StringVar(&config, "c", "", "path to the station config file")
rest := args
for {
if err := fs.Parse(rest); err != nil {
return "", err
}
rest = fs.Args()
if len(rest) == 0 {
break
}
// A non-flag word ends a parse. Skip it and keep later flags.
rest = rest[1:]
}
if config == "" {
path, err := defaultConfigPath()
if err != nil {
return "", fmt.Errorf("config path: %w", err)
}
return path, nil
}
abs, err := filepath.Abs(config)
if err != nil {
return "", fmt.Errorf("config path: %w", err)
}
return abs, nil
}