util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/client"
  26. "github.com/coreos/etcd/pkg/transport"
  27. )
  28. var (
  29. ErrNoAvailSrc = errors.New("no available argument and stdin")
  30. )
  31. // trimsplit slices s into all substrings separated by sep and returns a
  32. // slice of the substrings between the separator with all leading and trailing
  33. // white space removed, as defined by Unicode.
  34. func trimsplit(s, sep string) []string {
  35. raw := strings.Split(s, ",")
  36. trimmed := make([]string, 0)
  37. for _, r := range raw {
  38. trimmed = append(trimmed, strings.TrimSpace(r))
  39. }
  40. return trimmed
  41. }
  42. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  43. if i < len(args) {
  44. return args[i], nil
  45. }
  46. bytes, err := ioutil.ReadAll(stdin)
  47. if string(bytes) == "" || err != nil {
  48. return "", ErrNoAvailSrc
  49. }
  50. return string(bytes), nil
  51. }
  52. func getPeersFlagValue(c *cli.Context) []string {
  53. peerstr := c.GlobalString("peers")
  54. // Use an environment variable if nothing was supplied on the
  55. // command line
  56. if peerstr == "" {
  57. peerstr = os.Getenv("ETCDCTL_PEERS")
  58. }
  59. // If we still don't have peers, use a default
  60. if peerstr == "" {
  61. peerstr = "127.0.0.1:4001,127.0.0.1:2379"
  62. }
  63. return strings.Split(peerstr, ",")
  64. }
  65. func getEndpoints(c *cli.Context) ([]string, error) {
  66. eps := getPeersFlagValue(c)
  67. for i, ep := range eps {
  68. u, err := url.Parse(ep)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if u.Scheme == "" {
  73. u.Scheme = "http"
  74. }
  75. eps[i] = u.String()
  76. }
  77. return eps, nil
  78. }
  79. func getTransport(c *cli.Context) (*http.Transport, error) {
  80. cafile := c.GlobalString("ca-file")
  81. certfile := c.GlobalString("cert-file")
  82. keyfile := c.GlobalString("key-file")
  83. // Use an environment variable if nothing was supplied on the
  84. // command line
  85. if cafile == "" {
  86. cafile = os.Getenv("ETCDCTL_CA_FILE")
  87. }
  88. if certfile == "" {
  89. certfile = os.Getenv("ETCDCTL_CERT_FILE")
  90. }
  91. if keyfile == "" {
  92. keyfile = os.Getenv("ETCDCTL_KEY_FILE")
  93. }
  94. tls := transport.TLSInfo{
  95. CAFile: cafile,
  96. CertFile: certfile,
  97. KeyFile: keyfile,
  98. }
  99. return transport.NewTransport(tls)
  100. }
  101. func mustNewClient(c *cli.Context) client.Client {
  102. eps, err := getEndpoints(c)
  103. if err != nil {
  104. fmt.Fprintln(os.Stderr, err.Error())
  105. os.Exit(1)
  106. }
  107. tr, err := getTransport(c)
  108. if err != nil {
  109. fmt.Fprintln(os.Stderr, err.Error())
  110. os.Exit(1)
  111. }
  112. cfg := client.Config{
  113. Transport: tr,
  114. Endpoints: eps,
  115. }
  116. uFlag := c.GlobalString("username")
  117. if uFlag != "" {
  118. username, password, err := getUsernamePasswordFromFlag(uFlag)
  119. if err != nil {
  120. fmt.Fprintln(os.Stderr, err.Error())
  121. os.Exit(1)
  122. }
  123. cfg.Username = username
  124. cfg.Password = password
  125. }
  126. hc, err := client.New(cfg)
  127. if err != nil {
  128. fmt.Fprintln(os.Stderr, err.Error())
  129. os.Exit(1)
  130. }
  131. return hc
  132. }