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.

69 lines
1.5 KiB

6 years ago
  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. weed_server "github.com/chrislusf/seaweedfs/weed/server"
  10. )
  11. func init() {
  12. commands = append(commands, &commandFsCat{})
  13. }
  14. type commandFsCat struct {
  15. }
  16. func (c *commandFsCat) Name() string {
  17. return "fs.cat"
  18. }
  19. func (c *commandFsCat) Help() string {
  20. return `stream the file content on to the screen
  21. fs.cat /dir/
  22. fs.cat /dir/file_name
  23. fs.cat /dir/file_prefix
  24. fs.cat http://<filer_server>:<port>/dir/
  25. fs.cat http://<filer_server>:<port>/dir/file_name
  26. fs.cat http://<filer_server>:<port>/dir/file_prefix
  27. `
  28. }
  29. func (c *commandFsCat) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  30. input := findInputDirectory(args)
  31. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  32. if err != nil {
  33. return err
  34. }
  35. ctx := context.Background()
  36. if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
  37. return fmt.Errorf("%s is a directory", path)
  38. }
  39. dir, name := filer2.FullPath(path).DirAndName()
  40. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  41. request := &filer_pb.LookupDirectoryEntryRequest{
  42. Name: name,
  43. Directory: dir,
  44. }
  45. respLookupEntry, err := client.LookupDirectoryEntry(ctx, request)
  46. if err != nil {
  47. return err
  48. }
  49. return weed_server.StreamContent(commandEnv.masterClient, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt32)
  50. })
  51. }