refactoring

This commit is contained in:
Chris Lu 2020-03-23 01:14:21 -07:00
parent c07bcd5065
commit 7f1e3c843d
3 changed files with 53 additions and 52 deletions

View file

@ -203,3 +203,21 @@ func MkFile(filerClient FilerClient, parentDirectoryPath string, fileName string
return nil 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
})
}

View file

@ -1,7 +1,6 @@
package shell package shell
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -44,28 +43,14 @@ func (c *commandBucketDelete) Do(args []string, commandEnv *CommandEnv, writer i
return parseErr 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{}) var filerBucketsPath string
filerBucketsPath, err = readFilerBucketsPath(filerClient)
if err != nil { if err != nil {
return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err) return fmt.Errorf("read buckets: %v", err)
}
filerBucketsPath := resp.DirBuckets
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 filer_pb.Remove(filerClient, filerBucketsPath, *bucketName, false, true, true)
})
return err
} }

View file

@ -39,43 +39,41 @@ func (c *commandBucketList) Do(args []string, commandEnv *CommandEnv, writer io.
return parseErr 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{}) var filerBucketsPath string
filerBucketsPath, err = readFilerBucketsPath(filerClient)
if err != nil { if err != nil {
return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err) return fmt.Errorf("read buckets: %v", err)
} }
filerBucketsPath := resp.DirBuckets
stream, err := client.ListEntries(context.Background(), &filer_pb.ListEntriesRequest{ err = filer_pb.List(filerClient, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) {
Directory: filerBucketsPath, if entry.Attributes.Replication == "" || entry.Attributes.Replication == "000" {
Limit: math.MaxUint32, 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 { if err != nil {
return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err) return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
} }
for { return err
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" { func readFilerBucketsPath(filerClient filer_pb.FilerClient) (filerBucketsPath string, err error) {
fmt.Fprintf(writer, " %s\n", resp.Entry.Name) err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
} else {
fmt.Fprintf(writer, " %s\t\t\treplication: %s\n", resp.Entry.Name, resp.Entry.Attributes.Replication) resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
} if err != nil {
return fmt.Errorf("get filer configuration: %v", err)
} }
filerBucketsPath = resp.DirBuckets
return nil return nil
}) })
return err return filerBucketsPath, err
} }