2019-04-22 06:43:43 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-02-08 15:08:54 +08:00
|
|
|
"io"
|
2019-04-22 06:43:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 16:30:24 +08:00
|
|
|
Commands = append(Commands, &commandFsCat{})
|
2019-04-22 06:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsCat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsCat) Name() string {
|
|
|
|
return "fs.cat"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsCat) Help() string {
|
|
|
|
return `stream the file content on to the screen
|
|
|
|
|
|
|
|
fs.cat /dir/file_name
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-04-22 06:43:43 +08:00
|
|
|
|
2020-03-24 12:26:15 +08:00
|
|
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
2019-04-22 06:43:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:26:15 +08:00
|
|
|
if commandEnv.isDirectory(path) {
|
2019-04-22 06:43:43 +08:00
|
|
|
return fmt.Errorf("%s is a directory", path)
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.FullPath(path).DirAndName()
|
2019-04-22 06:43:43 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2019-04-22 06:43:43 +08:00
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Name: name,
|
|
|
|
Directory: dir,
|
|
|
|
}
|
2020-03-08 08:51:46 +08:00
|
|
|
respLookupEntry, err := filer_pb.LookupEntry(client, request)
|
2019-04-22 06:43:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-24 17:38:32 +08:00
|
|
|
if len(respLookupEntry.Entry.Content) > 0 {
|
|
|
|
_, err = writer.Write(respLookupEntry.Entry.Content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:33:36 +08:00
|
|
|
return filer.StreamContent(commandEnv.MasterClient, writer, respLookupEntry.Entry.GetChunks(), 0, int64(filer.FileSize(respLookupEntry.Entry)))
|
2019-04-22 06:43:43 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|