command_fs_meta_cat.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/golang/protobuf/jsonpb"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandFsMetaCat{})
  12. }
  13. type commandFsMetaCat struct {
  14. }
  15. func (c *commandFsMetaCat) Name() string {
  16. return "fs.meta.cat"
  17. }
  18. func (c *commandFsMetaCat) Help() string {
  19. return `print out the meta data content for a file or directory
  20. fs.meta.cat /dir/
  21. fs.meta.cat /dir/file_name
  22. fs.meta.cat http://<filer_server>:<port>/dir/
  23. fs.meta.cat http://<filer_server>:<port>/dir/file_name
  24. `
  25. }
  26. func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. input := findInputDirectory(args)
  28. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  29. if err != nil {
  30. return err
  31. }
  32. ctx := context.Background()
  33. dir, name := filer2.FullPath(path).DirAndName()
  34. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  35. request := &filer_pb.LookupDirectoryEntryRequest{
  36. Name: name,
  37. Directory: dir,
  38. }
  39. respLookupEntry, err := client.LookupDirectoryEntry(ctx, request)
  40. if err != nil {
  41. return err
  42. }
  43. m := jsonpb.Marshaler{
  44. EmitDefaults: true,
  45. Indent: " ",
  46. }
  47. text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
  48. if marshalErr != nil {
  49. return fmt.Errorf("marshal meta: %v", marshalErr)
  50. }
  51. fmt.Fprintf(writer, "%s\n", text)
  52. return nil
  53. })
  54. }