-
Notifications
You must be signed in to change notification settings - Fork 18
/
builder.go
212 lines (179 loc) · 5.25 KB
/
builder.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package builder
import (
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/modern-go/reflect2"
"github.com/onsi/ginkgo/v2"
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/env"
"github.com/open-component-model/ocm/pkg/exception"
"github.com/open-component-model/ocm/pkg/utils"
)
type element interface {
SetBuilder(b *Builder)
Type() string
Close() error
Set()
Result() interface{}
}
type State struct{}
type base struct {
*Builder
result interface{}
}
func (e *base) SetBuilder(b *Builder) {
e.Builder = b
}
func (e *base) Result() interface{} {
return e.result
}
type static struct {
def_modopts ocm.ModificationOptions
}
type state struct {
*static
ocm_repo ocm.Repository
ocm_comp ocm.ComponentAccess
ocm_vers ocm.ComponentVersionAccess
ocm_rsc *compdesc.ResourceMeta
ocm_src *compdesc.SourceMeta
ocm_meta *compdesc.ElementMeta
ocm_labels *metav1.Labels
ocm_acc *compdesc.AccessSpec
ocm_modopts *ocm.ModificationOptions
blob *blobaccess.BlobAccess
hint *string
oci_repo oci.Repository
oci_nsacc oci.NamespaceAccess
oci_artacc oci.ArtifactAccess
oci_cleanuplayers bool
oci_tags *[]string
oci_artfunc func(oci.ArtifactAccess) error
oci_annofunc func(name, value string)
}
type Builder struct {
*env.Environment
stack []element
state
}
// New creates a new composition environment
// including an own OCM context and a private
// filesystem, which can be used to compose
// OCM/OCI repositories and their content.
// It can be configured to work with dedicated
// settings, also.
func New(opts ...env.Option) *Builder {
return &Builder{Environment: env.NewEnvironment(append([]env.Option{env.FileSystem(osfs.OsFs, "/"), env.FailHandler(env.ExceptionFailHandler)}, opts...)...), state: state{static: &static{}}}
}
// NewBuilder creates a new composition environment
// including an own OCM context and a private
// filesystem, which can be used to compose
// OCM/OCI repositories and their content.
// By default, a private environment is created based on
// a ginko fail handling intended to be used for test cases.
// But it can be configured to work as library with dedicated
// settings, also.
func NewBuilder(opts ...env.Option) *Builder {
return &Builder{Environment: env.NewEnvironment(append([]env.Option{env.FailHandler(ginkgo.Fail)}, opts...)...), state: state{static: &static{}}}
}
var _ accessio.Option = (*Builder)(nil)
// Build executes the given functions and returns a potential configuration
// error, instead of using the builder's env.FailHandler.
// Additionally, a build can always throw an exception using
// the exception.Throw function.
func (b *Builder) Build(funcs ...func(*Builder)) (err error) {
old := b.GetFailHandler()
defer func() {
b.SetFailHandler(old)
}()
b.SetFailHandler(env.ExceptionFailHandler)
defer exception.PropagateException(&err)
for _, f := range funcs {
f(b)
}
return nil
}
func (b *Builder) SetFailhandler(h ...env.FailHandler) *Builder {
b.Environment.SetFailHandler(h...)
return b
}
// PropagateError can be used in defer to convert an composition error
// into an error return.
func (b *Builder) PropagateError(errp *error, matchers ...exception.Matcher) {
if r := recover(); r != nil {
*errp = exception.FilterException(r, matchers...)
}
}
func (b *Builder) set() {
b.state = state{static: b.state.static}
if len(b.stack) > 0 {
b.peek().Set()
}
}
func (b *Builder) expect(p interface{}, msg string, tests ...func() bool) {
if reflect2.IsNil(p) {
b.fail(msg+" required", 1)
}
for _, f := range tests {
if !f() {
b.fail(msg+" required", 1)
}
}
}
func (b *Builder) fail(msg string, callerSkip ...int) {
b.Fail(msg, utils.Optional(callerSkip...)+2)
}
func (b *Builder) failOn(err error, callerSkip ...int) {
b.FailOnErr(err, "", utils.Optional(callerSkip...)+2)
}
func (b *Builder) peek() element {
if len(b.stack) == 0 {
b.fail("no open frame", 2)
}
return b.stack[len(b.stack)-1]
}
func (b *Builder) pop() element {
if len(b.stack) == 0 {
b.fail("no open frame", 2)
}
e := b.stack[len(b.stack)-1]
b.stack = b.stack[:len(b.stack)-1]
b.set()
return e
}
func (b *Builder) push(e element) {
b.stack = append(b.stack, e)
b.set()
}
func (b *Builder) configure(e element, funcs []func(), skip ...int) interface{} {
e.SetBuilder(b)
b.push(e)
b.Configure(funcs...)
err := b.pop().Close()
if err != nil {
b.fail(err.Error(), utils.Optional(skip...)+1)
}
return e.Result()
}
func (b *Builder) Configure(funcs ...func()) {
for _, f := range funcs {
if f != nil {
f()
}
}
}
////////////////////////////////////////////////////////////////////////////////
func (b *Builder) Hint(hint string) {
b.expect(b.hint, T_OCMACCESS)
if b.ocm_acc != nil && *b.ocm_acc != nil {
b.fail("access already set")
}
*(b.hint) = hint
}