command_fs_cat.go 1.3 KB

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