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>
This commit is contained in:
2026-10-07 12:42:01 +02:00
co-authored by Cursor
parent 465a26911e
commit 87e366e36c
6 changed files with 317 additions and 14 deletions
+24 -10
View File
@@ -62,7 +62,11 @@ func loadConfig(path string) (loaded, error) {
}
func writeTemplate(path string) error {
body, err := jsonv2.Marshal(templateFile{LogFile: defaultLogPath()})
logFile, err := defaultLogPath()
if err != nil {
return err
}
body, err := jsonv2.Marshal(templateFile{LogFile: logFile})
if err != nil {
return err
}
@@ -99,7 +103,11 @@ func parseConfig(path string, body []byte) (loaded, error) {
if !operationalKindsOK(shape) {
return parseOperational(shape)
}
shape = withLogDefaults(shape)
var err error
shape, err = withLogDefaults(shape)
if err != nil {
return loaded{}, err
}
if err := rewriteConfig(path, shape); err != nil {
return loaded{}, fmt.Errorf("the file could not be read: %w", err)
}
@@ -189,12 +197,14 @@ func intField(name string, v jsontext.Value, min, max int) (int, error) {
return n, nil
}
func defaultLogPath() string {
home, err := os.UserHomeDir()
var userHomeDir = os.UserHomeDir
func defaultLogPath() (string, error) {
home, err := userHomeDir()
if err != nil {
return ""
return "", fmt.Errorf("config path: %w", err)
}
return filepath.Join(home, "funknetz.log")
return filepath.Join(home, "funknetz.log"), nil
}
func present(v jsontext.Value) bool { return len(v) > 0 }
@@ -243,7 +253,7 @@ func blockedLog(shape fileShape) (block bool, notice string) {
return true, ""
}
func withLogDefaults(shape fileShape) fileShape {
func withLogDefaults(shape fileShape) (fileShape, error) {
shape.Host = rawOr(shape.Host, `""`)
shape.Port = rawOr(shape.Port, "0")
shape.Callsign = rawOr(shape.Callsign, `""`)
@@ -251,13 +261,17 @@ func withLogDefaults(shape fileShape) fileShape {
shape.TalkGroup = rawOr(shape.TalkGroup, "0")
shape.EnableLogging = rawOr(shape.EnableLogging, "false")
if !present(shape.LogFile) {
raw, err := jsonv2.Marshal(defaultLogPath())
logFile, err := defaultLogPath()
if err != nil {
raw = []byte(`""`)
return fileShape{}, err
}
raw, err := jsonv2.Marshal(logFile)
if err != nil {
return fileShape{}, err
}
shape.LogFile = jsontext.Value(raw)
}
return shape
return shape, nil
}
func rawOr(v jsontext.Value, fallback string) jsontext.Value {
+52
View File
@@ -0,0 +1,52 @@
package station
import (
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
func TestMissingHomeDoesNotCreateConfig(t *testing.T) {
orig := userHomeDir
t.Cleanup(func() { userHomeDir = orig })
userHomeDir = func() (string, error) {
return "", errors.New("no home")
}
path := filepath.Join(t.TempDir(), "lab.json")
_, err := New(Options{ConfigPath: path})
if err == nil || !strings.HasPrefix(err.Error(), "config path:") {
t.Fatalf("New() error = %v, want a config path failure", err)
}
if _, statErr := os.Stat(path); !errors.Is(statErr, fs.ErrNotExist) {
t.Fatalf("stat = %v, want the file absent", statErr)
}
}
func TestMissingHomeDoesNotRewriteConfig(t *testing.T) {
orig := userHomeDir
t.Cleanup(func() { userHomeDir = orig })
userHomeDir = func() (string, error) {
return "", errors.New("no home")
}
path := filepath.Join(t.TempDir(), "lab.json")
original := []byte("{\"host\":\"127.0.0.1\",\"port\":5308,\"callsign\":\"DL1ABC\",\"authKey\":\"key\",\"talkGroup\":20}\n")
if err := os.WriteFile(path, original, 0o640); err != nil {
t.Fatal(err)
}
_, err := New(Options{ConfigPath: path})
if err == nil || !strings.HasPrefix(err.Error(), "config path:") {
t.Fatalf("New() error = %v, want a config path failure", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(got) != string(original) {
t.Fatalf("file = %q, want %q", got, original)
}
}