global.go 2.4 KB

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