lsf: implement 'i' format for showing object ID - fixes #1476

This commit is contained in:
Nick Craig-Wood 2018-05-13 09:18:08 +01:00
parent 826975c341
commit 909c3a92d6
3 changed files with 17 additions and 0 deletions

View File

@ -63,6 +63,7 @@ output:
s - size s - size
t - modification time t - modification time
h - hash h - hash
i - ID of object if known
So if you wanted the path, size and modification time, you would use So if you wanted the path, size and modification time, you would use
--format "pst", or maybe --format "tsp" to put the path last. --format "pst", or maybe --format "tsp" to put the path last.
@ -138,6 +139,8 @@ func Lsf(fsrc fs.Fs, out io.Writer) error {
list.AddSize() list.AddSize()
case 'h': case 'h':
list.AddHash(hashType) list.AddHash(hashType)
case 'i':
list.AddID()
default: default:
return errors.Errorf("Unknown format character %q", char) return errors.Errorf("Unknown format character %q", char)
} }

View File

@ -1420,6 +1420,16 @@ func (l *ListFormat) AddHash(ht hash.Type) {
}) })
} }
// AddID adds file's ID to the output if known
func (l *ListFormat) AddID() {
l.AppendOutput(func() string {
if do, ok := l.entry.(fs.IDer); ok {
return do.ID()
}
return ""
})
}
// AppendOutput adds string generated by specific function to printed output // AppendOutput adds string generated by specific function to printed output
func (l *ListFormat) AppendOutput(functionToAppend func() string) { func (l *ListFormat) AppendOutput(functionToAppend func() string) {
if len(l.output) > 0 { if len(l.output) > 0 {

View File

@ -712,6 +712,10 @@ func TestListFormat(t *testing.T) {
list.AddModTime() list.AddModTime()
assert.Equal(t, items[0].ModTime().Local().Format("2006-01-02 15:04:05"), operations.ListFormatted(&items[0], &list)) assert.Equal(t, items[0].ModTime().Local().Format("2006-01-02 15:04:05"), operations.ListFormatted(&items[0], &list))
list.SetOutput(nil)
list.AddID()
_ = operations.ListFormatted(&items[0], &list) // Can't really check anything - at least it didn't panic!
list.SetOutput(nil) list.SetOutput(nil)
list.AddSize() list.AddSize()
assert.Equal(t, "1", operations.ListFormatted(&items[0], &list)) assert.Equal(t, "1", operations.ListFormatted(&items[0], &list))