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.

64 lines
1.3 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. ctx := context.Background()
  31. if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
  32. return fmt.Errorf("%s is a directory", path)
  33. }
  34. dir, name := filer2.FullPath(path).DirAndName()
  35. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.LookupDirectoryEntryRequest{
  37. Name: name,
  38. Directory: dir,
  39. }
  40. respLookupEntry, err := client.LookupDirectoryEntry(ctx, request)
  41. if err != nil {
  42. return err
  43. }
  44. return filer2.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt32)
  45. })
  46. }