From fbb04c561143bdd0790c79e6ff36eab2a582ddc2 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 28 Sep 2026 13:33:54 +0000 Subject: [PATCH] testcapture: read captures through util.ReadFileByExt Drops its private HuJSON decoder. --- hscontrol/types/testcapture/read.go | 34 +++-------------------------- 1 file changed, 3 insertions(+), 31 deletions(-) diff --git a/hscontrol/types/testcapture/read.go b/hscontrol/types/testcapture/read.go index 2c153bbb..6f05eb57 100644 --- a/hscontrol/types/testcapture/read.go +++ b/hscontrol/types/testcapture/read.go @@ -1,12 +1,10 @@ package testcapture import ( - "encoding/json" "errors" "fmt" - "os" - "github.com/tailscale/hujson" + "github.com/juanfont/headscale/hscontrol/util" ) // ErrUnsupportedSchemaVersion is returned by [Read] when a capture @@ -22,16 +20,9 @@ var ErrUnsupportedSchemaVersion = errors.New("testcapture: unsupported schema ve // The returned [Capture]'s [Capture.CapturedAt] is the value recorded in the file // (not "now"). func Read(path string) (*Capture, error) { - data, err := os.ReadFile(path) + c, err := util.ReadFileByExt[Capture](path) if err != nil { - return nil, fmt.Errorf("testcapture: read %s: %w", path, err) - } - - var c Capture - - err = unmarshalHuJSON(data, &c) - if err != nil { - return nil, fmt.Errorf("testcapture: %s: %w", path, err) + return nil, fmt.Errorf("testcapture: %w", err) } if c.SchemaVersion > SchemaVersion { @@ -41,22 +32,3 @@ func Read(path string) (*Capture, error) { return &c, nil } - -// unmarshalHuJSON parses HuJSON bytes (JSON with comments / trailing -// commas) into v. Comments are stripped via [hujson.Value.Standardize] before -// [json.Unmarshal] is called. -func unmarshalHuJSON(data []byte, v any) error { - ast, err := hujson.Parse(data) - if err != nil { - return fmt.Errorf("hujson parse: %w", err) - } - - ast.Standardize() - - err = json.Unmarshal(ast.Pack(), v) - if err != nil { - return fmt.Errorf("json unmarshal: %w", err) - } - - return nil -}