cmd: new command lsf
This commit is contained in:
committed by
Nick Craig-Wood
parent
8f47d7fc06
commit
0d041602cf
@@ -11,6 +11,7 @@ import (
|
||||
"mime"
|
||||
"path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -1784,3 +1785,66 @@ func MoveFile(fdst Fs, fsrc Fs, dstFileName string, srcFileName string) (err err
|
||||
func CopyFile(fdst Fs, fsrc Fs, dstFileName string, srcFileName string) (err error) {
|
||||
return moveOrCopyFile(fdst, fsrc, dstFileName, srcFileName, true)
|
||||
}
|
||||
|
||||
// ListFormat defines files information print format
|
||||
type ListFormat struct {
|
||||
separator string
|
||||
dirSlash bool
|
||||
output []func() string
|
||||
entry DirEntry
|
||||
}
|
||||
|
||||
// SetSeparator changes separator in struct
|
||||
func (l *ListFormat) SetSeparator(separator string) {
|
||||
l.separator = separator
|
||||
}
|
||||
|
||||
// SetDirSlash defines if slash should be printed
|
||||
func (l *ListFormat) SetDirSlash(dirSlash bool) {
|
||||
l.dirSlash = dirSlash
|
||||
}
|
||||
|
||||
// SetOutput sets functions used to create files information
|
||||
func (l *ListFormat) SetOutput(output []func() string) {
|
||||
l.output = output
|
||||
}
|
||||
|
||||
// AddModTime adds file's Mod Time to output
|
||||
func (l *ListFormat) AddModTime() {
|
||||
l.AppendOutput(func() string { return l.entry.ModTime().Format("2006-01-02 15:04:05") })
|
||||
}
|
||||
|
||||
// AddSize adds file's size to output
|
||||
func (l *ListFormat) AddSize() {
|
||||
l.AppendOutput(func() string { return strconv.FormatInt(l.entry.Size(), 10) })
|
||||
}
|
||||
|
||||
// AddPath adds path to file to output
|
||||
func (l *ListFormat) AddPath() {
|
||||
l.AppendOutput(func() string {
|
||||
_, isDir := l.entry.(Directory)
|
||||
|
||||
if isDir && l.dirSlash {
|
||||
return l.entry.Remote() + "/"
|
||||
}
|
||||
return l.entry.Remote()
|
||||
})
|
||||
}
|
||||
|
||||
// AppendOutput adds string generated by specific function to printed output
|
||||
func (l *ListFormat) AppendOutput(functionToAppend func() string) {
|
||||
if len(l.output) > 0 {
|
||||
l.output = append(l.output, func() string { return l.separator })
|
||||
}
|
||||
l.output = append(l.output, functionToAppend)
|
||||
}
|
||||
|
||||
// ListFormatted prints information about specific file in specific format
|
||||
func ListFormatted(entry *DirEntry, list *ListFormat) string {
|
||||
list.entry = *entry
|
||||
var out string
|
||||
for _, fun := range list.output {
|
||||
out += fun()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -958,3 +958,45 @@ func TestCheckEqualReaders(t *testing.T) {
|
||||
assert.Equal(t, myErr, err)
|
||||
assert.Equal(t, differ, true)
|
||||
}
|
||||
|
||||
func TestListFormat(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
file1 := r.WriteObject("a", "a", t1)
|
||||
file2 := r.WriteObject("subdir/b", "b", t1)
|
||||
|
||||
fstest.CheckItems(t, r.Fremote, file1, file2)
|
||||
|
||||
items, _ := fs.ListDirSorted(r.Fremote, true, "")
|
||||
var list fs.ListFormat
|
||||
list.AddPath()
|
||||
list.SetDirSlash(false)
|
||||
assert.Equal(t, "subdir", fs.ListFormatted(&items[1], &list))
|
||||
|
||||
list.SetDirSlash(true)
|
||||
assert.Equal(t, "subdir/", fs.ListFormatted(&items[1], &list))
|
||||
|
||||
list.SetOutput(nil)
|
||||
assert.Equal(t, "", fs.ListFormatted(&items[1], &list))
|
||||
|
||||
list.AppendOutput(func() string { return "a" })
|
||||
list.AppendOutput(func() string { return "b" })
|
||||
assert.Equal(t, "ab", fs.ListFormatted(&items[1], &list))
|
||||
list.SetSeparator(":::")
|
||||
assert.Equal(t, "a:::b", fs.ListFormatted(&items[1], &list))
|
||||
|
||||
list.SetOutput(nil)
|
||||
list.AddModTime()
|
||||
assert.Equal(t, items[0].ModTime().Format("2006-01-02 15:04:05"), fs.ListFormatted(&items[0], &list))
|
||||
|
||||
list.SetOutput(nil)
|
||||
list.AddSize()
|
||||
assert.Equal(t, "1", fs.ListFormatted(&items[0], &list))
|
||||
|
||||
list.AddPath()
|
||||
list.AddModTime()
|
||||
list.SetDirSlash(true)
|
||||
list.SetSeparator("__SEP__")
|
||||
assert.Equal(t, "1__SEP__a__SEP__"+items[0].ModTime().Format("2006-01-02 15:04:05"), fs.ListFormatted(&items[0], &list))
|
||||
assert.Equal(t, "-1__SEP__subdir/__SEP__"+items[1].ModTime().Format("2006-01-02 15:04:05"), fs.ListFormatted(&items[1], &list))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user