util.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "errors"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "net/http"
  21. "net/url"
  22. "os"
  23. "strings"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  25. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  26. "github.com/coreos/etcd/client"
  27. "github.com/coreos/etcd/pkg/transport"
  28. )
  29. var (
  30. ErrNoAvailSrc = errors.New("no available argument and stdin")
  31. )
  32. // trimsplit slices s into all substrings separated by sep and returns a
  33. // slice of the substrings between the separator with all leading and trailing
  34. // white space removed, as defined by Unicode.
  35. func trimsplit(s, sep string) []string {
  36. raw := strings.Split(s, ",")
  37. trimmed := make([]string, 0)
  38. for _, r := range raw {
  39. trimmed = append(trimmed, strings.TrimSpace(r))
  40. }
  41. return trimmed
  42. }
  43. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  44. if i < len(args) {
  45. return args[i], nil
  46. }
  47. bytes, err := ioutil.ReadAll(stdin)
  48. if string(bytes) == "" || err != nil {
  49. return "", ErrNoAvailSrc
  50. }
  51. return string(bytes), nil
  52. }
  53. func getPeersFlagValue(c *cli.Context) []string {
  54. peerstr := c.GlobalString("peers")
  55. // Use an environment variable if nothing was supplied on the
  56. // command line
  57. if peerstr == "" {
  58. peerstr = os.Getenv("ETCDCTL_PEERS")
  59. }
  60. // If we still don't have peers, use a default
  61. if peerstr == "" {
  62. peerstr = "127.0.0.1:4001,127.0.0.1:2379"
  63. }
  64. return strings.Split(peerstr, ",")
  65. }
  66. func getEndpoints(c *cli.Context) ([]string, error) {
  67. eps := getPeersFlagValue(c)
  68. for i, ep := range eps {
  69. u, err := url.Parse(ep)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if u.Scheme == "" {
  74. u.Scheme = "http"
  75. }
  76. eps[i] = u.String()
  77. }
  78. return eps, nil
  79. }
  80. func getTransport(c *cli.Context) (*http.Transport, error) {
  81. cafile := c.GlobalString("ca-file")
  82. certfile := c.GlobalString("cert-file")
  83. keyfile := c.GlobalString("key-file")
  84. // Use an environment variable if nothing was supplied on the
  85. // command line
  86. if cafile == "" {
  87. cafile = os.Getenv("ETCDCTL_CA_FILE")
  88. }
  89. if certfile == "" {
  90. certfile = os.Getenv("ETCDCTL_CERT_FILE")
  91. }
  92. if keyfile == "" {
  93. keyfile = os.Getenv("ETCDCTL_KEY_FILE")
  94. }
  95. tls := transport.TLSInfo{
  96. CAFile: cafile,
  97. CertFile: certfile,
  98. KeyFile: keyfile,
  99. }
  100. return transport.NewTransport(tls)
  101. }
  102. func mustNewKeyAPI(c *cli.Context) client.KeysAPI {
  103. return client.NewKeysAPI(mustNewClient(c))
  104. }
  105. func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
  106. return client.NewMembersAPI(mustNewClient(c))
  107. }
  108. func mustNewClient(c *cli.Context) client.Client {
  109. eps, err := getEndpoints(c)
  110. if err != nil {
  111. fmt.Fprintln(os.Stderr, err.Error())
  112. os.Exit(1)
  113. }
  114. tr, err := getTransport(c)
  115. if err != nil {
  116. fmt.Fprintln(os.Stderr, err.Error())
  117. os.Exit(1)
  118. }
  119. cfg := client.Config{
  120. Transport: tr,
  121. Endpoints: eps,
  122. }
  123. uFlag := c.GlobalString("username")
  124. if uFlag != "" {
  125. username, password, err := getUsernamePasswordFromFlag(uFlag)
  126. if err != nil {
  127. fmt.Fprintln(os.Stderr, err.Error())
  128. os.Exit(1)
  129. }
  130. cfg.Username = username
  131. cfg.Password = password
  132. }
  133. hc, err := client.New(cfg)
  134. if err != nil {
  135. fmt.Fprintln(os.Stderr, err.Error())
  136. os.Exit(1)
  137. }
  138. if !c.GlobalBool("no-sync") {
  139. ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
  140. err := hc.Sync(ctx)
  141. cancel()
  142. if err != nil {
  143. fmt.Fprintln(os.Stderr, err.Error())
  144. os.Exit(1)
  145. }
  146. }
  147. if c.GlobalBool("debug") {
  148. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  149. }
  150. if c.GlobalBool("debug") {
  151. client.EnablecURLDebug()
  152. }
  153. return hc
  154. }