2019-12-21 00:22:18 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandFsMetaCat{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsMetaCat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaCat) Name() string {
|
|
|
|
return "fs.meta.cat"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaCat) Help() string {
|
|
|
|
return `print out the meta data content for a file or directory
|
|
|
|
|
|
|
|
fs.meta.cat /dir/
|
|
|
|
fs.meta.cat /dir/file_name
|
|
|
|
fs.meta.cat http://<filer_server>:<port>/dir/
|
|
|
|
fs.meta.cat http://<filer_server>:<port>/dir/file_name
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
|
|
|
input := findInputDirectory(args)
|
|
|
|
|
|
|
|
filerServer, filerPort, path, err := commandEnv.parseUrl(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, name := filer2.FullPath(path).DirAndName()
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
return commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
|
2019-12-21 00:22:18 +08:00
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Name: name,
|
|
|
|
Directory: dir,
|
|
|
|
}
|
2020-02-26 13:50:12 +08:00
|
|
|
respLookupEntry, err := client.LookupDirectoryEntry(context.Background(), request)
|
2019-12-21 00:22:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-26 03:13:06 +08:00
|
|
|
if respLookupEntry.Entry == nil {
|
|
|
|
return fmt.Errorf("file not found: %s", path)
|
|
|
|
}
|
2019-12-21 00:22:18 +08:00
|
|
|
|
|
|
|
m := jsonpb.Marshaler{
|
|
|
|
EmitDefaults: true,
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
|
|
|
|
text, marshalErr := m.MarshalToString(respLookupEntry.Entry)
|
|
|
|
if marshalErr != nil {
|
|
|
|
return fmt.Errorf("marshal meta: %v", marshalErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(writer, "%s\n", text)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|