global.go 2.7 KB

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