From 7f1e3c843ddc8d6880985f0a32f37cfb19b27c3c Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 23 Mar 2020 01:14:21 -0700 Subject: [PATCH] refactoring --- weed/pb/filer_pb/filer_client.go | 18 +++++++++ weed/shell/command_bucket_delete.go | 29 ++++----------- weed/shell/command_bucket_list.go | 58 ++++++++++++++--------------- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/weed/pb/filer_pb/filer_client.go b/weed/pb/filer_pb/filer_client.go index 1a92b452d..fd88d3d77 100644 --- a/weed/pb/filer_pb/filer_client.go +++ b/weed/pb/filer_pb/filer_client.go @@ -203,3 +203,21 @@ func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string return nil }) } + +func Remove(filerClient FilerClient, parentDirectoryPath string, name string, isDeleteData, isRecursive, ignoreRecursiveErr bool) error { + return filerClient.WithFilerClient(func(client SeaweedFilerClient) error { + + if _, err := client.DeleteEntry(context.Background(), &DeleteEntryRequest{ + Directory: parentDirectoryPath, + Name: name, + IsDeleteData: isDeleteData, + IsRecursive: isRecursive, + IgnoreRecursiveError: ignoreRecursiveErr, + }); err != nil { + return err + } + + return nil + + }) +} diff --git a/weed/shell/command_bucket_delete.go b/weed/shell/command_bucket_delete.go index c57ce7221..509b3c1de 100644 --- a/weed/shell/command_bucket_delete.go +++ b/weed/shell/command_bucket_delete.go @@ -1,7 +1,6 @@ package shell import ( - "context" "flag" "fmt" "io" @@ -44,28 +43,14 @@ func (c *commandBucketDelete) Do(args []string, commandEnv *CommandEnv, writer i return parseErr } - err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error { + filerClient := commandEnv.getFilerClient(filerServer, filerPort) - resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{}) - if err != nil { - return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err) - } - filerBucketsPath := resp.DirBuckets + var filerBucketsPath string + filerBucketsPath, err = readFilerBucketsPath(filerClient) + if err != nil { + return fmt.Errorf("read buckets: %v", err) + } - if _, err := client.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{ - Directory: filerBucketsPath, - Name: *bucketName, - IsDeleteData: false, - IsRecursive: true, - IgnoreRecursiveError: true, - }); err != nil { - return err - } - - return nil - - }) - - return err + return filer_pb.Remove(filerClient, filerBucketsPath, *bucketName, false, true, true) } diff --git a/weed/shell/command_bucket_list.go b/weed/shell/command_bucket_list.go index 5eb5972ce..486d40fba 100644 --- a/weed/shell/command_bucket_list.go +++ b/weed/shell/command_bucket_list.go @@ -39,43 +39,41 @@ func (c *commandBucketList) Do(args []string, commandEnv *CommandEnv, writer io. return parseErr } - err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error { + filerClient := commandEnv.getFilerClient(filerServer, filerPort) + + var filerBucketsPath string + filerBucketsPath, err = readFilerBucketsPath(filerClient) + if err != nil { + return fmt.Errorf("read buckets: %v", err) + } + + err = filer_pb.List(filerClient, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) { + if entry.Attributes.Replication == "" || entry.Attributes.Replication == "000" { + fmt.Fprintf(writer, " %s\n", entry.Name) + } else { + fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", entry.Name, entry.Attributes.Replication) + } + }, "", false, math.MaxUint32) + if err != nil { + return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err) + } + + return err + +} + +func readFilerBucketsPath(filerClient filer_pb.FilerClient) (filerBucketsPath string, err error) { + err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error { resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{}) if err != nil { - return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err) - } - filerBucketsPath := resp.DirBuckets - - stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{ - Directory: filerBucketsPath, - Limit: math.MaxUint32, - }) - if err != nil { - return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err) - } - - for { - resp, recvErr := stream.Recv() - if recvErr != nil { - if recvErr == io.EOF { - break - } else { - return recvErr - } - } - - if resp.Entry.Attributes.Replication == "" || resp.Entry.Attributes.Replication == "000" { - fmt.Fprintf(writer, " %s\n", resp.Entry.Name) - } else { - fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", resp.Entry.Name, resp.Entry.Attributes.Replication) - } + return fmt.Errorf("get filer configuration: %v", err) } + filerBucketsPath = resp.DirBuckets return nil }) - return err - + return filerBucketsPath, err }