From 8dfcd2b1dd4bc3692b3325fe1a0decad287a8783 Mon Sep 17 00:00:00 2001 From: Alberto Carretero Date: Mon, 19 Aug 2024 10:24:56 +0200 Subject: [PATCH] Read takes io.Reader --- internal/manifest/manifest.go | 22 +++++----------------- internal/manifest/manifest_test.go | 9 ++++++++- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index df9b138e..fc8a1366 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -2,11 +2,9 @@ package manifest import ( "fmt" - "os" + "io" "slices" - "github.com/klauspost/compress/zstd" - "github.com/canonical/chisel/internal/jsonwall" "github.com/canonical/chisel/internal/setup" ) @@ -47,26 +45,16 @@ type Manifest struct { db *jsonwall.DB } -// Read loads a Manifest from a file without performing any validation. The file -// is assumed to be both valid jsonwall and a valid Manifest (see [Validate]). -func Read(absPath string) (manifest *Manifest, err error) { +// Read loads a Manifest without performing any validation. The data is assumed +// to be both valid jsonwall and a valid Manifest (see Validate). +func Read(reader io.Reader) (manifest *Manifest, err error) { defer func() { if err != nil { err = fmt.Errorf("cannot read manifest: %s", err) } }() - file, err := os.OpenFile(absPath, os.O_RDONLY, 0644) - if err != nil { - return nil, err - } - defer file.Close() - r, err := zstd.NewReader(file) - if err != nil { - return nil, err - } - defer r.Close() - db, err := jsonwall.ReadDB(r) + db, err := jsonwall.ReadDB(reader) if err != nil { return nil, err } diff --git a/internal/manifest/manifest_test.go b/internal/manifest/manifest_test.go index 1266f4d2..de0ad7eb 100644 --- a/internal/manifest/manifest_test.go +++ b/internal/manifest/manifest_test.go @@ -151,7 +151,14 @@ func (s *S) TestRun(c *C) { w.Close() f.Close() - mfest, err := manifest.Read(manifestPath) + file, err := os.OpenFile(manifestPath, os.O_RDONLY, 0644) + c.Assert(err, IsNil) + defer file.Close() + r, err := zstd.NewReader(file) + c.Assert(err, IsNil) + defer r.Close() + + mfest, err := manifest.Read(r) if test.readError != "" { c.Assert(err, ErrorMatches, test.readError) continue