-
Notifications
You must be signed in to change notification settings - Fork 18
/
options.go
90 lines (71 loc) · 1.64 KB
/
options.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
package ociartifact
import (
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/oci"
"github.com/open-component-model/ocm/pkg/optionutils"
)
type Option = optionutils.Option[*Options]
type Options struct {
Context oci.Context
Version string
Printer common.Printer
}
func (o *Options) OCIContext() oci.Context {
if o.Context == nil {
return oci.DefaultContext()
}
return o.Context
}
func (o *Options) GetPrinter() common.Printer {
if o.Printer == nil {
return common.NewPrinter(nil)
}
return o.Printer
}
func (o *Options) Printf(msg string, args ...interface{}) {
if o.Printer != nil {
o.Printer.Printf(msg, args...)
}
}
func (o *Options) ApplyTo(opts *Options) {
if opts == nil {
return
}
if o.Context != nil {
opts.Context = o.Context
}
if o.Version != "" {
opts.Version = o.Version
}
if o.Printer != nil {
opts.Printer = o.Printer
}
}
////////////////////////////////////////////////////////////////////////////////
type context struct {
oci.Context
}
func (o context) ApplyTo(opts *Options) {
opts.Context = o
}
func WithContext(ctx oci.ContextProvider) Option {
return context{ctx.OCIContext()}
}
////////////////////////////////////////////////////////////////////////////////
type version string
func (o version) ApplyTo(opts *Options) {
opts.Version = string(o)
}
func WithVersion(v string) Option {
return version(v)
}
////////////////////////////////////////////////////////////////////////////////
type printer struct {
common.Printer
}
func (o printer) ApplyTo(opts *Options) {
opts.Printer = o
}
func WithPrinter(p common.Printer) Option {
return printer{p}
}