command_fs_cat.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) HasTag(CommandTag) bool {
  23. return false
  24. }
  25. func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. path, err := commandEnv.parseUrl(findInputDirectory(args))
  27. if err != nil {
  28. return err
  29. }
  30. if commandEnv.isDirectory(path) {
  31. return fmt.Errorf("%s is a directory", path)
  32. }
  33. dir, name := util.FullPath(path).DirAndName()
  34. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  35. request := &filer_pb.LookupDirectoryEntryRequest{
  36. Name: name,
  37. Directory: dir,
  38. }
  39. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  40. if err != nil {
  41. return err
  42. }
  43. if len(respLookupEntry.Entry.Content) > 0 {
  44. _, err = writer.Write(respLookupEntry.Entry.Content)
  45. return err
  46. }
  47. return filer.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.GetChunks(), 0, int64(filer.FileSize(respLookupEntry.Entry)))
  48. })
  49. }