global.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. TLS transport.TLSInfo
  29. OutputFormat string
  30. IsHex bool
  31. }
  32. var display printer = &simplePrinter{}
  33. func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
  34. endpoint, err := cmd.Flags().GetString("endpoint")
  35. if err != nil {
  36. ExitWithError(ExitError, err)
  37. }
  38. var cert, key, cacert string
  39. if cert, err = cmd.Flags().GetString("cert"); err != nil {
  40. ExitWithError(ExitBadArgs, err)
  41. } else if cert == "" && cmd.Flags().Changed("cert") {
  42. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
  43. }
  44. if key, err = cmd.Flags().GetString("key"); err != nil {
  45. ExitWithError(ExitBadArgs, err)
  46. } else if key == "" && cmd.Flags().Changed("key") {
  47. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
  48. }
  49. if cacert, err = cmd.Flags().GetString("cacert"); err != nil {
  50. ExitWithError(ExitBadArgs, err)
  51. } else if cacert == "" && cmd.Flags().Changed("cacert") {
  52. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
  53. }
  54. isHex, _ := cmd.Flags().GetBool("hex")
  55. outputType, _ := cmd.Flags().GetString("write-out")
  56. if display = NewPrinter(outputType, isHex); display == nil {
  57. ExitWithError(ExitBadFeature, errors.New("unsupported output format"))
  58. }
  59. return mustClient(endpoint, cert, key, cacert)
  60. }
  61. func mustClient(endpoint, cert, key, cacert string) *clientv3.Client {
  62. // set tls if any one tls option set
  63. var cfgtls *transport.TLSInfo
  64. tls := transport.TLSInfo{}
  65. var file string
  66. if cert != "" {
  67. tls.CertFile = cert
  68. cfgtls = &tls
  69. }
  70. if key != "" {
  71. tls.KeyFile = key
  72. cfgtls = &tls
  73. }
  74. if cacert != "" {
  75. tls.CAFile = file
  76. cfgtls = &tls
  77. }
  78. cfg := clientv3.Config{
  79. Endpoints: []string{endpoint},
  80. TLS: cfgtls,
  81. DialTimeout: 20 * time.Second,
  82. }
  83. client, err := clientv3.New(cfg)
  84. if err != nil {
  85. ExitWithError(ExitBadConnection, err)
  86. }
  87. return client
  88. }
  89. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  90. if i < len(args) {
  91. return args[i], nil
  92. }
  93. bytes, err := ioutil.ReadAll(stdin)
  94. if string(bytes) == "" || err != nil {
  95. return "", errors.New("no available argument and stdin")
  96. }
  97. return string(bytes), nil
  98. }