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.

78 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. 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. if respLookupEntry.Entry == nil {
  44. return fmt.Errorf("file not found: %s", path)
  45. }
  46. m := jsonpb.Marshaler{
  47. EmitDefaults: true,
  48. Indent: " ",
  49. }
  50. text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
  51. if marshalErr != nil {
  52. return fmt.Errorf("marshal meta: %v", marshalErr)
  53. }
  54. fmt.Fprintf(writer, "%s\n", text)
  55. return nil
  56. })
  57. }