command_fs_cat.go 1.3 KB

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