lsjson: add --metadata/-M flag

Note that this removes the `-M` flag from `--encrypted` as it
conflicted with the global flag and adds it to `--metadata`.
This commit is contained in:
Nick Craig-Wood
2022-05-24 11:16:29 +01:00
parent 78d52882ca
commit d823a38ce5
4 changed files with 29 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ type ListJSONItem struct {
OrigID string `json:",omitempty"`
Tier string `json:",omitempty"`
IsBucket bool `json:",omitempty"`
Metadata fs.Metadata `json:",omitempty"`
}
// Timestamp a time in the provided format
@@ -80,6 +81,7 @@ type ListJSONOpt struct {
ShowHash bool `json:"showHash"`
DirsOnly bool `json:"dirsOnly"`
FilesOnly bool `json:"filesOnly"`
Metadata bool `json:"metadata"`
HashTypes []string `json:"hashTypes"` // hash types to show if ShowHash is set, e.g. "MD5", "SHA-1"
}
@@ -222,6 +224,14 @@ func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem
item.Tier = do.GetTier()
}
}
if lj.opt.Metadata {
metadata, err := fs.GetMetadata(ctx, x)
if err != nil {
fs.Errorf(x, "Failed to read metadata: %v", err)
} else if metadata != nil {
item.Metadata = metadata
}
}
default:
fs.Errorf(nil, "Unknown type %T in listing in ListJSON", entry)
}

View File

@@ -172,6 +172,19 @@ func TestListJSON(t *testing.T) {
ModTime: operations.Timestamp{When: t1},
IsDir: false,
}},
}, {
name: "Metadata",
opt: operations.ListJSONOpt{
FilesOnly: true,
Metadata: true,
},
want: []*operations.ListJSONItem{{
Path: "file1",
Name: "file1",
Size: 5,
ModTime: operations.Timestamp{When: t1},
IsDir: false,
}},
},
} {
t.Run(test.name, func(t *testing.T) {

View File

@@ -34,6 +34,7 @@ func init() {
- noMimeType - If set don't show mime types
- dirsOnly - If set only show directories
- filesOnly - If set only show files
- metadata - If set return metadata of objects also
- hashTypes - array of strings of hash types to show if showHash set
Returns: