global.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "time"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/pkg/transport"
  23. )
  24. // GlobalFlags are flags that defined globally
  25. // and are inherited to all sub-commands.
  26. type GlobalFlags struct {
  27. Endpoints []string
  28. DialTimeout time.Duration
  29. TLS transport.TLSInfo
  30. OutputFormat string
  31. IsHex bool
  32. }
  33. var display printer = &simplePrinter{}
  34. func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
  35. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  36. if err != nil {
  37. ExitWithError(ExitError, err)
  38. }
  39. dialTimeout := dialTimeoutFromCmd(cmd)
  40. cert, key, cacert := keyAndCertFromCmd(cmd)
  41. return mustClient(endpoints, dialTimeout, cert, key, cacert)
  42. }
  43. func mustClient(endpoints []string, dialTimeout time.Duration, cert, key, cacert string) *clientv3.Client {
  44. cfg, err := newClientCfg(endpoints, dialTimeout, cert, key, cacert)
  45. if err != nil {
  46. ExitWithError(ExitBadArgs, err)
  47. }
  48. client, err := clientv3.New(*cfg)
  49. if err != nil {
  50. ExitWithError(ExitBadConnection, err)
  51. }
  52. return client
  53. }
  54. func newClientCfg(endpoints []string, dialTimeout time.Duration, cert, key, cacert string) (*clientv3.Config, error) {
  55. // set tls if any one tls option set
  56. var cfgtls *transport.TLSInfo
  57. tls := transport.TLSInfo{}
  58. var file string
  59. if cert != "" {
  60. tls.CertFile = cert
  61. cfgtls = &tls
  62. }
  63. if key != "" {
  64. tls.KeyFile = key
  65. cfgtls = &tls
  66. }
  67. if cacert != "" {
  68. tls.CAFile = file
  69. cfgtls = &tls
  70. }
  71. cfg := &clientv3.Config{
  72. Endpoints: endpoints,
  73. DialTimeout: dialTimeout,
  74. }
  75. if cfgtls != nil {
  76. clientTLS, err := cfgtls.ClientConfig()
  77. if err != nil {
  78. return nil, err
  79. }
  80. cfg.TLS = clientTLS
  81. }
  82. return cfg, nil
  83. }
  84. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  85. if i < len(args) {
  86. return args[i], nil
  87. }
  88. bytes, err := ioutil.ReadAll(stdin)
  89. if string(bytes) == "" || err != nil {
  90. return "", errors.New("no available argument and stdin")
  91. }
  92. return string(bytes), nil
  93. }
  94. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  95. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  96. if err != nil {
  97. ExitWithError(ExitError, err)
  98. }
  99. return dialTimeout
  100. }
  101. func keyAndCertFromCmd(cmd *cobra.Command) (cert, key, cacert string) {
  102. var err error
  103. if cert, err = cmd.Flags().GetString("cert"); err != nil {
  104. ExitWithError(ExitBadArgs, err)
  105. } else if cert == "" && cmd.Flags().Changed("cert") {
  106. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
  107. }
  108. if key, err = cmd.Flags().GetString("key"); err != nil {
  109. ExitWithError(ExitBadArgs, err)
  110. } else if key == "" && cmd.Flags().Changed("key") {
  111. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
  112. }
  113. if cacert, err = cmd.Flags().GetString("cacert"); err != nil {
  114. ExitWithError(ExitBadArgs, err)
  115. } else if cacert == "" && cmd.Flags().Changed("cacert") {
  116. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
  117. }
  118. return cert, key, cacert
  119. }