From 776a08389244335ad05d5b9a0825c1f4ade555e1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 24 May 2022 11:31:48 +0100 Subject: [PATCH] lsf: add metadata support with `M` flag --- cmd/lsf/lsf.go | 4 ++++ fs/operations/operations.go | 16 ++++++++++++++++ fs/operations/operations_test.go | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/cmd/lsf/lsf.go b/cmd/lsf/lsf.go index 732d2b5e8..e370f930c 100644 --- a/cmd/lsf/lsf.go +++ b/cmd/lsf/lsf.go @@ -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) } diff --git a/fs/operations/operations.go b/fs/operations/operations.go index a41a0a009..1ea41853f 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -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) diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index 4b65f6894..8cd7296e4 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -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)