Skip to content

Commit 21d68d8

Browse files
committed
Add Architectures sorting, deduplication, and validation
1 parent 4fd80f3 commit 21d68d8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

manifest/example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GitFetch: refs/heads/master
1919
GitRepo: https://github.com/docker-library/golang.git
2020
SharedTags: latest
2121
arm64v8-GitRepo: https://github.com/docker-library/golang.git
22-
Architectures: amd64
22+
Architectures: amd64, amd64
2323
2424
2525
# hi
@@ -57,7 +57,7 @@ ppc64le-Directory: 1.5/ppc64le
5757
SharedTags: raspbian
5858
GitCommit: deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
5959
Tags: raspbian-s390x
60-
Architectures: s390x
60+
Architectures: s390x, i386
6161
6262
6363
`)))
@@ -115,7 +115,7 @@ i: g@h j
115115
//
116116
// Tags: raspbian-s390x
117117
// SharedTags: raspbian
118-
// Architectures: s390x
118+
// Architectures: i386, s390x
119119
// GitCommit: deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
120120
//
121121
// Shared Tag Groups:

manifest/rfc2822.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strings"
1010

11+
"github.com/docker-library/go-dockerlibrary/architecture"
1112
"github.com/docker-library/go-dockerlibrary/pkg/stripper"
1213

1314
"pault.ag/go/debian/control"
@@ -364,11 +365,15 @@ func (manifest *Manifest2822) AddEntry(entry Manifest2822Entry) error {
364365
return fmt.Errorf("Tags %q missing one of GitRepo, GitFetch, or GitCommit", entry.TagsString())
365366
}
366367
if invalidMaintainers := entry.InvalidMaintainers(); len(invalidMaintainers) > 0 {
367-
return fmt.Errorf("Tags %q has invalid Maintainers: %q (expected format %q)", strings.Join(invalidMaintainers, ", "), MaintainersFormat)
368+
return fmt.Errorf("Tags %q has invalid Maintainers: %q (expected format %q)", entry.TagsString(), strings.Join(invalidMaintainers, ", "), MaintainersFormat)
368369
}
369370

370371
entry.DeduplicateSharedTags()
371372

373+
if invalidArchitectures := entry.InvalidArchitectures(); len(invalidArchitectures) > 0 {
374+
return fmt.Errorf("Tags %q has invalid Architectures: %q", entry.TagsString(), strings.Join(invalidArchitectures, ", "))
375+
}
376+
372377
seenTag := map[string]bool{}
373378
for _, tag := range entry.Tags {
374379
if otherEntry := manifest.GetTag(tag); otherEntry != nil {
@@ -428,6 +433,16 @@ func (entry Manifest2822Entry) InvalidMaintainers() []string {
428433
return invalid
429434
}
430435

436+
func (entry Manifest2822Entry) InvalidArchitectures() []string {
437+
invalid := []string{}
438+
for _, arch := range entry.Architectures {
439+
if _, ok := architecture.SupportedArches[arch]; !ok {
440+
invalid = append(invalid, arch)
441+
}
442+
}
443+
return invalid
444+
}
445+
431446
// DeduplicateSharedTags will remove duplicate values from entry.SharedTags, preserving order.
432447
func (entry *Manifest2822Entry) DeduplicateSharedTags() {
433448
aggregate := []string{}
@@ -442,6 +457,21 @@ func (entry *Manifest2822Entry) DeduplicateSharedTags() {
442457
entry.SharedTags = aggregate
443458
}
444459

460+
// DeduplicateArchitectures will remove duplicate values from entry.Architectures and sort the result.
461+
func (entry *Manifest2822Entry) DeduplicateArchitectures() {
462+
aggregate := []string{}
463+
seen := map[string]bool{}
464+
for _, arch := range entry.Architectures {
465+
if seen[arch] {
466+
continue
467+
}
468+
seen[arch] = true
469+
aggregate = append(aggregate, arch)
470+
}
471+
sort.Strings(aggregate)
472+
entry.Architectures = aggregate
473+
}
474+
445475
type decoderWrapper struct {
446476
*control.Decoder
447477
}
@@ -471,6 +501,7 @@ func (decoder *decoderWrapper) Decode(entry *Manifest2822Entry) error {
471501
if len(entry.Architectures) == 0 {
472502
entry.Architectures = arches
473503
}
504+
entry.DeduplicateArchitectures()
474505

475506
// pull out any new architecture-specific values from Paragraph.Values
476507
entry.SeedArchValues()
@@ -504,6 +535,9 @@ func Parse2822(readerIn io.Reader) (*Manifest2822, error) {
504535
if len(manifest.Global.Tags) > 0 {
505536
return nil, fmt.Errorf("global Tags not permitted")
506537
}
538+
if invalidArchitectures := manifest.Global.InvalidArchitectures(); len(invalidArchitectures) > 0 {
539+
return nil, fmt.Errorf("invalid global Architectures: %q", strings.Join(invalidArchitectures, ", "))
540+
}
507541

508542
for {
509543
entry := manifest.Global.Clone()

0 commit comments

Comments
 (0)