-
Notifications
You must be signed in to change notification settings - Fork 18
/
interfaces.go
211 lines (176 loc) · 4.14 KB
/
interfaces.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package options
import (
"reflect"
"github.com/spf13/pflag"
"github.com/open-component-model/ocm/v2/pkg/contexts/clictx"
"github.com/open-component-model/ocm/v2/pkg/out"
)
type OptionsProcessor func(Options) error
type SimpleOptionCompleter interface {
Complete() error
}
type OptionWithOutputContextCompleter interface {
Complete(ctx out.Context) error
}
type OptionWithCLIContextCompleter interface {
Configure(ctx clictx.Context) error
}
type Usage interface {
Usage() string
}
type Options interface {
AddFlags(fs *pflag.FlagSet)
}
////////////////////////////////////////////////////////////////////////////////
type OptionSelector func(provider OptionSetProvider) bool
func Not(s OptionSelector) OptionSelector {
return func(provider OptionSetProvider) bool {
return !s(provider)
}
}
func Always() OptionSelector {
return func(provider OptionSetProvider) bool {
return true
}
}
func Never() OptionSelector {
return func(provider OptionSetProvider) bool {
return false
}
}
////////////////////////////////////////////////////////////////////////////////
type OptionSet []Options
type OptionSetProvider interface {
AsOptionSet() OptionSet
}
func (s OptionSet) AddFlags(fs *pflag.FlagSet) {
for _, o := range s {
o.AddFlags(fs)
}
}
func (s OptionSet) AsOptionSet() OptionSet {
return s
}
func (s OptionSet) Usage() string {
u := ""
for _, n := range s {
if c, ok := n.(Usage); ok {
u += "\n" + c.Usage()
}
}
return u
}
func (s OptionSet) Options(proto Options) interface{} {
t := reflect.TypeOf(proto)
for _, o := range s {
if reflect.TypeOf(o) == t {
return o
}
if set, ok := o.(OptionSetProvider); ok {
r := set.AsOptionSet().Options(proto)
if r != nil {
return r
}
}
}
return nil
}
// Get extracts the option for a given target. This might be a
// - pointer to a struct implementing the Options interface which
// will fill the struct with a copy of the options OR
// - a pointer to such a pointer which will be filled with the
// pointer to the actual member of the OptionSet.
func (s OptionSet) Get(proto interface{}) bool {
val := true
t := reflect.TypeOf(proto)
if t.Elem().Kind() == reflect.Ptr {
t = t.Elem()
val = false
}
for _, o := range s {
if reflect.TypeOf(o) == t {
if val {
reflect.ValueOf(proto).Elem().Set(reflect.ValueOf(o).Elem())
} else {
reflect.ValueOf(proto).Elem().Set(reflect.ValueOf(o))
}
return true
}
if set, ok := o.(OptionSetProvider); ok {
r := set.AsOptionSet().Get(proto)
if r {
return r
}
}
}
return false
}
func (s OptionSet) ProcessOnOptions(f OptionsProcessor) error {
for _, n := range s {
var err error
if set, ok := n.(OptionSetProvider); ok {
err = set.AsOptionSet().ProcessOnOptions(f)
if err != nil {
return err
}
}
err = f(n)
if err != nil {
return err
}
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
func CompleteOptions(opt Options) error {
if c, ok := opt.(SimpleOptionCompleter); ok {
return c.Complete()
}
return nil
}
func CompleteOptionsWithCLIContext(ctx clictx.Context) OptionsProcessor {
return func(opt Options) error {
if c, ok := opt.(OptionWithCLIContextCompleter); ok {
return c.Configure(ctx)
}
if c, ok := opt.(OptionWithOutputContextCompleter); ok {
return c.Complete(ctx)
}
return CompleteOptions(opt)
}
}
////////////////////////////////////////////////////////////////////////////////
type Condition interface {
IsTrue() bool
}
type ConditionFunction func() bool
func (f ConditionFunction) IsTrue() bool {
return f()
}
type Flag bool
func (f Flag) IsTrue() bool {
return bool(f)
}
func Or(conditions ...Condition) Condition {
return ConditionFunction(func() bool {
for _, c := range conditions {
if c.IsTrue() {
return true
}
}
return false
})
}
func And(conditions ...Condition) Condition {
return ConditionFunction(func() bool {
for _, c := range conditions {
if !c.IsTrue() {
return false
}
}
return true
})
}