lsf: add metadata support with M flag

This commit is contained in:
Nick Craig-Wood 2022-05-24 11:31:48 +01:00
parent d823a38ce5
commit 776a083892
3 changed files with 25 additions and 0 deletions

View File

@ -72,6 +72,7 @@ output:
m - MimeType of object if known
e - encrypted name
T - tier of storage if known, e.g. "Hot" or "Cool"
M - Metadata of object in JSON blob format, eg {"key":"value"}
So if you wanted the path, size and modification time, you would use
` + "`--format \"pst\"`, or maybe `--format \"tsp\"`" + ` to put the path last.
@ -198,6 +199,9 @@ func Lsf(ctx context.Context, fsrc fs.Fs, out io.Writer) error {
opt.ShowOrigIDs = true
case 'T':
list.AddTier()
case 'M':
list.AddMetadata()
opt.Metadata = true
default:
return fmt.Errorf("unknown format character %q", char)
}

View File

@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/csv"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
@ -2154,6 +2155,21 @@ func (l *ListFormat) AddMimeType() {
})
}
// AddMetadata adds file's Metadata to the output if known
func (l *ListFormat) AddMetadata() {
l.AppendOutput(func(entry *ListJSONItem) string {
metadata := entry.Metadata
if metadata == nil {
metadata = make(fs.Metadata)
}
out, err := json.Marshal(metadata)
if err != nil {
return fmt.Sprintf("Failed to read metadata: %v", err.Error())
}
return string(out)
})
}
// AppendOutput adds string generated by specific function to printed output
func (l *ListFormat) AppendOutput(functionToAppend func(item *ListJSONItem) string) {
l.output = append(l.output, functionToAppend)

View File

@ -1428,6 +1428,11 @@ func TestListFormat(t *testing.T) {
assert.Contains(t, list.Format(item0), "/")
assert.Equal(t, "inode/directory", list.Format(item1))
list.SetOutput(nil)
list.AddMetadata()
assert.Equal(t, "{}", list.Format(item0))
assert.Equal(t, "{}", list.Format(item1))
list.SetOutput(nil)
list.AddPath()
list.SetAbsolute(true)