-
Notifications
You must be signed in to change notification settings - Fork 23
/
type.go
74 lines (63 loc) · 2.18 KB
/
type.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package dockerconfig
import (
"encoding/json"
"fmt"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/runtime"
)
const (
Type = "DockerConfig"
TypeV1 = Type + runtime.VersionSeparator + "v1"
)
func init() {
cpi.RegisterRepositoryType(Type, cpi.NewRepositoryType(Type, &RepositorySpec{}))
cpi.RegisterRepositoryType(TypeV1, cpi.NewRepositoryType(TypeV1, &RepositorySpec{}))
}
// RepositorySpec describes a docker config based credential repository interface.
type RepositorySpec struct {
runtime.ObjectVersionedType `json:",inline"`
DockerConfigFile string `json:"dockerConfigFile,omitempty"`
DockerConfig json.RawMessage `json:"dockerConfig,omitempty"`
PropgateConsumerIdentity bool `json:"propagateConsumerIdentity,omitempty"`
}
func (s RepositorySpec) WithConsumerPropagation(propagate bool) *RepositorySpec {
s.PropgateConsumerIdentity = propagate
return &s
}
// NewRepositorySpec creates a new memory RepositorySpec.
func NewRepositorySpec(path string, prop ...bool) *RepositorySpec {
p := false
for _, e := range prop {
p = p || e
}
return &RepositorySpec{
ObjectVersionedType: runtime.NewVersionedObjectType(Type),
DockerConfigFile: path,
PropgateConsumerIdentity: p,
}
}
func NewRepositorySpecForConfig(data []byte, prop ...bool) *RepositorySpec {
p := false
for _, e := range prop {
p = p || e
}
return &RepositorySpec{
ObjectVersionedType: runtime.NewVersionedObjectType(Type),
DockerConfig: data,
PropgateConsumerIdentity: p,
}
}
func (a *RepositorySpec) GetType() string {
return Type
}
func (a *RepositorySpec) Repository(ctx cpi.Context, creds cpi.Credentials) (cpi.Repository, error) {
r := ctx.GetAttributes().GetOrCreateAttribute(ATTR_REPOS, newRepositories)
repos, ok := r.(*Repositories)
if !ok {
return nil, fmt.Errorf("failed to assert type %T to Repositories", r)
}
return repos.GetRepository(ctx, a.DockerConfigFile, a.DockerConfig, a.PropgateConsumerIdentity)
}