Skip to content

Commit cf67e26

Browse files
Rework types a little
1 parent ac1b8a8 commit cf67e26

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

pkg/apk/apk/implementation.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,10 @@ func (a *APK) InstallPackages(ctx context.Context, sourceDateEpoch *time.Time, a
786786
if err != nil {
787787
return fmt.Errorf("failed to read .PKGINFO for %s: %w", pkg, err)
788788
}
789-
infos[i] = pkgInfo
789+
asPackage := pkgInfo.AsPackage(exp.ControlHash, uint64(exp.Size))
790+
infos[i] = &asPackage
790791

791-
installedFiles, err := a.installPackage(ctx, pkgInfo, exp, sourceDateEpoch)
792+
installedFiles, err := a.installPackage(ctx, &asPackage, exp, sourceDateEpoch)
792793
if err != nil {
793794
return fmt.Errorf("installing %s: %w", pkg, err)
794795
}

pkg/apk/expandapk/expandapk.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"path/filepath"
2222
"strings"
2323
"sync"
24-
"time"
2524

2625
"chainguard.dev/apko/internal/tarfs"
2726
"chainguard.dev/apko/pkg/apk/types"
@@ -104,12 +103,12 @@ type APKExpanded struct {
104103
SignatureSize int64
105104

106105
sync.Mutex
107-
parsedPkgInfo *types.Package
106+
parsedPkgInfo *types.PackageInfo
108107
controlData []byte
109108
}
110109

111110
// PkgInfo parses and returns the .PKGINFO file.
112-
func (a *APKExpanded) PkgInfo() (*types.Package, error) {
111+
func (a *APKExpanded) PkgInfo() (*types.PackageInfo, error) {
113112
a.Lock()
114113
defer a.Unlock()
115114
if a.parsedPkgInfo != nil {
@@ -127,31 +126,7 @@ func (a *APKExpanded) PkgInfo() (*types.Package, error) {
127126
return nil, fmt.Errorf("parsing .PKGINFO: %w", err)
128127
}
129128

130-
pkg := &types.Package{
131-
Name: pkginfo.Name,
132-
Version: pkginfo.Version,
133-
Arch: pkginfo.Arch,
134-
Description: pkginfo.Description,
135-
License: pkginfo.License,
136-
Origin: pkginfo.Origin,
137-
Maintainer: pkginfo.Maintainer,
138-
URL: pkginfo.URL,
139-
Checksum: a.ControlHash,
140-
Dependencies: pkginfo.Dependencies,
141-
Provides: pkginfo.Provides,
142-
InstallIf: pkginfo.InstallIf,
143-
Size: uint64(a.Size),
144-
InstalledSize: pkginfo.Size,
145-
ProviderPriority: pkginfo.ProviderPriority,
146-
BuildTime: time.Unix(pkginfo.BuildDate, 0).UTC(),
147-
BuildDate: pkginfo.BuildDate,
148-
RepoCommit: pkginfo.RepoCommit,
149-
Replaces: pkginfo.Replaces,
150-
DataHash: pkginfo.DataHash,
151-
Triggers: pkginfo.Triggers,
152-
}
153-
154-
a.parsedPkgInfo = pkg
129+
a.parsedPkgInfo = pkginfo
155130
return a.parsedPkgInfo, nil
156131
}
157132

pkg/apk/expandapk/expandapk_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"archive/tar"
55
"bytes"
66
"testing"
7-
"time"
87

98
"github.com/stretchr/testify/require"
109

@@ -13,13 +12,10 @@ import (
1312
)
1413

1514
func TestPkgInfo(t *testing.T) {
16-
controlHash := []byte{0x01, 0x02, 0x03, 0x04}
17-
size := int64(123456)
18-
1915
tests := []struct {
2016
name string
2117
content string
22-
want *types.Package
18+
want *types.PackageInfo
2319
}{{
2420
// See example at https://wiki.alpinelinux.org/wiki/Apk_spec
2521
name: "example",
@@ -49,7 +45,7 @@ provides = cmd:sh=1.35.0-r18
4945
depend = so:libc.musl-x86_64.so.1
5046
datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
5147
`,
52-
want: &types.Package{
48+
want: &types.PackageInfo{
5349
Name: "busybox",
5450
Version: "1.35.0-r18",
5551
Arch: "x86_64",
@@ -61,16 +57,13 @@ datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
6157
Dependencies: []string{"so:libc.musl-x86_64.so.1"},
6258
Provides: []string{"/bin/sh", "cmd:busybox=1.35.0-r18", "cmd:sh=1.35.0-r18"},
6359
InstallIf: nil,
64-
Size: uint64(size),
60+
Size: 958464,
6561
ProviderPriority: 100,
6662
BuildDate: 1657134589,
6763
RepoCommit: "332d2fff53cd4537d415e15e55e8ceb6fe6eaedb",
6864
Replaces: []string{"busybox-initscripts"},
6965
DataHash: "7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7",
7066
Triggers: []string{"/bin /usr/bin /sbin /usr/sbin /lib/modules/*"},
71-
Checksum: controlHash,
72-
InstalledSize: 958464,
73-
BuildTime: time.Unix(1657134589, 0).UTC(),
7467
},
7568
}}
7669

@@ -91,11 +84,7 @@ datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
9184
controlFs, err := tarfs.New(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
9285
require.NoError(t, err)
9386

94-
exp := &APKExpanded{
95-
ControlHash: controlHash,
96-
ControlFS: controlFs,
97-
Size: size,
98-
}
87+
exp := &APKExpanded{ControlFS: controlFs}
9988

10089
got, err := exp.PkgInfo()
10190
if err != nil {

pkg/apk/types/package.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ type PackageInfo struct {
3131
Triggers []string `ini:"triggers,,allowshadow"`
3232
}
3333

34+
// AsPackage converts PackageInfo to a Package struct with the given controlHash and size.
35+
func (pkginfo PackageInfo) AsPackage(controlHash []byte, size uint64) Package {
36+
return Package{
37+
Name: pkginfo.Name,
38+
Version: pkginfo.Version,
39+
Arch: pkginfo.Arch,
40+
Description: pkginfo.Description,
41+
License: pkginfo.License,
42+
Origin: pkginfo.Origin,
43+
Maintainer: pkginfo.Maintainer,
44+
URL: pkginfo.URL,
45+
Dependencies: pkginfo.Dependencies,
46+
Provides: pkginfo.Provides,
47+
InstallIf: pkginfo.InstallIf,
48+
InstalledSize: pkginfo.Size,
49+
ProviderPriority: pkginfo.ProviderPriority,
50+
BuildDate: pkginfo.BuildDate,
51+
RepoCommit: pkginfo.RepoCommit,
52+
Replaces: pkginfo.Replaces,
53+
DataHash: pkginfo.DataHash,
54+
Triggers: pkginfo.Triggers,
55+
56+
BuildTime: time.Unix(pkginfo.BuildDate, 0).UTC(),
57+
Checksum: controlHash,
58+
Size: size,
59+
}
60+
}
61+
3462
// Package represents a single package with the information present in an
3563
// APKINDEX.
3664
type Package struct {

0 commit comments

Comments
 (0)