command_fs_cat.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  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/
  21. fs.cat /dir/file_name
  22. fs.cat /dir/file_prefix
  23. fs.cat http://<filer_server>:<port>/dir/
  24. fs.cat http://<filer_server>:<port>/dir/file_name
  25. fs.cat http://<filer_server>:<port>/dir/file_prefix
  26. `
  27. }
  28. func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. input := findInputDirectory(args)
  30. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  31. if err != nil {
  32. return err
  33. }
  34. ctx := context.Background()
  35. if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
  36. return fmt.Errorf("%s is a directory", path)
  37. }
  38. dir, name := filer2.FullPath(path).DirAndName()
  39. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  40. request := &filer_pb.LookupDirectoryEntryRequest{
  41. Name: name,
  42. Directory: dir,
  43. }
  44. respLookupEntry, err := client.LookupDirectoryEntry(ctx, request)
  45. if err != nil {
  46. return err
  47. }
  48. return filer2.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt32)
  49. })
  50. }