Skip to content

Commit

Permalink
Read takes io.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Aug 19, 2024
1 parent 56dd49c commit 8dfcd2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
22 changes: 5 additions & 17 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8dfcd2b

Please sign in to comment.