command_fs_cat.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "math"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. path, err := commandEnv.parseUrl(findInputDirectory(args))
  25. if err != nil {
  26. return err
  27. }
  28. if commandEnv.isDirectory(path) {
  29. return fmt.Errorf("%s is a directory", path)
  30. }
  31. dir, name := util.FullPath(path).DirAndName()
  32. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  33. request := &filer_pb.LookupDirectoryEntryRequest{
  34. Name: name,
  35. Directory: dir,
  36. }
  37. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  38. if err != nil {
  39. return err
  40. }
  41. if len(respLookupEntry.Entry.Content) > 0 {
  42. _, err = writer.Write(respLookupEntry.Entry.Content)
  43. return err
  44. }
  45. return filer.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  46. })
  47. }