-
Notifications
You must be signed in to change notification settings - Fork 43
/
connect.go
70 lines (59 loc) · 1.4 KB
/
connect.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
package command
import (
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/bgentry/speakeasy"
"github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/transport"
"golang.org/x/net/context"
)
func contextWithCommandTimeout(c *cli.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), c.GlobalDuration("command-timeout"))
}
func newTransport(e Etcdtool) *http.Transport {
tls := transport.TLSInfo{
CAFile: e.CA,
CertFile: e.Cert,
KeyFile: e.Key,
}
timeout := 30 * time.Second
tr, err := transport.NewTransport(tls, timeout)
if err != nil {
fatal(err.Error())
}
return tr
}
func newClient(e Etcdtool) client.Client {
cfg := client.Config{
Transport: newTransport(e),
Endpoints: strings.Split(e.Peers, ","),
HeaderTimeoutPerRequest: e.Timeout,
}
if e.User != "" {
cfg.Username = e.User
var err error
if e.PasswordFilePath != "" {
passBytes, err := ioutil.ReadFile(e.PasswordFilePath)
if err != nil {
fatal(err.Error())
}
cfg.Password = strings.TrimRight(string(passBytes), "\n")
} else {
cfg.Password, err = speakeasy.Ask("Password: ")
if err != nil {
fatal(err.Error())
}
}
}
cl, err := client.New(cfg)
if err != nil {
fatal(err.Error())
}
return cl
}
func newKeyAPI(e Etcdtool) client.KeysAPI {
return client.NewKeysAPI(newClient(e))
}