-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
159 lines (115 loc) · 2.11 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
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
package rac
import (
"errors"
"time"
)
type ManagerOptions struct {
V8Version string
RacPath string
UpdateInterval time.Duration
Timeout time.Duration
TryTimeoutCount int
DetectCluster bool
ClusterAuth struct {
User string
Pwd string
}
Logger Logger
Runner Runner
}
type Option func(o *Options)
type Options struct {
Cluster string
Process string
Infobase string
InfobaseAuth *Auth
ClusterAuth *Auth
}
func (o *Options) Options(opts ...interface{}) (err error) {
defer func() {
if e := recover(); e != nil {
switch errT := e.(type) {
case error:
err = errT
case string:
err = errors.New(errT)
default:
panic(e)
}
}
}()
for _, opt := range opts {
optFn, ok := opt.(Option)
if ok {
optFn(o)
}
}
return
}
func (o *Options) copy() *Options {
newO := *o
return &newO
}
func (o *Options) Values() map[string]string {
values := make(map[string]string)
if o.InfobaseAuth != nil {
values["--infobase-user"] = o.InfobaseAuth.User
values["--infobase-pwd"] = o.InfobaseAuth.Pwd
}
if o.ClusterAuth != nil {
values["--cluster-user"] = o.ClusterAuth.User
values["--cluster-pwd"] = o.ClusterAuth.Pwd
}
if len(o.Cluster) > 0 {
values["--cluster"] = o.Cluster
}
if len(o.Process) > 0 {
values["--process"] = o.Process
}
return values
}
func WithInfobaseAuth(user, pwd string) Option {
return func(o *Options) {
if len(user) == 0 {
return
}
o.InfobaseAuth = &Auth{
User: user,
Pwd: pwd,
}
}
}
func WithInfobase(infobase interface{}) Option {
return func(o *Options) {
switch i := infobase.(type) {
case InfobaseSig:
o.Infobase = i.InfobaseSig()
case string:
if len(i) == 0 {
return
}
o.Infobase = i
default:
panic(errors.New("unsupported infobase type"))
}
}
}
func WithProcess(process string) Option {
return func(o *Options) {
if len(process) == 0 {
return
}
o.Process = process
}
}
func WithAuth(user, pwd string) Option {
return func(o *Options) {
if len(user) == 0 {
return
}
o.ClusterAuth = &Auth{
User: user,
Pwd: pwd,
}
}
}