Skip to content

Commit 443ac8b

Browse files
committed
feat: bs list dir +flag & fix go.mod replace & list dir cmd
1 parent f856f13 commit 443ac8b

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

tools-v2/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/opencurve/curve/tools-v2
22

33
go 1.19
44

5+
replace github.com/optiopay/kafka => github.com/cilium/kafka v0.0.0-20180809090225-01ce283b732b
6+
57
require (
68
github.com/cilium/cilium v1.12.1
79
github.com/deckarep/golang-set/v2 v2.1.0

tools-v2/internal/utils/row.go

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const (
4040
ROW_EXPLAIN = "explain"
4141
ROW_EXTERNAL_ADDR = "externalAddr"
4242
ROW_FILE_SIZE = "fileSize"
43+
ROW_FILE_TYPE = "fileType"
44+
ROW_FILE_NAME = "fileName"
4345
ROW_FS_ID = "fsId"
4446
ROW_FS_NAME = "fsName"
4547
ROW_FS_TYPE = "fsType"
@@ -64,6 +66,7 @@ const (
6466
ROW_ORIGINAL_PATH = "originalPath"
6567
ROW_OWNER = "owner"
6668
ROW_PARENT = "parent"
69+
ROW_PARENT_ID = "parentId"
6770
ROW_PARTITION_ID = "partitionId"
6871
ROW_PEER_ADDR = "peerAddr"
6972
ROW_PEER_ID = "peerId"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}

tools-v2/pkg/cli/command/curvebs/list/list.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package list
2525
import (
2626
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
2727
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/client"
28+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/dir"
2829
logicalpool "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/logicalPool"
2930
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/server"
3031
"github.com/spf13/cobra"
@@ -41,6 +42,7 @@ func (listCmd *ListCommand) AddSubCommands() {
4142
logicalpool.NewLogicalPoolCommand(),
4243
server.NewServerCommand(),
4344
client.NewClientCommand(),
45+
dir.NewDirCommand(),
4446
)
4547
}
4648

tools-v2/pkg/config/bs.go

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
CURVEBS_PASSWORD = "password"
4848
VIPER_CURVEBS_PASSWORD = "curvebs.root.password"
4949
CURVEBS_DEFAULT_PASSWORD = "root_password"
50+
CURVEBS_FILENAME = "fileName"
5051
)
5152

5253
var (
@@ -126,6 +127,11 @@ func AddBsPasswordOptionFlag(cmd *cobra.Command) {
126127
AddBsStringOptionFlag(cmd, CURVEBS_PASSWORD, "user password")
127128
}
128129

130+
// fileName
131+
func AddBsFileNameOptionFlag(cmd *cobra.Command) {
132+
AddBsStringOptionFlag(cmd, CURVEBS_FILENAME, "file name")
133+
}
134+
129135
// etcd
130136
func AddBsEtcdAddrFlag(cmd *cobra.Command) {
131137
AddBsStringOptionFlag(cmd, CURVEBS_ETCDADDR, "etcd address, should be like 127.0.0.1:8700,127.0.0.1:8701,127.0.0.1:8702")

0 commit comments

Comments
 (0)