-
Notifications
You must be signed in to change notification settings - Fork 25
/
clientoptions.go
244 lines (207 loc) · 8.04 KB
/
clientoptions.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package rig
import (
"fmt"
"github.com/k0sproject/rig/v2/cmd"
"github.com/k0sproject/rig/v2/initsystem"
"github.com/k0sproject/rig/v2/log"
"github.com/k0sproject/rig/v2/os"
"github.com/k0sproject/rig/v2/packagemanager"
"github.com/k0sproject/rig/v2/protocol"
"github.com/k0sproject/rig/v2/remotefs"
"github.com/k0sproject/rig/v2/sudo"
)
// ConnectionConfigurer can create connections. When a connection is not given, the configurer is used
// to build a connection.
type ConnectionConfigurer interface {
fmt.Stringer
Connection() (protocol.Connection, error)
}
func defaultConnectionConfigurer() ConnectionConfigurer {
return &CompositeConfig{}
}
// ClientOptions is a struct that holds the variadic options for the rig package.
type ClientOptions struct {
log.LoggerInjectable
connection protocol.Connection
connectionConfigurer ConnectionConfigurer
runner cmd.Runner
retryConnection bool
providersContainer
}
type providersContainer struct {
packageManagerProvider
initSystemProvider
remoteFSProvider
osReleaseProvider
sudoProvider
}
type packageManagerProvider struct {
provider packagemanager.PackageManagerProvider
}
func (p *packageManagerProvider) GetPackageManagerService(runner cmd.Runner) *packagemanager.Service {
return packagemanager.NewPackageManagerService(p.provider, runner)
}
type initSystemProvider struct {
provider initsystem.InitSystemProvider
}
func (p *initSystemProvider) GetInitSystemService(runner cmd.Runner) *initsystem.Service {
return initsystem.NewInitSystemService(p.provider, runner)
}
type remoteFSProvider struct {
provider remotefs.RemoteFSProvider
}
func (p *remoteFSProvider) GetRemoteFSService(runner cmd.Runner) *remotefs.Service {
return remotefs.NewRemoteFSService(p.provider, runner)
}
type osReleaseProvider struct {
provider os.OSReleaseProvider
}
func (p *osReleaseProvider) GetOSReleaseService(runner cmd.Runner) *os.Service {
return os.NewOSReleaseService(p.provider, runner)
}
type sudoProvider struct {
provider sudo.SudoProvider
}
func (p *sudoProvider) GetSudoService(runner cmd.Runner) *sudo.Service {
return sudo.NewSudoService(p.provider, runner)
}
func defaultProviders() providersContainer {
return providersContainer{
packageManagerProvider: packageManagerProvider{provider: packagemanager.DefaultProvider()},
initSystemProvider: initSystemProvider{provider: initsystem.DefaultProvider()},
remoteFSProvider: remoteFSProvider{provider: remotefs.DefaultProvider()},
osReleaseProvider: osReleaseProvider{provider: os.DefaultProvider()},
sudoProvider: sudoProvider{provider: sudo.DefaultProvider()},
}
}
// Apply applies the supplied options to the Options struct.
func (o *ClientOptions) Apply(opts ...ClientOption) {
for _, opt := range opts {
opt(o)
}
}
// Validate the options.
func (o *ClientOptions) Validate() error {
if o.connection == nil && o.connectionConfigurer == nil {
return fmt.Errorf("%w: no connection or connection configurer provided", protocol.ErrValidationFailed)
}
return nil
}
// Clone returns a copy of the Options struct.
func (o *ClientOptions) Clone() *ClientOptions {
return &ClientOptions{
connection: o.connection,
connectionConfigurer: o.connectionConfigurer,
runner: o.runner,
providersContainer: o.providersContainer,
}
}
// ShouldRetry returns whether the connection should be retried.
func (o *ClientOptions) ShouldRetry() bool {
return o.retryConnection
}
// GetConnection returns the connection to use for the rig client. If no connection is set, it will use the ConnectionConfigurer to create one.
func (o *ClientOptions) GetConnection() (protocol.Connection, error) {
var conn protocol.Connection
if o.connection != nil {
o.Log().Debug("using provided connection", log.HostAttr(o.connection), log.KeyComponent, "clientoptions")
conn = o.connection
} else {
if o.connectionConfigurer == nil {
return nil, fmt.Errorf("%w: no connection or connection configurer provided", protocol.ErrAbort)
}
o.Log().Debug("using client configurer to setup a connection", log.HostAttr(o.connectionConfigurer), log.KeyComponent, "clientoptions")
c, err := o.connectionConfigurer.Connection()
if err != nil {
return nil, fmt.Errorf("create connection: %w", err)
}
o.Log().Debug("using connection received from client configurer", log.HostAttr(c), log.KeyComponent, "clientoptions")
conn = c
}
log.InjectLogger(log.WithAttrs(o.Log(), log.HostAttr(conn), log.KeyProtocol, conn.Protocol()), conn)
return conn, nil
}
// GetRunner returns the runner to use for the rig client.
func (o *ClientOptions) GetRunner(conn protocol.Connection) cmd.Runner {
if o.runner != nil {
return o.runner
}
runner := cmd.NewExecutor(conn)
return runner
}
// ClientOption is a functional option type for the Options struct.
type ClientOption func(*ClientOptions)
// WithLogger is a functional option that sets the logger to use for the connection and its child components.
func WithLogger(logger log.Logger) ClientOption {
return func(o *ClientOptions) {
o.SetLogger(logger)
}
}
// WithConnection is a functional option that sets the client to use for connecting instead of getting it from the ConnectionConfigurer.
func WithConnection(conn protocol.Connection) ClientOption {
return func(o *ClientOptions) {
o.connection = conn
}
}
// WithRunner is a functional option that sets the runner to use for executing commands.
func WithRunner(runner cmd.Runner) ClientOption {
return func(o *ClientOptions) {
o.runner = runner
}
}
// WithConnectionConfigurer is a functional option that sets the client configurer to use for connecting.
func WithConnectionConfigurer(configurer ConnectionConfigurer) ClientOption {
return func(o *ClientOptions) {
o.connectionConfigurer = configurer
}
}
// WithRemoteFSProvider is a functional option that sets the filesystem provider to use for the connection's RemoteFSService.
func WithRemoteFSProvider(provider remotefs.RemoteFSProvider) ClientOption {
return func(o *ClientOptions) {
o.providersContainer.remoteFSProvider = remoteFSProvider{provider: provider}
}
}
// WithInitSystemProvider is a functional option that sets the init system provider to use for the connection's InitSystemService.
func WithInitSystemProvider(provider initsystem.InitSystemProvider) ClientOption {
return func(o *ClientOptions) {
o.providersContainer.initSystemProvider = initSystemProvider{provider: provider}
}
}
// WithOSReleaseProvider is a functional option that sets the os release provider to use for the connection's OSReleaseService.
func WithOSReleaseProvider(provider os.OSReleaseProvider) ClientOption {
return func(o *ClientOptions) {
o.providersContainer.osReleaseProvider = osReleaseProvider{provider: provider}
}
}
// WithPackageManagerProvider is a functional option that sets the package manager provider to use for the connection's PackageManagerService.
func WithPackageManagerProvider(provider packagemanager.PackageManagerProvider) ClientOption {
return func(o *ClientOptions) {
o.providersContainer.packageManagerProvider = packageManagerProvider{provider: provider}
}
}
// WithSudoProvider is a functional option that sets the sudo provider to use for the connection's SudoService.
func WithSudoProvider(provider sudo.SudoProvider) ClientOption {
return func(o *ClientOptions) {
o.providersContainer.sudoProvider = sudoProvider{provider: provider}
}
}
// WithRetry is a functional option that toggles the connection retry feature. Default is true.
func WithRetry(retry bool) ClientOption {
return func(o *ClientOptions) {
o.retryConnection = retry
}
}
// DefaultClientOptions returns a new Options struct with the default options applied.
func DefaultClientOptions() *ClientOptions {
return &ClientOptions{
connectionConfigurer: defaultConnectionConfigurer(),
providersContainer: defaultProviders(),
retryConnection: true,
}
}
// NewClientOptions creates a new Options struct with the supplied options applied over the defaults.
func NewClientOptions(opts ...ClientOption) *ClientOptions {
options := DefaultClientOptions()
options.Apply(opts...)
return options
}