global.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var cert, key, cacert string
  41. if cert, err = cmd.Flags().GetString("cert"); err != nil {
  42. ExitWithError(ExitBadArgs, err)
  43. } else if cert == "" && cmd.Flags().Changed("cert") {
  44. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
  45. }
  46. if key, err = cmd.Flags().GetString("key"); err != nil {
  47. ExitWithError(ExitBadArgs, err)
  48. } else if key == "" && cmd.Flags().Changed("key") {
  49. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
  50. }
  51. if cacert, err = cmd.Flags().GetString("cacert"); err != nil {
  52. ExitWithError(ExitBadArgs, err)
  53. } else if cacert == "" && cmd.Flags().Changed("cacert") {
  54. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
  55. }
  56. isHex, _ := cmd.Flags().GetBool("hex")
  57. outputType, _ := cmd.Flags().GetString("write-out")
  58. if display = NewPrinter(outputType, isHex); display == nil {
  59. ExitWithError(ExitBadFeature, errors.New("unsupported output format"))
  60. }
  61. return mustClient(endpoints, dialTimeout, cert, key, cacert)
  62. }
  63. func mustClient(endpoints []string, dialTimeout time.Duration, cert, key, cacert string) *clientv3.Client {
  64. // set tls if any one tls option set
  65. var cfgtls *transport.TLSInfo
  66. tls := transport.TLSInfo{}
  67. var file string
  68. if cert != "" {
  69. tls.CertFile = cert
  70. cfgtls = &tls
  71. }
  72. if key != "" {
  73. tls.KeyFile = key
  74. cfgtls = &tls
  75. }
  76. if cacert != "" {
  77. tls.CAFile = file
  78. cfgtls = &tls
  79. }
  80. cfg := clientv3.Config{
  81. Endpoints: endpoints,
  82. DialTimeout: dialTimeout,
  83. }
  84. if cfgtls != nil {
  85. clientTLS, err := cfgtls.ClientConfig()
  86. if err != nil {
  87. ExitWithError(ExitBadArgs, err)
  88. }
  89. cfg.TLS = clientTLS
  90. }
  91. client, err := clientv3.New(cfg)
  92. if err != nil {
  93. ExitWithError(ExitBadConnection, err)
  94. }
  95. return client
  96. }
  97. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  98. if i < len(args) {
  99. return args[i], nil
  100. }
  101. bytes, err := ioutil.ReadAll(stdin)
  102. if string(bytes) == "" || err != nil {
  103. return "", errors.New("no available argument and stdin")
  104. }
  105. return string(bytes), nil
  106. }
  107. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  108. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  109. if err != nil {
  110. ExitWithError(ExitError, err)
  111. }
  112. return dialTimeout
  113. }