|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "errors"
|
6 | 5 | "fmt"
|
7 |
| - "os" |
8 | 6 |
|
9 | 7 | clientv3 "go.etcd.io/etcd/client/v3"
|
10 | 8 |
|
11 | 9 | "github.com/tarantool/go-tarantool/v2"
|
12 | 10 | "github.com/tarantool/tt/cli/cluster"
|
13 | 11 | libcluster "github.com/tarantool/tt/lib/cluster"
|
14 |
| - "github.com/tarantool/tt/lib/connect" |
| 12 | + libconnect "github.com/tarantool/tt/lib/connect" |
15 | 13 | )
|
16 | 14 |
|
17 | 15 | // printRawClusterConfig prints a raw cluster configuration or an instance
|
@@ -137,87 +135,12 @@ func printConfig(config *libcluster.Config) {
|
137 | 135 | fmt.Print(config.String())
|
138 | 136 | }
|
139 | 137 |
|
140 |
| -// connectOpts is additional connect options specified by a user. |
141 |
| -type connectOpts struct { |
142 |
| - Username string |
143 |
| - Password string |
144 |
| -} |
145 |
| - |
146 |
| -// connectTarantool establishes a connection to Tarantool. |
147 |
| -func connectTarantool(uriOpts connect.UriOpts, connOpts connectOpts) (tarantool.Connector, error) { |
148 |
| - if uriOpts.Username == "" && uriOpts.Password == "" { |
149 |
| - uriOpts.Username = connOpts.Username |
150 |
| - uriOpts.Password = connOpts.Password |
151 |
| - if uriOpts.Username == "" { |
152 |
| - uriOpts.Username = os.Getenv(connect.TarantoolUsernameEnv) |
153 |
| - } |
154 |
| - if uriOpts.Password == "" { |
155 |
| - uriOpts.Password = os.Getenv(connect.TarantoolPasswordEnv) |
156 |
| - } |
157 |
| - } |
158 |
| - |
159 |
| - dialer, connectorOpts, err := MakeConnectOptsFromUriOpts(uriOpts) |
160 |
| - if err != nil { |
161 |
| - return nil, err |
162 |
| - } |
163 |
| - |
164 |
| - ctx := context.Background() |
165 |
| - if connectorOpts.Timeout > 0 { |
166 |
| - var cancel context.CancelFunc |
167 |
| - ctx, cancel = context.WithTimeout(ctx, connectorOpts.Timeout) |
168 |
| - defer cancel() |
169 |
| - } |
170 |
| - conn, err := tarantool.Connect(ctx, dialer, connectorOpts) |
171 |
| - if err != nil { |
172 |
| - return nil, fmt.Errorf("failed to connect to tarantool: %w", err) |
173 |
| - } |
174 |
| - return conn, nil |
175 |
| -} |
176 |
| - |
177 |
| -// connectEtcd establishes a connection to etcd. |
178 |
| -func connectEtcd(uriOpts connect.UriOpts, connOpts connectOpts) (*clientv3.Client, error) { |
179 |
| - etcdOpts := MakeEtcdOptsFromUriOpts(uriOpts) |
180 |
| - if etcdOpts.Username == "" && etcdOpts.Password == "" { |
181 |
| - etcdOpts.Username = connOpts.Username |
182 |
| - etcdOpts.Password = connOpts.Password |
183 |
| - if etcdOpts.Username == "" { |
184 |
| - etcdOpts.Username = os.Getenv(connect.EtcdUsernameEnv) |
185 |
| - } |
186 |
| - if etcdOpts.Password == "" { |
187 |
| - etcdOpts.Password = os.Getenv(connect.EtcdPasswordEnv) |
188 |
| - } |
189 |
| - } |
190 |
| - |
191 |
| - etcdcli, err := libcluster.ConnectEtcd(etcdOpts) |
192 |
| - if err != nil { |
193 |
| - return nil, fmt.Errorf("failed to connect to etcd: %w", err) |
194 |
| - } |
195 |
| - return etcdcli, nil |
196 |
| -} |
197 |
| - |
198 |
| -// doOnStorage determines a storage based on the opts. |
199 |
| -func doOnStorage(connOpts connectOpts, opts connect.UriOpts, |
200 |
| - tarantoolFunc func(tarantool.Connector) error, etcdFunc func(*clientv3.Client) error) error { |
201 |
| - etcdcli, errEtcd := connectEtcd(opts, connOpts) |
202 |
| - if errEtcd == nil { |
203 |
| - return etcdFunc(etcdcli) |
204 |
| - } |
205 |
| - |
206 |
| - conn, errTarantool := connectTarantool(opts, connOpts) |
207 |
| - if errTarantool == nil { |
208 |
| - return tarantoolFunc(conn) |
209 |
| - } |
210 |
| - |
211 |
| - return fmt.Errorf("failed to establish a connection to tarantool or etcd: %w, %w", |
212 |
| - errTarantool, errEtcd) |
213 |
| -} |
214 |
| - |
215 | 138 | // createPublisherAndCollector creates a new data publisher and collector based on UriOpts.
|
216 | 139 | func createPublisherAndCollector(
|
217 | 140 | publishers libcluster.DataPublisherFactory,
|
218 | 141 | collectors libcluster.CollectorFactory,
|
219 |
| - connOpts connectOpts, |
220 |
| - opts connect.UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) { |
| 142 | + connOpts libcluster.ConnectOpts, |
| 143 | + opts libconnect.UriOpts) (libcluster.DataPublisher, libcluster.Collector, func(), error) { |
221 | 144 | prefix, key, timeout := opts.Prefix, opts.Params["key"], opts.Timeout
|
222 | 145 |
|
223 | 146 | var (
|
@@ -265,7 +188,7 @@ func createPublisherAndCollector(
|
265 | 188 | return nil
|
266 | 189 | }
|
267 | 190 |
|
268 |
| - if err := doOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil { |
| 191 | + if err := libcluster.DoOnStorage(connOpts, opts, tarantoolFunc, etcdFunc); err != nil { |
269 | 192 | return nil, nil, nil, err
|
270 | 193 | }
|
271 | 194 |
|
|
0 commit comments