status_command.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2016 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. "fmt"
  17. "os"
  18. v3 "github.com/coreos/etcd/clientv3"
  19. "github.com/spf13/cobra"
  20. )
  21. // NewStatusCommand returns the cobra command for "Status".
  22. func NewStatusCommand() *cobra.Command {
  23. return &cobra.Command{
  24. Use: "status",
  25. Short: "status prints out the statuses of the members with given endpoints.",
  26. Run: statusCommandFunc,
  27. }
  28. }
  29. type statusInfo struct {
  30. ep string
  31. resp *v3.StatusResponse
  32. }
  33. func statusCommandFunc(cmd *cobra.Command, args []string) {
  34. c := mustClientFromCmd(cmd)
  35. statusList := []statusInfo{}
  36. var err error
  37. for _, ep := range c.Endpoints() {
  38. ctx, cancel := commandCtx(cmd)
  39. resp, serr := c.Status(ctx, ep)
  40. cancel()
  41. if serr != nil {
  42. err = serr
  43. fmt.Fprintf(os.Stderr, "Failed to get the status of endpoint %s (%v)", ep, serr)
  44. continue
  45. }
  46. statusList = append(statusList, statusInfo{ep: ep, resp: resp})
  47. }
  48. display.MemberStatus(statusList)
  49. if err != nil {
  50. os.Exit(ExitError)
  51. }
  52. }