You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB

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