|
| 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-17 |
| 20 | + * Author: Sindweller |
| 21 | + */ |
| 22 | + |
| 23 | +package dir |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 28 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 29 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 30 | + "github.com/opencurve/curve/tools-v2/pkg/config" |
| 31 | + "github.com/opencurve/curve/tools-v2/pkg/output" |
| 32 | + "github.com/opencurve/curve/tools-v2/proto/proto/nameserver2" |
| 33 | + "github.com/spf13/cobra" |
| 34 | + "google.golang.org/grpc" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + dirExample = `$ curve bs list dir -fileName=/test` |
| 39 | +) |
| 40 | + |
| 41 | +type ListDirRpc struct { |
| 42 | + Info *basecmd.Rpc |
| 43 | + Request *nameserver2.ListDirRequest |
| 44 | + curveFSClient nameserver2.CurveFSServiceClient |
| 45 | +} |
| 46 | + |
| 47 | +var _ basecmd.RpcFunc = (*ListDirRpc)(nil) // check interface |
| 48 | + |
| 49 | +type DirCommand struct { |
| 50 | + basecmd.FinalCurveCmd |
| 51 | + Rpc []*ListDirRpc |
| 52 | +} |
| 53 | + |
| 54 | +var _ basecmd.FinalCurveCmdFunc = (*DirCommand)(nil) // check interface |
| 55 | + |
| 56 | +func (lRpc *ListDirRpc) NewRpcClient(cc grpc.ClientConnInterface) { |
| 57 | + lRpc.curveFSClient = nameserver2.NewCurveFSServiceClient(cc) |
| 58 | +} |
| 59 | + |
| 60 | +func (lRpc *ListDirRpc) Stub_Func(ctx context.Context) (interface{}, error) { |
| 61 | + return lRpc.curveFSClient.ListDir(ctx, lRpc.Request) |
| 62 | +} |
| 63 | + |
| 64 | +func NewDirCommand() *cobra.Command { |
| 65 | + return NewListDirCommand().Cmd |
| 66 | +} |
| 67 | + |
| 68 | +func NewListDirCommand() *DirCommand { |
| 69 | + lsCmd := &DirCommand{ |
| 70 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 71 | + Use: "dir", |
| 72 | + Short: "list dir information in curvebs", |
| 73 | + Example: dirExample, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd) |
| 78 | + return lsCmd |
| 79 | +} |
| 80 | + |
| 81 | +// AddFlags implements basecmd.FinalCurveCmdFunc |
| 82 | +func (pCmd *DirCommand) AddFlags() { |
| 83 | + config.AddBsMdsFlagOption(pCmd.Cmd) |
| 84 | + config.AddRpcRetryTimesFlag(pCmd.Cmd) |
| 85 | + config.AddRpcTimeoutFlag(pCmd.Cmd) |
| 86 | + config.AddBsFileNameOptionFlag(pCmd.Cmd) |
| 87 | +} |
| 88 | + |
| 89 | +// Init implements basecmd.FinalCurveCmdFunc |
| 90 | +func (pCmd *DirCommand) 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 | + fileName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_FILENAME) |
| 99 | + |
| 100 | + rpc := &ListDirRpc{ |
| 101 | + Request: &nameserver2.ListDirRequest{ |
| 102 | + FileName: &fileName, |
| 103 | + }, |
| 104 | + Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListDir"), |
| 105 | + } |
| 106 | + pCmd.Rpc = append(pCmd.Rpc, rpc) |
| 107 | + |
| 108 | + header := []string{cobrautil.ROW_IP, cobrautil.ROW_PORT} |
| 109 | + pCmd.SetHeader(header) |
| 110 | + pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice( |
| 111 | + pCmd.Header, header, |
| 112 | + )) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// Print implements basecmd.FinalCurveCmdFunc |
| 117 | +func (pCmd *DirCommand) Print(cmd *cobra.Command, args []string) error { |
| 118 | + return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd) |
| 119 | +} |
| 120 | + |
| 121 | +// RunCommand implements basecmd.FinalCurveCmdFunc |
| 122 | +func (pCmd *DirCommand) RunCommand(cmd *cobra.Command, args []string) error { |
| 123 | + var infos []*basecmd.Rpc |
| 124 | + var funcs []basecmd.RpcFunc |
| 125 | + for _, rpc := range pCmd.Rpc { |
| 126 | + infos = append(infos, rpc.Info) |
| 127 | + funcs = append(funcs, rpc) |
| 128 | + } |
| 129 | + results, errs := basecmd.GetRpcListResponse(infos, funcs) |
| 130 | + if len(errs) == len(infos) { |
| 131 | + mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs) |
| 132 | + return mergeErr.ToError() |
| 133 | + } |
| 134 | + var errors []*cmderror.CmdError |
| 135 | + rows := make([]map[string]string, 0) |
| 136 | + for _, res := range results { |
| 137 | + infos := res.(*nameserver2.ListDirResponse).GetFileInfo() |
| 138 | + for _, info := range infos { |
| 139 | + row := make(map[string]string) |
| 140 | + row[cobrautil.ROW_FILE_NAME] = info.GetFileName() |
| 141 | + row[cobrautil.ROW_PARENT_ID] = string(info.GetParentId()) |
| 142 | + // TODO 修改计算size的方式 |
| 143 | + row[cobrautil.ROW_FILE_TYPE] = string(info.GetFileType()) |
| 144 | + row[cobrautil.ROW_OWNER] = info.GetOwner() |
| 145 | + row[cobrautil.ROW_CTIME] = string(info.GetCtime()) |
| 146 | + rows = append(rows, row) |
| 147 | + } |
| 148 | + } |
| 149 | + list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{ |
| 150 | + cobrautil.ROW_FS_NAME, |
| 151 | + }) |
| 152 | + pCmd.TableNew.AppendBulk(list) |
| 153 | + errRet := cmderror.MergeCmdError(errors) |
| 154 | + pCmd.Error = &errRet |
| 155 | + pCmd.Result = results |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +// ResultPlainOutput implements basecmd.FinalCurveCmdFunc |
| 160 | +func (pCmd *DirCommand) ResultPlainOutput() error { |
| 161 | + return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd) |
| 162 | +} |
0 commit comments