command_fs_cat.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandFsCat{})
  12. }
  13. type commandFsCat struct {
  14. }
  15. func (c *commandFsCat) Name() string {
  16. return "fs.cat"
  17. }
  18. func (c *commandFsCat) Help() string {
  19. return `stream the file content on to the screen
  20. fs.cat /dir/file_name
  21. `
  22. }
  23. func (c *commandFsCat) HasTag(CommandTag) bool {
  24. return false
  25. }
  26. func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. path, err := commandEnv.parseUrl(findInputDirectory(args))
  28. if err != nil {
  29. return err
  30. }
  31. if commandEnv.isDirectory(path) {
  32. return fmt.Errorf("%s is a directory", path)
  33. }
  34. dir, name := util.FullPath(path).DirAndName()
  35. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.LookupDirectoryEntryRequest{
  37. Name: name,
  38. Directory: dir,
  39. }
  40. respLookupEntry, err := filer_pb.LookupEntry(context.Background(), client, request)
  41. if err != nil {
  42. return err
  43. }
  44. if len(respLookupEntry.Entry.Content) > 0 {
  45. _, err = writer.Write(respLookupEntry.Entry.Content)
  46. return err
  47. }
  48. return filer.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.GetChunks(), 0, int64(filer.FileSize(respLookupEntry.Entry)))
  49. })
  50. }