Skip to content
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

extract: Add callbacks #72

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ import (
"github.com/canonical/chisel/internal/deb"
)

type PackageInfo interface {
Name() string
Version() string
Arch() string
SHA256() string
}

type Archive interface {
Options() *Options
Fetch(pkg string) (io.ReadCloser, error)
Info(pkg string) PackageInfo
Exists(pkg string) bool
}

Expand Down Expand Up @@ -86,6 +94,22 @@ func (a *ubuntuArchive) Exists(pkg string) bool {
return err == nil
}

type pkgInfo struct{ control.Section }

var _ PackageInfo = pkgInfo{}

func (info pkgInfo) Name() string { return info.Get("Package") }
func (info pkgInfo) Version() string { return info.Get("Version") }
func (info pkgInfo) Arch() string { return info.Get("Architecture") }
func (info pkgInfo) SHA256() string { return info.Get("SHA256") }

func (a *ubuntuArchive) Info(pkg string) PackageInfo {
if section, _, _ := a.selectPackage(pkg); section != nil {
return &pkgInfo{section}
}
return nil
}

func (a *ubuntuArchive) selectPackage(pkg string) (control.Section, *ubuntuIndex, error) {
var selectedVersion string
var selectedSection control.Section
Expand Down
33 changes: 33 additions & 0 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,39 @@ func (s *httpSuite) TestArchiveLabels(c *C) {
c.Assert(err, ErrorMatches, `.*\bno Ubuntu section`)
}

func (s *httpSuite) TestPackageInfo(c *C) {
s.prepareArchive("jammy", "22.04", "amd64", []string{"main", "universe"})

options := archive.Options{
Label: "ubuntu",
Version: "22.04",
Arch: "amd64",
Suites: []string{"jammy"},
Components: []string{"main", "universe"},
CacheDir: c.MkDir(),
}

archive, err := archive.Open(&options)
c.Assert(err, IsNil)

info1 := archive.Info("mypkg1")
c.Assert(info1, NotNil)
c.Assert(info1.Name(), Equals, "mypkg1")
c.Assert(info1.Version(), Equals, "1.1")
c.Assert(info1.Arch(), Equals, "amd64")
c.Assert(info1.SHA256(), Equals, "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05")

info3 := archive.Info("mypkg3")
c.Assert(info3, NotNil)
c.Assert(info3.Name(), Equals, "mypkg3")
c.Assert(info3.Version(), Equals, "1.3")
c.Assert(info3.Arch(), Equals, "amd64")
c.Assert(info3.SHA256(), Equals, "fe377bf13ba1a5cb287cb4e037e6e7321281c929405ae39a72358ef0f5d179aa")

info99 := archive.Info("mypkg99")
c.Assert(info99, IsNil)
}

func read(r io.Reader) string {
data, err := io.ReadAll(r)
if err != nil {
Expand Down
Loading