Skip to content

Commit 14945a4

Browse files
authored
Thunk-ify layout options (google#847)
If we want to add anything other than descriptor mutators as options, we need a place to do that, so this introduces an options struct that holds options and thunk-ifies the existing descriptor mutators.
1 parent efb2d62 commit 14945a4

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

pkg/v1/layout/options.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,55 @@ package layout
33
import v1 "github.com/google/go-containerregistry/pkg/v1"
44

55
// Option is a functional option for Layout.
6-
//
7-
// TODO: We'll need to change this signature to support Sparse/Thin images.
8-
// Or, alternatively, wrap it in a sparse.Image that returns an empty list for layers?
9-
type Option func(*v1.Descriptor) error
6+
type Option func(*options)
7+
8+
type options struct {
9+
descOpts []descriptorOption
10+
}
11+
12+
func makeOptions(opts ...Option) *options {
13+
o := &options{
14+
descOpts: []descriptorOption{},
15+
}
16+
for _, apply := range opts {
17+
apply(o)
18+
}
19+
return o
20+
}
21+
22+
type descriptorOption func(*v1.Descriptor)
1023

1124
// WithAnnotations adds annotations to the artifact descriptor.
1225
func WithAnnotations(annotations map[string]string) Option {
13-
return func(desc *v1.Descriptor) error {
14-
if desc.Annotations == nil {
15-
desc.Annotations = make(map[string]string)
16-
}
17-
for k, v := range annotations {
18-
desc.Annotations[k] = v
19-
}
20-
21-
return nil
26+
return func(o *options) {
27+
o.descOpts = append(o.descOpts, func(desc *v1.Descriptor) {
28+
if desc.Annotations == nil {
29+
desc.Annotations = make(map[string]string)
30+
}
31+
for k, v := range annotations {
32+
desc.Annotations[k] = v
33+
}
34+
})
2235
}
2336
}
2437

2538
// WithURLs adds urls to the artifact descriptor.
2639
func WithURLs(urls []string) Option {
27-
return func(desc *v1.Descriptor) error {
28-
if desc.URLs == nil {
29-
desc.URLs = []string{}
30-
}
31-
desc.URLs = append(desc.URLs, urls...)
32-
return nil
40+
return func(o *options) {
41+
o.descOpts = append(o.descOpts, func(desc *v1.Descriptor) {
42+
if desc.URLs == nil {
43+
desc.URLs = []string{}
44+
}
45+
desc.URLs = append(desc.URLs, urls...)
46+
})
3347
}
3448
}
3549

3650
// WithPlatform sets the platform of the artifact descriptor.
3751
func WithPlatform(platform v1.Platform) Option {
38-
return func(desc *v1.Descriptor) error {
39-
desc.Platform = &platform
40-
return nil
52+
return func(o *options) {
53+
o.descOpts = append(o.descOpts, func(desc *v1.Descriptor) {
54+
desc.Platform = &platform
55+
})
4156
}
4257
}

pkg/v1/layout/write.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ func (l Path) AppendImage(img v1.Image, options ...Option) error {
6262
Digest: d,
6363
}
6464

65-
for _, opt := range options {
66-
if err := opt(&desc); err != nil {
67-
return err
68-
}
65+
o := makeOptions(options...)
66+
for _, opt := range o.descOpts {
67+
opt(&desc)
6968
}
7069

7170
return l.AppendDescriptor(desc)
@@ -99,10 +98,9 @@ func (l Path) AppendIndex(ii v1.ImageIndex, options ...Option) error {
9998
Digest: d,
10099
}
101100

102-
for _, opt := range options {
103-
if err := opt(&desc); err != nil {
104-
return err
105-
}
101+
o := makeOptions(options...)
102+
for _, opt := range o.descOpts {
103+
opt(&desc)
106104
}
107105

108106
return l.AppendDescriptor(desc)

0 commit comments

Comments
 (0)