lsf: add --absolute flag to add a leading / onto path names

This commit is contained in:
Nick Craig-Wood 2018-06-03 10:42:34 +01:00
parent 5302e5f9b1
commit 3ef938ebde
3 changed files with 33 additions and 5 deletions

View File

@ -24,6 +24,7 @@ var (
filesOnly bool
dirsOnly bool
csv bool
absolute bool
)
func init() {
@ -36,6 +37,7 @@ func init() {
flags.BoolVarP(&filesOnly, "files-only", "", false, "Only list files.")
flags.BoolVarP(&dirsOnly, "dirs-only", "", false, "Only list directories.")
flags.BoolVarP(&csv, "csv", "", false, "Output in CSV format.")
flags.BoolVarP(&absolute, "absolute", "", false, "Put a leading / in front of path names.")
commandDefintion.Flags().BoolVarP(&recurse, "recursive", "R", false, "Recurse into the listing.")
}
@ -125,6 +127,15 @@ Eg
test.sh,449
"this file contains a comma, in the file name.txt",6
Note that the --absolute parameter is useful for making lists of files
to pass to an rclone copy with the --files-from flag.
For example to find all the files modified within one day and copy
those only (without traversing the whole directory structure):
rclone lsf --absolute --files-only --max-age 1d /path/to/local > new_files
rclone copy --files-from new_files /path/to/local remote:path
` + lshelp.Help,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
@ -149,6 +160,7 @@ func Lsf(fsrc fs.Fs, out io.Writer) error {
list.SetSeparator(separator)
list.SetCSV(csv)
list.SetDirSlash(dirSlash)
list.SetAbsolute(absolute)
for _, char := range format {
switch char {

View File

@ -1372,6 +1372,7 @@ func CopyFile(fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string) (e
type ListFormat struct {
separator string
dirSlash bool
absolute bool
output []func() string
entry fs.DirEntry
csv *csv.Writer
@ -1388,6 +1389,11 @@ func (l *ListFormat) SetDirSlash(dirSlash bool) {
l.dirSlash = dirSlash
}
// SetAbsolute prints a leading slash in front of path names
func (l *ListFormat) SetAbsolute(absolute bool) {
l.absolute = absolute
}
// SetCSV defines if the output should be csv
//
// Note that you should call SetSeparator before this if you want a
@ -1423,12 +1429,15 @@ func (l *ListFormat) AddSize() {
// AddPath adds path to file to output
func (l *ListFormat) AddPath() {
l.AppendOutput(func() string {
_, isDir := l.entry.(fs.Directory)
if isDir && l.dirSlash {
return l.entry.Remote() + "/"
remote := l.entry.Remote()
if l.absolute && !strings.HasPrefix(remote, "/") {
remote = "/" + remote
}
return l.entry.Remote()
_, isDir := l.entry.(fs.Directory)
if isDir && l.dirSlash {
remote += "/"
}
return remote
})
}

View File

@ -726,6 +726,13 @@ func TestListFormat(t *testing.T) {
assert.Contains(t, list.Format(items[0]), "/")
assert.Equal(t, "inode/directory", list.Format(items[1]))
list.SetOutput(nil)
list.AddPath()
list.SetAbsolute(true)
assert.Equal(t, "/a", list.Format(items[0]))
list.SetAbsolute(false)
assert.Equal(t, "a", list.Format(items[0]))
list.SetOutput(nil)
list.AddSize()
assert.Equal(t, "1", list.Format(items[0]))