util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "io"
  18. "io/ioutil"
  19. "net/http"
  20. "net/url"
  21. "os"
  22. "strings"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  24. "github.com/coreos/etcd/pkg/transport"
  25. )
  26. var (
  27. ErrNoAvailSrc = errors.New("no available argument and stdin")
  28. )
  29. // trimsplit slices s into all substrings separated by sep and returns a
  30. // slice of the substrings between the separator with all leading and trailing
  31. // white space removed, as defined by Unicode.
  32. func trimsplit(s, sep string) []string {
  33. raw := strings.Split(s, ",")
  34. trimmed := make([]string, 0)
  35. for _, r := range raw {
  36. trimmed = append(trimmed, strings.TrimSpace(r))
  37. }
  38. return trimmed
  39. }
  40. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  41. if i < len(args) {
  42. return args[i], nil
  43. }
  44. bytes, err := ioutil.ReadAll(stdin)
  45. if string(bytes) == "" || err != nil {
  46. return "", ErrNoAvailSrc
  47. }
  48. return string(bytes), nil
  49. }
  50. func getPeersFlagValue(c *cli.Context) []string {
  51. peerstr := c.GlobalString("peers")
  52. // Use an environment variable if nothing was supplied on the
  53. // command line
  54. if peerstr == "" {
  55. peerstr = os.Getenv("ETCDCTL_PEERS")
  56. }
  57. // If we still don't have peers, use a default
  58. if peerstr == "" {
  59. peerstr = "127.0.0.1:4001,127.0.0.1:2379"
  60. }
  61. return strings.Split(peerstr, ",")
  62. }
  63. func getEndpoints(c *cli.Context) ([]string, error) {
  64. eps := getPeersFlagValue(c)
  65. for i, ep := range eps {
  66. u, err := url.Parse(ep)
  67. if err != nil {
  68. return nil, err
  69. }
  70. if u.Scheme == "" {
  71. u.Scheme = "http"
  72. }
  73. eps[i] = u.String()
  74. }
  75. return eps, nil
  76. }
  77. func getTransport(c *cli.Context) (*http.Transport, error) {
  78. cafile := c.GlobalString("ca-file")
  79. certfile := c.GlobalString("cert-file")
  80. keyfile := c.GlobalString("key-file")
  81. // Use an environment variable if nothing was supplied on the
  82. // command line
  83. if cafile == "" {
  84. cafile = os.Getenv("ETCDCTL_CA_FILE")
  85. }
  86. if certfile == "" {
  87. certfile = os.Getenv("ETCDCTL_CERT_FILE")
  88. }
  89. if keyfile == "" {
  90. keyfile = os.Getenv("ETCDCTL_KEY_FILE")
  91. }
  92. tls := transport.TLSInfo{
  93. CAFile: cafile,
  94. CertFile: certfile,
  95. KeyFile: keyfile,
  96. }
  97. return transport.NewTransport(tls)
  98. }