global.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  17. "github.com/coreos/etcd/clientv3"
  18. "github.com/coreos/etcd/pkg/transport"
  19. )
  20. // GlobalFlags are flags that defined globally
  21. // and are inherited to all sub-commands.
  22. type GlobalFlags struct {
  23. Endpoints string
  24. TLS transport.TLSInfo
  25. }
  26. func mustClient(cmd *cobra.Command) *clientv3.Client {
  27. endpoint, err := cmd.Flags().GetString("endpoint")
  28. if err != nil {
  29. ExitWithError(ExitError, err)
  30. }
  31. // set tls if any one tls option set
  32. var cfgtls *transport.TLSInfo
  33. tls := transport.TLSInfo{}
  34. if tls.CertFile, err = cmd.Flags().GetString("cert"); err == nil {
  35. cfgtls = &tls
  36. }
  37. if tls.KeyFile, err = cmd.Flags().GetString("key"); err == nil {
  38. cfgtls = &tls
  39. }
  40. if tls.CAFile, err = cmd.Flags().GetString("cacert"); err == nil {
  41. cfgtls = &tls
  42. }
  43. cfg := clientv3.Config{
  44. Endpoints: []string{endpoint},
  45. TLS: cfgtls,
  46. }
  47. client, err := clientv3.New(cfg)
  48. if err != nil {
  49. ExitWithError(ExitBadConnection, err)
  50. }
  51. return client
  52. }