You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.6 KiB

  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. dir, name := filer2.FullPath(path).DirAndName()
  33. return commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  34. request := &filer_pb.LookupDirectoryEntryRequest{
  35. Name: name,
  36. Directory: dir,
  37. }
  38. respLookupEntry, err := client.LookupDirectoryEntry(context.Background(), request)
  39. if err != nil {
  40. return err
  41. }
  42. if respLookupEntry.Entry == nil {
  43. return fmt.Errorf("file not found: %s", path)
  44. }
  45. m := jsonpb.Marshaler{
  46. EmitDefaults: true,
  47. Indent: " ",
  48. }
  49. text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
  50. if marshalErr != nil {
  51. return fmt.Errorf("marshal meta: %v", marshalErr)
  52. }
  53. fmt.Fprintf(writer, "%s\n", text)
  54. return nil
  55. })
  56. }