/
factory.go
50 lines (40 loc) · 1.25 KB
/
factory.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
// Copyright 2023 lichangao(李长傲) <changao.li.work@outlook.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/changaolee/skeleton.
package util
import (
"github.com/changaolee/skeleton/internal/pkg/client"
"github.com/changaolee/skeleton/internal/pkg/clioptions"
"github.com/changaolee/skeleton/internal/pkg/rest"
)
type Factory interface {
clioptions.RESTClientGetter
RESTClient() (*rest.RESTClient, error)
}
type factory struct {
clientGetter clioptions.RESTClientGetter
}
func NewFactory(clientGetter clioptions.RESTClientGetter) Factory {
if clientGetter == nil {
panic("attempt to instantiate factory with nil clientGetter")
}
f := &factory{
clientGetter: clientGetter,
}
return f
}
func (f *factory) ToRESTConfig() (*rest.Config, error) {
return f.clientGetter.ToRESTConfig()
}
func (f *factory) ToRawSKTConfigLoader() client.ClientConfig {
return f.clientGetter.ToRawSKTConfigLoader()
}
func (f *factory) RESTClient() (*rest.RESTClient, error) {
clientConfig, err := f.ToRESTConfig()
if err != nil {
return nil, err
}
_ = setSKTDefaults(clientConfig)
return rest.RESTClientFor(clientConfig)
}