|
| 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 | + "fmt" |
| 28 | + "github.com/dustin/go-humanize" |
| 29 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 30 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 31 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 32 | + "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/query/file" |
| 33 | + "github.com/opencurve/curve/tools-v2/pkg/config" |
| 34 | + "github.com/opencurve/curve/tools-v2/pkg/output" |
| 35 | + "github.com/opencurve/curve/tools-v2/proto/proto/nameserver2" |
| 36 | + "github.com/spf13/cobra" |
| 37 | + "github.com/spf13/viper" |
| 38 | + "google.golang.org/grpc" |
| 39 | + "log" |
| 40 | + "time" |
| 41 | +) |
| 42 | + |
| 43 | +const ( |
| 44 | + dirExample = `$ curve bs list dir --dir /` |
| 45 | +) |
| 46 | + |
| 47 | +type ListDirRpc struct { |
| 48 | + Info *basecmd.Rpc |
| 49 | + Request *nameserver2.ListDirRequest |
| 50 | + curveFSClient nameserver2.CurveFSServiceClient |
| 51 | +} |
| 52 | + |
| 53 | +var _ basecmd.RpcFunc = (*ListDirRpc)(nil) // check interface |
| 54 | + |
| 55 | +type DirCommand struct { |
| 56 | + basecmd.FinalCurveCmd |
| 57 | + Rpc []*ListDirRpc |
| 58 | +} |
| 59 | + |
| 60 | +var _ basecmd.FinalCurveCmdFunc = (*DirCommand)(nil) // check interface |
| 61 | + |
| 62 | +func (lRpc *ListDirRpc) NewRpcClient(cc grpc.ClientConnInterface) { |
| 63 | + lRpc.curveFSClient = nameserver2.NewCurveFSServiceClient(cc) |
| 64 | +} |
| 65 | + |
| 66 | +func (lRpc *ListDirRpc) Stub_Func(ctx context.Context) (interface{}, error) { |
| 67 | + return lRpc.curveFSClient.ListDir(ctx, lRpc.Request) |
| 68 | +} |
| 69 | + |
| 70 | +func NewDirCommand() *cobra.Command { |
| 71 | + return NewListDirCommand().Cmd |
| 72 | +} |
| 73 | + |
| 74 | +func NewListDirCommand() *DirCommand { |
| 75 | + lsCmd := &DirCommand{ |
| 76 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 77 | + Use: "dir", |
| 78 | + Short: "list directory information in curvebs", |
| 79 | + Example: dirExample, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd) |
| 84 | + return lsCmd |
| 85 | +} |
| 86 | + |
| 87 | +// AddFlags implements basecmd.FinalCurveCmdFunc |
| 88 | +func (pCmd *DirCommand) AddFlags() { |
| 89 | + config.AddBsMdsFlagOption(pCmd.Cmd) |
| 90 | + config.AddRpcRetryTimesFlag(pCmd.Cmd) |
| 91 | + config.AddRpcTimeoutFlag(pCmd.Cmd) |
| 92 | + config.AddBsDirOptionFlag(pCmd.Cmd) |
| 93 | + config.AddBsUserOptionFlag(pCmd.Cmd) |
| 94 | + config.AddBsPasswordOptionFlag(pCmd.Cmd) |
| 95 | +} |
| 96 | + |
| 97 | +// Init implements basecmd.FinalCurveCmdFunc |
| 98 | +func (pCmd *DirCommand) Init(cmd *cobra.Command, args []string) error { |
| 99 | + mdsAddrs, err := config.GetBsMdsAddrSlice(pCmd.Cmd) |
| 100 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 101 | + return err.ToError() |
| 102 | + } |
| 103 | + |
| 104 | + timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT) |
| 105 | + retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES) |
| 106 | + fileName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR) |
| 107 | + owner := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_USER) |
| 108 | + date, errDat := cobrautil.GetTimeofDayUs() |
| 109 | + if errDat.TypeCode() != cmderror.CODE_SUCCESS { |
| 110 | + return errDat.ToError() |
| 111 | + } |
| 112 | + |
| 113 | + rpc := &ListDirRpc{ |
| 114 | + Request: &nameserver2.ListDirRequest{ |
| 115 | + FileName: &fileName, |
| 116 | + Owner: &owner, |
| 117 | + Date: &date, |
| 118 | + }, |
| 119 | + Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListDir"), |
| 120 | + } |
| 121 | + // auth |
| 122 | + password := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_PASSWORD) |
| 123 | + if owner == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 { |
| 124 | + strSig := cobrautil.GetString2Signature(date, owner) |
| 125 | + sig := cobrautil.CalcString2Signature(strSig, password) |
| 126 | + rpc.Request.Signature = &sig |
| 127 | + } |
| 128 | + pCmd.Rpc = append(pCmd.Rpc, rpc) |
| 129 | + header := []string{ |
| 130 | + cobrautil.ROW_ID, |
| 131 | + cobrautil.ROW_FILE_NAME, |
| 132 | + cobrautil.ROW_PARENT_ID, |
| 133 | + cobrautil.ROW_FILE_TYPE, |
| 134 | + cobrautil.ROW_OWNER, |
| 135 | + cobrautil.ROW_CTIME, |
| 136 | + cobrautil.ROW_ALLOC_SIZE, |
| 137 | + cobrautil.ROW_FILE_SIZE, |
| 138 | + } |
| 139 | + pCmd.SetHeader(header) |
| 140 | + pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice( |
| 141 | + pCmd.Header, []string{cobrautil.ROW_OWNER, cobrautil.ROW_FILE_TYPE, cobrautil.ROW_PARENT_ID}, |
| 142 | + )) |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// Print implements basecmd.FinalCurveCmdFunc |
| 147 | +func (pCmd *DirCommand) Print(cmd *cobra.Command, args []string) error { |
| 148 | + return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd) |
| 149 | +} |
| 150 | + |
| 151 | +// RunCommand implements basecmd.FinalCurveCmdFunc |
| 152 | +func (pCmd *DirCommand) RunCommand(cmd *cobra.Command, args []string) error { |
| 153 | + var infos []*basecmd.Rpc |
| 154 | + var funcs []basecmd.RpcFunc |
| 155 | + for _, rpc := range pCmd.Rpc { |
| 156 | + infos = append(infos, rpc.Info) |
| 157 | + funcs = append(funcs, rpc) |
| 158 | + } |
| 159 | + results, errs := basecmd.GetRpcListResponse(infos, funcs) |
| 160 | + if len(errs) == len(infos) { |
| 161 | + mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs) |
| 162 | + return mergeErr.ToError() |
| 163 | + } |
| 164 | + var errors []*cmderror.CmdError |
| 165 | + rows := make([]map[string]string, 0) |
| 166 | + for _, res := range results { |
| 167 | + infos := res.(*nameserver2.ListDirResponse).GetFileInfo() |
| 168 | + for _, info := range infos { |
| 169 | + row := make(map[string]string) |
| 170 | + dirName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR) |
| 171 | + var fileName string |
| 172 | + if dirName == "/" { |
| 173 | + fileName = dirName + info.GetFileName() |
| 174 | + } else { |
| 175 | + fileName = dirName + "/" + info.GetFileName() |
| 176 | + } |
| 177 | + row[cobrautil.ROW_ID] = fmt.Sprintf("%d", info.GetId()) |
| 178 | + row[cobrautil.ROW_FILE_NAME] = fileName |
| 179 | + row[cobrautil.ROW_PARENT_ID] = fmt.Sprintf("%d", info.GetParentId()) |
| 180 | + row[cobrautil.ROW_FILE_TYPE] = fmt.Sprintf("%v", info.GetFileType()) |
| 181 | + row[cobrautil.ROW_OWNER] = info.GetOwner() |
| 182 | + row[cobrautil.ROW_CTIME] = time.Unix(int64(info.GetCtime()/1000000), 0).Format("2006-01-02 15:04:05") |
| 183 | + |
| 184 | + // generate a query file command |
| 185 | + fInfoCmd := file.NewQueryFileCommand() |
| 186 | + config.AlignFlagsValue(pCmd.Cmd, fInfoCmd.Cmd, []string{ |
| 187 | + config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR, |
| 188 | + config.CURVEBS_PATH, |
| 189 | + }) |
| 190 | + fInfoCmd.Cmd.Flags().Set("path", fileName) |
| 191 | + |
| 192 | + // Get file size |
| 193 | + sizeRes, err := file.GetFileSize(fInfoCmd.Cmd) |
| 194 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 195 | + // TODO handle err |
| 196 | + log.Printf("%s failed to get file size: %v", info.GetFileName(), err) |
| 197 | + //return err.ToError() |
| 198 | + } |
| 199 | + row[cobrautil.ROW_FILE_SIZE] = fmt.Sprintf("%s", humanize.IBytes(sizeRes.GetFileSize())) |
| 200 | + // Get allocated size |
| 201 | + allocRes, err := file.GetAllocatedSize(fInfoCmd.Cmd) |
| 202 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 203 | + // TODO handle err |
| 204 | + log.Printf("%s failed to get allocated size: %v", info.GetFileName(), err) |
| 205 | + //return err.ToError() |
| 206 | + } |
| 207 | + row[cobrautil.ROW_ALLOC_SIZE] = fmt.Sprintf("%s", humanize.IBytes(allocRes.GetAllocatedSize())) |
| 208 | + rows = append(rows, row) |
| 209 | + } |
| 210 | + } |
| 211 | + list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{cobrautil.ROW_OWNER, cobrautil.ROW_FILE_TYPE, cobrautil.ROW_PARENT_ID}) |
| 212 | + pCmd.TableNew.AppendBulk(list) |
| 213 | + errRet := cmderror.MergeCmdError(errors) |
| 214 | + pCmd.Error = &errRet |
| 215 | + pCmd.Result = results |
| 216 | + return nil |
| 217 | +} |
| 218 | + |
| 219 | +// ResultPlainOutput implements basecmd.FinalCurveCmdFunc |
| 220 | +func (pCmd *DirCommand) ResultPlainOutput() error { |
| 221 | + return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd) |
| 222 | +} |
0 commit comments