-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclientoptions.go
More file actions
246 lines (209 loc) · 8.17 KB
/
clientoptions.go
File metadata and controls
246 lines (209 loc) · 8.17 KB
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
245
246
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"
)
// ConnectionFactory can create connections. When a connection is not given, the factory is used
// to build a connection.
type ConnectionFactory interface {
fmt.Stringer
Connection() (protocol.Connection, error)
}
func defaultConnectionFactory() ConnectionFactory {
return &CompositeConfig{}
}
// ClientOptions is a struct that holds the variadic options for the rig package.
type ClientOptions struct {
log.LoggerInjectable
connection protocol.Connection
connectionFactory ConnectionFactory
runner cmd.Runner
retryConnection bool
providersContainer
}
type providersContainer struct {
packageManagerProviderConfig
initSystemProviderConfig
remoteFSProviderConfig
osReleaseProviderConfig
sudoProviderConfig
}
type packageManagerProviderConfig struct {
provider packagemanager.ManagerProvider
}
func (p *packageManagerProviderConfig) GetPackageManagerProvider(runner cmd.Runner) *packagemanager.Provider {
return packagemanager.NewPackageManagerProvider(p.provider, runner)
}
type initSystemProviderConfig struct {
provider initsystem.ServiceManagerProvider
}
func (p *initSystemProviderConfig) GetInitSystemProvider(runner cmd.Runner) *initsystem.Provider {
return initsystem.NewInitSystemProvider(p.provider, runner)
}
type remoteFSProviderConfig struct {
provider remotefs.FSProvider
}
func (p *remoteFSProviderConfig) GetRemoteFSProvider(runner cmd.Runner) *remotefs.Provider {
return remotefs.NewRemoteFSProvider(p.provider, runner)
}
type osReleaseProviderConfig struct {
provider os.ReleaseProvider
}
func (p *osReleaseProviderConfig) GetOSReleaseProvider(runner cmd.Runner) *os.Provider {
return os.NewOSReleaseProvider(p.provider, runner)
}
type sudoProviderConfig struct {
provider sudo.RunnerProvider
}
func (p *sudoProviderConfig) GetSudoProvider(runner cmd.Runner) *sudo.Provider {
return sudo.NewSudoProvider(p.provider, runner)
}
func defaultProviders() providersContainer {
return providersContainer{
packageManagerProviderConfig: packageManagerProviderConfig{provider: packagemanager.DefaultRegistry().Get},
initSystemProviderConfig: initSystemProviderConfig{provider: initsystem.DefaultRegistry().Get},
remoteFSProviderConfig: remoteFSProviderConfig{provider: remotefs.DefaultRegistry().Get},
osReleaseProviderConfig: osReleaseProviderConfig{provider: os.DefaultRegistry().Get},
sudoProviderConfig: sudoProviderConfig{provider: sudo.DefaultRegistry().Get},
}
}
// 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.connectionFactory == nil {
return fmt.Errorf("%w: no connection or connection factory provided", protocol.ErrValidationFailed)
}
return nil
}
// Clone returns a copy of the Options struct.
func (o *ClientOptions) Clone() *ClientOptions {
return &ClientOptions{
LoggerInjectable: o.LoggerInjectable,
connection: o.connection,
connectionFactory: o.connectionFactory,
runner: o.runner,
retryConnection: o.retryConnection,
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 ConnectionFactory 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.connectionFactory == nil {
return nil, fmt.Errorf("%w: no connection or connection factory provided", protocol.ErrNonRetryable)
}
o.Log().Debug("using connection factory to setup a connection", log.HostAttr(o.connectionFactory), log.KeyComponent, "clientoptions")
c, err := o.connectionFactory.Connection()
if err != nil {
return nil, fmt.Errorf("create connection: %w", err)
}
o.Log().Debug("using connection received from connection factory", 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 ConnectionFactory.
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
}
}
// WithConnectionFactory is a functional option that sets the connection factory to use for connecting.
func WithConnectionFactory(factory ConnectionFactory) ClientOption {
return func(o *ClientOptions) {
o.connectionFactory = factory
}
}
// WithRemoteFSProvider is a functional option that sets the filesystem provider to use for the connection's RemoteFSProvider.
func WithRemoteFSProvider(provider remotefs.FSProvider) ClientOption {
return func(o *ClientOptions) {
o.remoteFSProviderConfig = remoteFSProviderConfig{provider: provider}
}
}
// WithInitSystemProvider is a functional option that sets the init system provider to use for the connection's InitSystemProvider.
func WithInitSystemProvider(provider initsystem.ServiceManagerProvider) ClientOption {
return func(o *ClientOptions) {
o.initSystemProviderConfig = initSystemProviderConfig{provider: provider}
}
}
// WithOSReleaseProvider is a functional option that sets the os release provider to use for the connection's OSReleaseProvider.
func WithOSReleaseProvider(provider os.ReleaseProvider) ClientOption {
return func(o *ClientOptions) {
o.osReleaseProviderConfig = osReleaseProviderConfig{provider: provider}
}
}
// WithPackageManagerProvider is a functional option that sets the package manager provider to use for the connection's PackageManagerProvider.
func WithPackageManagerProvider(provider packagemanager.ManagerProvider) ClientOption {
return func(o *ClientOptions) {
o.packageManagerProviderConfig = packageManagerProviderConfig{provider: provider}
}
}
// WithSudoProvider is a functional option that sets the sudo provider to use for the connection's SudoProvider.
func WithSudoProvider(provider sudo.RunnerProvider) ClientOption {
return func(o *ClientOptions) {
o.sudoProviderConfig = sudoProviderConfig{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{
connectionFactory: defaultConnectionFactory(),
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
}