go.etcd.io/etcd@v3.3.27+incompatible/etcdctl/ctlv3/command/alarm_command.go (about)

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