alarm_command.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2016 The etcd Authors
  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. v3 "github.com/coreos/etcd/clientv3"
  18. "github.com/spf13/cobra"
  19. )
  20. // NewAlarmCommand returns the cobra command for "alarm".
  21. func NewAlarmCommand() *cobra.Command {
  22. ac := &cobra.Command{
  23. Use: "alarm <subcommand>",
  24. Short: "Alarm related commands",
  25. }
  26. ac.AddCommand(NewAlarmDisarmCommand())
  27. ac.AddCommand(NewAlarmListCommand())
  28. return ac
  29. }
  30. func NewAlarmDisarmCommand() *cobra.Command {
  31. cmd := cobra.Command{
  32. Use: "disarm",
  33. Short: "Disarms all alarms",
  34. Run: alarmDisarmCommandFunc,
  35. }
  36. return &cmd
  37. }
  38. // alarmDisarmCommandFunc executes the "alarm disarm" command.
  39. func alarmDisarmCommandFunc(cmd *cobra.Command, args []string) {
  40. if len(args) != 0 {
  41. ExitWithError(ExitBadArgs, fmt.Errorf("alarm disarm command accepts no arguments"))
  42. }
  43. ctx, cancel := commandCtx(cmd)
  44. resp, err := mustClientFromCmd(cmd).AlarmDisarm(ctx, &v3.AlarmMember{})
  45. cancel()
  46. if err != nil {
  47. ExitWithError(ExitError, err)
  48. }
  49. display.Alarm(*resp)
  50. }
  51. func NewAlarmListCommand() *cobra.Command {
  52. cmd := cobra.Command{
  53. Use: "list",
  54. Short: "Lists all alarms",
  55. Run: alarmListCommandFunc,
  56. }
  57. return &cmd
  58. }
  59. // alarmListCommandFunc executes the "alarm list" command.
  60. func alarmListCommandFunc(cmd *cobra.Command, args []string) {
  61. if len(args) != 0 {
  62. ExitWithError(ExitBadArgs, fmt.Errorf("alarm list command accepts no arguments"))
  63. }
  64. ctx, cancel := commandCtx(cmd)
  65. resp, err := mustClientFromCmd(cmd).AlarmList(ctx)
  66. cancel()
  67. if err != nil {
  68. ExitWithError(ExitError, err)
  69. }
  70. display.Alarm(*resp)
  71. }