-
Notifications
You must be signed in to change notification settings - Fork 18
/
builder.go
88 lines (72 loc) · 1.86 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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"context"
"io"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/v2/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm"
"github.com/open-component-model/ocm/v2/pkg/out"
)
type Builder struct {
ctx context.Context
shared datacontext.AttributesContext
ocm ocm.Context
out out.Context
filesystem vfs.FileSystem
}
func (b *Builder) getContext() context.Context {
if b.ctx == nil {
return context.Background()
}
return b.ctx
}
func (b Builder) WithContext(ctx context.Context) Builder {
b.ctx = ctx
return b
}
func (b Builder) WithFileSystem(fs vfs.FileSystem) Builder {
b.filesystem = fs
return b
}
func (b Builder) WithSharedAttributes(ctx datacontext.AttributesContext) Builder {
b.shared = ctx
return b
}
func (b Builder) WithOCM(ctx ocm.Context) Builder {
b.ocm = ctx
return b
}
func (b Builder) WithOutput(w io.Writer) Builder {
b.out = out.WithOutput(b.out, w)
return b
}
func (b Builder) WithErrorOutput(w io.Writer) Builder {
b.out = out.WithErrorOutput(b.out, w)
return b
}
func (b Builder) WithInput(r io.Reader) Builder {
b.out = out.WithInput(b.out, r)
return b
}
func (b Builder) Bound() (Context, context.Context) {
c := b.New()
return c, context.WithValue(b.getContext(), key, c)
}
func (b Builder) New(m ...datacontext.BuilderMode) Context {
mode := datacontext.Mode(m...)
ctx := b.getContext()
if b.ocm == nil {
var ok bool
b.ocm, ok = ocm.DefinedForContext(ctx)
if !ok && mode != datacontext.MODE_SHARED {
b.ocm = ocm.New(mode)
}
}
if b.shared == nil {
b.shared = b.ocm.AttributesContext()
}
return newContext(b.shared, b.ocm, out.NewFor(b.out), b.filesystem, b.shared)
}