-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_options.go
122 lines (110 loc) · 2.37 KB
/
client_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
package docker
import (
"context"
"crypto/tls"
"io"
"io/ioutil"
"path/filepath"
"github.com/Unknwon/com"
"github.com/docker/go-connections/tlsconfig"
)
type ClientOptions struct {
host string
tlsConfig *tls.Config
apiVersion string
stderr *OutStream
stdout *OutStream
stdin *InStream
context context.Context
}
type ClientOption func(*ClientOptions)
func NewClientOptions(opts ...ClientOption) *ClientOptions {
res := &ClientOptions{
host: Config.Host,
apiVersion: Config.APIVersion,
stderr: NewOutStream(ioutil.Discard),
stdout: NewOutStream(ioutil.Discard),
stdin: nil,
context: context.Background(),
}
if com.IsDir(Config.CertPath) {
TLSConfig(Config.CertPath, Config.TLSVerify)(res)
}
for _, o := range opts {
o(res)
}
return res
}
func Host(s string) ClientOption {
return func(o *ClientOptions) {
o.host = s
}
}
func TLSConfig(certPath string, verify bool) ClientOption {
return func(o *ClientOptions) {
options := tlsconfig.Options{
CAFile: filepath.Join(certPath, "ca.pem"),
CertFile: filepath.Join(certPath, "cert.pem"),
KeyFile: filepath.Join(certPath, "key.pem"),
InsecureSkipVerify: verify,
}
tlsc, err := tlsconfig.Client(options)
if err != nil {
log.WithError(err).
WithField("cert_path", certPath).
WithField("verify", verify).
Error("Failed to create tls configuration")
return
}
o.tlsConfig = tlsc
}
}
func APIVersion(s string) ClientOption {
return func(o *ClientOptions) {
o.apiVersion = s
}
}
func Stderr(stderr io.Writer) ClientOption {
return func(o *ClientOptions) {
if stderr == nil {
o.stderr = nil
return
}
if s, ok := stderr.(*OutStream); ok {
o.stderr = s
return
}
o.stderr = NewOutStream(stderr)
}
}
func Stdout(stdout io.Writer) ClientOption {
return func(o *ClientOptions) {
if stdout == nil {
o.stdout = nil
return
}
if s, ok := stdout.(*OutStream); ok {
o.stdout = s
return
}
o.stdout = NewOutStream(stdout)
}
}
func Stdin(stdin io.Reader) ClientOption {
return func(o *ClientOptions) {
if stdin == nil {
o.stdin = nil
return
}
if s, ok := stdin.(*InStream); ok {
o.stdin = s
return
}
o.stdin = NewInStream(ioutil.NopCloser(stdin))
}
}
func ClientContext(ctx context.Context) ClientOption {
return func(o *ClientOptions) {
o.context = ctx
}
}