-
Notifications
You must be signed in to change notification settings - Fork 18
/
type.go
104 lines (85 loc) · 2.85 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
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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
"github.com/open-component-model/ocm/pkg/contexts/clictx/internal"
"github.com/open-component-model/ocm/pkg/contexts/config"
"github.com/open-component-model/ocm/pkg/contexts/config/cpi"
ocicpi "github.com/open-component-model/ocm/pkg/contexts/oci/cpi"
ocmcpi "github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/runtime"
)
const (
OCMCmdConfigType = "ocm.cmd" + cpi.OCM_CONFIG_TYPE_SUFFIX
OCMCmdConfigTypeV1 = OCMCmdConfigType + runtime.VersionSeparator + "v1"
)
func init() {
cpi.RegisterConfigType(cpi.NewConfigType[*Config](OCMCmdConfigType, usage))
cpi.RegisterConfigType(cpi.NewConfigType[*Config](OCMCmdConfigTypeV1, usage))
}
// Config describes a memory based repository interface.
type Config struct {
runtime.ObjectVersionedType `json:",inline"`
OCMRepositories map[string]*ocmcpi.GenericRepositorySpec `json:"ocmRepositories,omitempty"`
OCIRepositories map[string]*ocicpi.GenericRepositorySpec `json:"ociRepositories,omitempty"`
}
// New creates a new memory ConfigSpec.
func New() *Config {
return &Config{
ObjectVersionedType: runtime.NewVersionedTypedObject(OCMCmdConfigType),
}
}
func (a *Config) GetType() string {
return OCMCmdConfigType
}
func (a *Config) AddOCIRepository(name string, spec ocicpi.RepositorySpec) error {
g, err := ocicpi.ToGenericRepositorySpec(spec)
if err != nil {
return fmt.Errorf("unable to convert oci repository spec to generic spec: %w", err)
}
if a.OCIRepositories == nil {
a.OCIRepositories = map[string]*ocicpi.GenericRepositorySpec{}
}
a.OCIRepositories[name] = g
return nil
}
func (a *Config) AddOCMRepository(name string, spec ocmcpi.RepositorySpec) error {
g, err := ocmcpi.ToGenericRepositorySpec(spec)
if err != nil {
return fmt.Errorf("unable to convert ocm repository spec to generic spec: %w", err)
}
if a.OCMRepositories == nil {
a.OCMRepositories = map[string]*ocmcpi.GenericRepositorySpec{}
}
a.OCMRepositories[name] = g
return nil
}
func (a *Config) ApplyTo(ctx config.Context, target interface{}) error {
t, ok := target.(internal.Context)
if !ok {
return config.ErrNoContext(OCMCmdConfigType)
}
for n, s := range a.OCIRepositories {
t.OCI().Context().SetAlias(n, s)
}
for n, s := range a.OCMRepositories {
t.OCM().Context().SetAlias(n, s)
}
return nil
}
const usage = `
The config type <code>` + OCMCmdConfigType + `</code> can be used to
configure predefined aliases for dedicated OCM repositories and
OCI registries.
<pre>
type: ` + OCMCmdConfigType + `
ocmRepositories:
<name>: <specification of OCM repository>
...
ociRepositories:
<name>: <specification of OCI registry>
...
</pre>
`