-
Notifications
You must be signed in to change notification settings - Fork 18
/
configoptions.go
168 lines (141 loc) · 3.43 KB
/
configoptions.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package flagsets
import (
"fmt"
"sort"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"
)
type Option interface {
GetName() string
AddFlags(fs *pflag.FlagSet)
Value() interface{}
Changed() bool
AddGroups(groups ...string)
}
type Filter func(name string) bool
// ConfigOptions is a set of arbitrary command line options.
// This set can be added to a pflag.FlagSet. After evaluation of
// the flag set against a set of arguments it provides
// information about the actual value and the changed state.
type ConfigOptions interface {
AddTypeSetGroupsToOptions(set ConfigOptionTypeSet)
AddFlags(fs *pflag.FlagSet)
Options() []Option
Names() []string
Size() int
HasOption(name string) bool
Check(set ConfigOptionTypeSet, desc string) error
GetValue(name string) (interface{}, bool)
Changed(names ...string) bool
FilterBy(Filter) ConfigOptions
}
type configOptions struct {
options []Option
flags *pflag.FlagSet
}
func NewOptions(opts []Option) ConfigOptions {
return &configOptions{options: opts}
}
func (o *configOptions) AddTypeSetGroupsToOptions(set ConfigOptionTypeSet) {
for _, opt := range o.options {
set.AddGroupsToOption(opt)
}
}
func (o *configOptions) Options() []Option {
return slices.Clone(o.options)
}
func (o *configOptions) Names() []string {
var keys []string
for _, e := range o.options {
keys = append(keys, e.GetName())
}
sort.Strings(keys)
return keys
}
func (o *configOptions) HasOption(name string) bool {
for _, e := range o.options {
if e.GetName() == name {
return true
}
}
return false
}
func (o *configOptions) Size() int {
return len(o.options)
}
func (o *configOptions) GetValue(name string) (interface{}, bool) {
for _, opt := range o.options {
if opt.GetName() == name {
return opt.Value(), o.flags.Changed(name)
}
}
return nil, false
}
func (o *configOptions) AddFlags(fs *pflag.FlagSet) {
for _, opt := range o.options {
opt.AddFlags(fs)
}
o.flags = fs
}
func (o *configOptions) Changed(names ...string) bool {
if len(names) == 0 {
for _, opt := range o.options {
if o.flags.Changed(opt.GetName()) {
return true
}
}
return false
}
set := map[string]struct{}{}
for _, n := range names {
set[n] = struct{}{}
}
for _, opt := range o.options {
if _, ok := set[opt.GetName()]; ok {
if o.flags.Changed(opt.GetName()) {
return true
}
}
}
return false
}
func (o *configOptions) FilterBy(filter Filter) ConfigOptions {
if filter == nil {
return o
}
var options []Option
for _, opt := range o.options {
if filter(opt.GetName()) {
options = append(options, opt)
}
}
return &configOptions{
options: options,
flags: o.flags,
}
}
func (o *configOptions) Check(set ConfigOptionTypeSet, desc string) error {
if desc != "" {
desc = " for " + desc
}
if set == nil {
for _, opt := range o.options {
if o.flags.Changed(opt.GetName()) {
return fmt.Errorf("option %q given, but not possible%s", opt.GetName(), desc)
}
}
} else {
for _, opt := range o.options {
if o.flags.Changed(opt.GetName()) && set.GetOptionType(opt.GetName()) == nil {
if desc == "" {
return fmt.Errorf("option %q given, but not valid for option set %q", opt.GetName(), set.GetName())
}
return fmt.Errorf("option %q given, but not possible%s", opt.GetName(), desc)
}
}
}
return nil
}