command_fs_meta_cat.go 1.4 KB

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