-
Notifications
You must be signed in to change notification settings - Fork 18
/
component.go
173 lines (153 loc) · 4.38 KB
/
component.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package genericocireg
import (
"strings"
"github.com/Masterminds/semver/v3"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/goutils/general"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/contexts/oci/artdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi/repocpi"
"github.com/open-component-model/ocm/pkg/refmgmt"
)
const META_SEPARATOR = ".build-"
////////////////////////////////////////////////////////////////////////////////
type componentAccessImpl struct {
bridge repocpi.ComponentAccessBridge
repo *RepositoryImpl
name string
namespace oci.NamespaceAccess
}
func newComponentAccess(repo *RepositoryImpl, name string, main bool) (*repocpi.ComponentAccessInfo, error) {
mapped, err := repo.MapComponentNameToNamespace(name)
if err != nil {
return nil, err
}
namespace, err := repo.ocirepo.LookupNamespace(mapped)
if err != nil {
return nil, err
}
impl := &componentAccessImpl{
repo: repo,
name: name,
namespace: namespace,
}
return &repocpi.ComponentAccessInfo{impl, "OCM component[OCI]", main}, nil
}
func (c *componentAccessImpl) Close() error {
refmgmt.AllocLog.Trace("closing component [OCI]", "name", c.name)
err := c.namespace.Close()
refmgmt.AllocLog.Trace("closed component [OCI]", "name", c.name)
return err
}
func (c *componentAccessImpl) SetBridge(base repocpi.ComponentAccessBridge) {
c.bridge = base
}
func (c *componentAccessImpl) GetParentBridge() repocpi.RepositoryViewManager {
return c.repo.bridge
}
func (c *componentAccessImpl) GetContext() cpi.Context {
return c.repo.GetContext()
}
func (c *componentAccessImpl) GetName() string {
return c.name
}
////////////////////////////////////////////////////////////////////////////////
func toTag(v string) (string, error) {
_, err := semver.NewVersion(v)
if err != nil {
return "", err
}
return strings.ReplaceAll(v, "+", META_SEPARATOR), nil
}
func toVersion(t string) string {
next := 0
for {
if idx := strings.Index(t[next:], META_SEPARATOR); idx >= 0 {
next += idx + len(META_SEPARATOR)
} else {
break
}
}
if next == 0 {
return t
}
return t[:next-len(META_SEPARATOR)] + "+" + t[next:]
}
func (c *componentAccessImpl) IsReadOnly() bool {
return c.repo.IsReadOnly()
}
func (c *componentAccessImpl) ListVersions() ([]string, error) {
tags, err := c.namespace.ListTags()
if err != nil {
return nil, err
}
result := make([]string, 0, len(tags))
for _, t := range tags {
// omit reported digests (typically for ctf)
if ok, _ := artdesc.IsDigest(t); !ok {
result = append(result, toVersion(t))
}
}
return result, err
}
func (c *componentAccessImpl) HasVersion(vers string) (bool, error) {
tags, err := c.namespace.ListTags()
if err != nil {
return false, err
}
for _, t := range tags {
// omit reported digests (typically for ctf)
if ok, _ := artdesc.IsDigest(t); !ok {
if vers == t {
return true, nil
}
}
}
return false, err
}
func (c *componentAccessImpl) LookupVersion(version string) (*repocpi.ComponentVersionAccessInfo, error) {
tag, err := toTag(version)
if err != nil {
return nil, err
}
acc, err := c.namespace.GetArtifact(tag)
if err != nil {
if errors.IsErrNotFound(err) {
return nil, cpi.ErrComponentVersionNotFoundWrap(err, c.name, version)
}
return nil, err
}
m := accessobj.ACC_WRITABLE
if c.IsReadOnly() {
m = accessobj.ACC_READONLY
}
return newComponentVersionAccess(m, c, version, acc, true)
}
func (c *componentAccessImpl) NewVersion(version string, overrides ...bool) (*repocpi.ComponentVersionAccessInfo, error) {
if c.IsReadOnly() {
return nil, accessio.ErrReadOnly
}
override := general.Optional(overrides...)
tag, err := toTag(version)
if err != nil {
return nil, err
}
acc, err := c.namespace.GetArtifact(tag)
if err == nil {
if override {
return newComponentVersionAccess(accessobj.ACC_CREATE, c, version, acc, false)
}
return nil, errors.ErrAlreadyExists(cpi.KIND_COMPONENTVERSION, c.name+"/"+version)
}
if !errors.IsErrNotFoundKind(err, oci.KIND_OCIARTIFACT) {
return nil, err
}
acc, err = c.namespace.NewArtifact()
if err != nil {
return nil, err
}
return newComponentVersionAccess(accessobj.ACC_CREATE, c, version, acc, false)
}