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:
+24
-10
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user