...
1 package schema2
2
3 import (
4 "context"
5
6 "github.com/docker/distribution"
7 "github.com/opencontainers/go-digest"
8 )
9
10
11 type builder struct {
12
13 bs distribution.BlobService
14
15
16 configMediaType string
17
18
19 configJSON []byte
20
21
22
23 dependencies []distribution.Descriptor
24 }
25
26
27
28
29 func NewManifestBuilder(bs distribution.BlobService, configMediaType string, configJSON []byte) distribution.ManifestBuilder {
30 mb := &builder{
31 bs: bs,
32 configMediaType: configMediaType,
33 configJSON: make([]byte, len(configJSON)),
34 }
35 copy(mb.configJSON, configJSON)
36
37 return mb
38 }
39
40
41 func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) {
42 m := Manifest{
43 Versioned: SchemaVersion,
44 Layers: make([]distribution.Descriptor, len(mb.dependencies)),
45 }
46 copy(m.Layers, mb.dependencies)
47
48 configDigest := digest.FromBytes(mb.configJSON)
49
50 var err error
51 m.Config, err = mb.bs.Stat(ctx, configDigest)
52 switch err {
53 case nil:
54
55
56 m.Config.MediaType = mb.configMediaType
57 return FromStruct(m)
58 case distribution.ErrBlobUnknown:
59
60 default:
61 return nil, err
62 }
63
64
65 m.Config, err = mb.bs.Put(ctx, mb.configMediaType, mb.configJSON)
66
67
68 m.Config.MediaType = mb.configMediaType
69 if err != nil {
70 return nil, err
71 }
72
73 return FromStruct(m)
74 }
75
76
77 func (mb *builder) AppendReference(d distribution.Describable) error {
78 mb.dependencies = append(mb.dependencies, d.Descriptor())
79 return nil
80 }
81
82
83 func (mb *builder) References() []distribution.Descriptor {
84 return mb.dependencies
85 }
86
View as plain text