loadJSONConfig reads path as a JSON object. A missing or whitespace-only file is treated as an empty config (so AddPadEntry can populate from scratch on first install). Callers that need to distinguish "missing" from "empty" check os.Stat separately. Read or parse errors on a non-empty file propaga
(path string)
| 321 | // never silently overwrite a corrupt config; the user has to fix or |
| 322 | // remove it. |
| 323 | func loadJSONConfig(path string) (map[string]any, error) { |
| 324 | b, err := os.ReadFile(path) |
| 325 | if errors.Is(err, os.ErrNotExist) { |
| 326 | return map[string]any{}, nil |
| 327 | } |
| 328 | if err != nil { |
| 329 | return nil, fmt.Errorf("read %s: %w", path, err) |
| 330 | } |
| 331 | if isAllWhitespace(b) { |
| 332 | return map[string]any{}, nil |
| 333 | } |
| 334 | var raw map[string]any |
| 335 | if err := json.Unmarshal(b, &raw); err != nil { |
| 336 | return nil, fmt.Errorf("parse %s: %w", path, err) |
| 337 | } |
| 338 | if raw == nil { |
| 339 | raw = map[string]any{} |
| 340 | } |
| 341 | return raw, nil |
| 342 | } |
| 343 | |
| 344 | func isAllWhitespace(b []byte) bool { |
| 345 | for _, c := range b { |
no test coverage detected