|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 NetEase Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: CurveCli |
| 19 | + * Created Date: 2022-11-10 |
| 20 | + * Author: Tsonglew |
| 21 | + */ |
| 22 | + |
| 23 | +package client |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 29 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 30 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 31 | + "github.com/opencurve/curve/tools-v2/pkg/config" |
| 32 | + "github.com/opencurve/curve/tools-v2/pkg/output" |
| 33 | + "github.com/opencurve/curve/tools-v2/proto/proto/nameserver2" |
| 34 | + "github.com/spf13/cobra" |
| 35 | + "google.golang.org/grpc" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + clientExample = `$ curve bs list client` |
| 40 | +) |
| 41 | + |
| 42 | +type ListClientRpc struct { |
| 43 | + Info *basecmd.Rpc |
| 44 | + Request *nameserver2.ListClientRequest |
| 45 | + curveFSClient nameserver2.CurveFSServiceClient |
| 46 | +} |
| 47 | + |
| 48 | +var _ basecmd.RpcFunc = (*ListClientRpc)(nil) // check interface |
| 49 | + |
| 50 | +type ClientCommand struct { |
| 51 | + basecmd.FinalCurveCmd |
| 52 | + Rpc []*ListClientRpc |
| 53 | +} |
| 54 | + |
| 55 | +var _ basecmd.FinalCurveCmdFunc = (*ClientCommand)(nil) // check interface |
| 56 | + |
| 57 | +func (lRpc *ListClientRpc) NewRpcClient(cc grpc.ClientConnInterface) { |
| 58 | + lRpc.curveFSClient = nameserver2.NewCurveFSServiceClient(cc) |
| 59 | +} |
| 60 | + |
| 61 | +func (lRpc *ListClientRpc) Stub_Func(ctx context.Context) (interface{}, error) { |
| 62 | + return lRpc.curveFSClient.ListClient(ctx, lRpc.Request) |
| 63 | +} |
| 64 | + |
| 65 | +func NewClientCommand() *cobra.Command { |
| 66 | + return NewListCLientCommand().Cmd |
| 67 | +} |
| 68 | + |
| 69 | +func NewListCLientCommand() *ClientCommand { |
| 70 | + lsCmd := &ClientCommand{ |
| 71 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 72 | + Use: "client", |
| 73 | + Short: "list all client information in curvebs", |
| 74 | + Example: clientExample, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd) |
| 79 | + return lsCmd |
| 80 | +} |
| 81 | + |
| 82 | +// AddFlags implements basecmd.FinalCurveCmdFunc |
| 83 | +func (pCmd *ClientCommand) AddFlags() { |
| 84 | + config.AddBsMdsFlagOption(pCmd.Cmd) |
| 85 | + config.AddRpcRetryTimesFlag(pCmd.Cmd) |
| 86 | + config.AddRpcTimeoutFlag(pCmd.Cmd) |
| 87 | +} |
| 88 | + |
| 89 | +// Init implements basecmd.FinalCurveCmdFunc |
| 90 | +func (pCmd *ClientCommand) Init(cmd *cobra.Command, args []string) error { |
| 91 | + mdsAddrs, err := config.GetBsMdsAddrSlice(pCmd.Cmd) |
| 92 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 93 | + return err.ToError() |
| 94 | + } |
| 95 | + |
| 96 | + timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT) |
| 97 | + retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES) |
| 98 | + |
| 99 | + rpc := &ListClientRpc{ |
| 100 | + Request: &nameserver2.ListClientRequest{}, |
| 101 | + Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListClient"), |
| 102 | + } |
| 103 | + pCmd.Rpc = append(pCmd.Rpc, rpc) |
| 104 | + |
| 105 | + header := []string{cobrautil.ROW_IP, cobrautil.ROW_PORT} |
| 106 | + pCmd.SetHeader(header) |
| 107 | + pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice( |
| 108 | + pCmd.Header, header, |
| 109 | + )) |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// Print implements basecmd.FinalCurveCmdFunc |
| 114 | +func (pCmd *ClientCommand) Print(cmd *cobra.Command, args []string) error { |
| 115 | + return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd) |
| 116 | +} |
| 117 | + |
| 118 | +// RunCommand implements basecmd.FinalCurveCmdFunc |
| 119 | +func (pCmd *ClientCommand) RunCommand(cmd *cobra.Command, args []string) error { |
| 120 | + var infos []*basecmd.Rpc |
| 121 | + var funcs []basecmd.RpcFunc |
| 122 | + for _, rpc := range pCmd.Rpc { |
| 123 | + infos = append(infos, rpc.Info) |
| 124 | + funcs = append(funcs, rpc) |
| 125 | + } |
| 126 | + results, errs := basecmd.GetRpcListResponse(infos, funcs) |
| 127 | + if len(errs) == len(infos) { |
| 128 | + mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs) |
| 129 | + return mergeErr.ToError() |
| 130 | + } |
| 131 | + var errors []*cmderror.CmdError |
| 132 | + rows := make([]map[string]string, 0) |
| 133 | + for _, res := range results { |
| 134 | + infos := res.(*nameserver2.ListClientResponse).GetClientInfos() |
| 135 | + for _, info := range infos { |
| 136 | + row := make(map[string]string) |
| 137 | + row[cobrautil.ROW_IP] = info.GetIp() |
| 138 | + row[cobrautil.ROW_PORT] = fmt.Sprintf("%d", info.GetPort()) |
| 139 | + rows = append(rows, row) |
| 140 | + } |
| 141 | + } |
| 142 | + list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{ |
| 143 | + cobrautil.ROW_IP, |
| 144 | + }) |
| 145 | + pCmd.TableNew.AppendBulk(list) |
| 146 | + errRet := cmderror.MergeCmdError(errors) |
| 147 | + pCmd.Error = &errRet |
| 148 | + pCmd.Result = results |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +// ResultPlainOutput implements basecmd.FinalCurveCmdFunc |
| 153 | +func (pCmd *ClientCommand) ResultPlainOutput() error { |
| 154 | + return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd) |
| 155 | +} |
0 commit comments