Repository navigation
feat: support extraction of multiple package types #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
upils
wants to merge
26
commits into
canonical:main
Choose a base branch
from
upils:support-bin-extract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f91d449
feat: extract bins
upils 1663446
refactor: rework approach
upils 3435cee
refactor: refining
upils 09a01bd
refactor: refining
upils 8d87fae
docs: clean comments
upils 1a38ccd
docs: remove mentions of packages in tarball pkg
upils ca8d6e8
Merge branch 'main' into support-bin-extract
upils ee8732a
style: refine naming and docs
upils ca72fd6
refactor: rework approach
upils dc8e94b
fix: clean superfluous import
upils f882d47
test: remove unused field for now
upils 116752e
refactor: put deb DataReader back in deb package
upils e9c4d88
style: add missing trailing newline
upils b1ec390
style: refine naming
upils a8ad813
style: improve naming consistency
upils 6ad029e
style: avoid ambiguity
upils 150e904
refactor: rework approach with interface
upils 791224b
refactor: refine interface
upils 50b03d1
tests: polish tests
upils 798ce59
docs: fix misleading docs
upils ec2c67c
refactor: rename to fit new abstraction
upils 6f2fe07
docs: refine docs
upils 436a161
fix: clarify interface following review
upils 7afa609
fix: use a single default test package
upils 95a45c1
fix: use better names
upils 1a0d381
ci: rerun
upils File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package bin | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Note to reviewer]: The introduction of this package can be done in a follow up PR if we want this one to focus on the reworked interface. I added it as this is rather small and it illustrates the work in this PR is not artificial. |
||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/ulikunitz/xz" | ||
| ) | ||
|
|
||
| type Pkg struct { | ||
| reader io.ReadSeekCloser | ||
| } | ||
|
|
||
| func Open(reader io.ReadSeekCloser) *Pkg { | ||
| return &Pkg{reader: reader} | ||
| } | ||
|
|
||
| func (p *Pkg) TarStream() (io.ReadCloser, error) { | ||
| _, err := p.reader.Seek(0, io.SeekStart) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| xzReader, err := xz.NewReader(p.reader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return io.NopCloser(xzReader), nil | ||
| } | ||
|
|
||
| func (p *Pkg) Close() error { | ||
| return p.reader.Close() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package bin_test | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
|
|
||
| . "gopkg.in/check.v1" | ||
|
|
||
| "github.com/canonical/chisel/internal/bin" | ||
| "github.com/canonical/chisel/internal/tarball" | ||
| "github.com/canonical/chisel/internal/testutil" | ||
| ) | ||
|
|
||
| var _ tarball.TarStreamer = (*bin.Pkg)(nil) | ||
|
|
||
| func (s *S) TestPkgTarStream(c *C) { | ||
| pkg := testutil.NewBinPkg(testutil.MustMakeBin([]testutil.TarEntry{ | ||
| testutil.Dir(0o755, "./"), | ||
| testutil.Reg(0o644, "./file", "content"), | ||
| })) | ||
|
|
||
| // Each call returns a fresh stream over the same content. | ||
| for range 2 { | ||
| tarStream, err := pkg.TarStream() | ||
| c.Assert(err, IsNil) | ||
| tarReader := tar.NewReader(tarStream) | ||
| _, err = tarReader.Next() | ||
| c.Assert(err, IsNil) | ||
| err = tarStream.Close() | ||
| c.Assert(err, IsNil) | ||
| } | ||
| } | ||
|
|
||
| func (s *S) TestPkgTarStreamInvalid(c *C) { | ||
| pkg := testutil.NewBinPkg([]byte("not an xz stream")) | ||
|
|
||
| _, err := pkg.TarStream() | ||
| c.Assert(err, ErrorMatches, "xz.*") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package bin | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| ) | ||
|
|
||
| // Avoid importing the log type information unnecessarily. There's a small cost | ||
| // associated with using an interface rather than the type. Depending on how | ||
| // often the logger is plugged in, it would be worth using the type instead. | ||
| type log_Logger interface { | ||
| Output(calldepth int, s string) error | ||
| } | ||
|
|
||
| var globalLoggerLock sync.Mutex | ||
| var globalLogger log_Logger | ||
| var globalDebug bool | ||
|
|
||
| // Specify the *log.Logger object where log messages should be sent to. | ||
| func SetLogger(logger log_Logger) { | ||
| globalLoggerLock.Lock() | ||
| globalLogger = logger | ||
| globalLoggerLock.Unlock() | ||
| } | ||
|
|
||
| // Enable the delivery of debug messages to the logger. Only meaningful | ||
| // if a logger is also set. | ||
| func SetDebug(debug bool) { | ||
| globalLoggerLock.Lock() | ||
| globalDebug = debug | ||
| globalLoggerLock.Unlock() | ||
| } | ||
|
|
||
| // logf sends to the logger registered via SetLogger the string resulting | ||
| // from running format and args through Sprintf. | ||
| func logf(format string, args ...any) { | ||
| globalLoggerLock.Lock() | ||
| defer globalLoggerLock.Unlock() | ||
| if globalLogger != nil { | ||
| globalLogger.Output(2, fmt.Sprintf(format, args...)) | ||
| } | ||
| } | ||
|
|
||
| // debugf sends to the logger registered via SetLogger the string resulting | ||
| // from running format and args through Sprintf, but only if debugging was | ||
| // enabled via SetDebug. | ||
| func debugf(format string, args ...any) { | ||
| globalLoggerLock.Lock() | ||
| defer globalLoggerLock.Unlock() | ||
| if globalDebug && globalLogger != nil { | ||
| globalLogger.Output(2, fmt.Sprintf(format, args...)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package bin_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "gopkg.in/check.v1" | ||
|
|
||
| "github.com/canonical/chisel/internal/bin" | ||
| ) | ||
|
|
||
| func Test(t *testing.T) { TestingT(t) } | ||
|
|
||
| type S struct{} | ||
|
|
||
| var _ = Suite(&S{}) | ||
|
|
||
| func (s *S) SetUpTest(c *C) { | ||
| bin.SetDebug(true) | ||
| bin.SetLogger(c) | ||
| } | ||
|
|
||
| func (s *S) TearDownTest(c *C) { | ||
| bin.SetDebug(false) | ||
| bin.SetLogger(nil) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Note to reviewer]: These hashes changed because the shape of the input in the test changed. A simple
[]bytecannot be used anymore as thetestarchive.Packagenow expects a valid package, providing extractable content. However the actual content of the input package did not change, so this is a benign change.