From 0eba37d8f35bef89e20218e7ce31703fdc0dc3d9 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 6 Jan 2018 18:01:29 +0000 Subject: [PATCH] lsf: add --files-only and --dirs-only flags --- cmd/lsf/lsf.go | 20 ++++++++++++++++++++ cmd/lsf/lsf_test.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/cmd/lsf/lsf.go b/cmd/lsf/lsf.go index 0f552e3cc..9f549cdc5 100644 --- a/cmd/lsf/lsf.go +++ b/cmd/lsf/lsf.go @@ -18,6 +18,8 @@ var ( dirSlash bool recurse bool hashType = fs.HashMD5 + filesOnly bool + dirsOnly bool ) func init() { @@ -27,6 +29,8 @@ func init() { flags.StringVarP(&separator, "separator", "s", ";", "Separator for the items in the format.") flags.BoolVarP(&dirSlash, "dir-slash", "d", true, "Append a slash to directory names.") flags.VarP(&hashType, "hash", "", "Use this hash when `h` is used in the format MD5|SHA-1|DropboxHash") + flags.BoolVarP(&filesOnly, "files-only", "", false, "Only list files.") + flags.BoolVarP(&dirsOnly, "dirs-only", "", false, "Only list directories.") commandDefintion.Flags().BoolVarP(&recurse, "recursive", "R", false, "Recurse into the listing.") } @@ -58,6 +62,12 @@ can be returned as an empty string if it isn't available on the object the object and "UNSUPPORTED" if that object does not support that hash type. +For example to emulate the md5sum command you can use + + rclone lsf -R --hash MD5 --format hp --separator " " --files-only . + +(Though "rclone md5sum ." is an easier way of typing this.) + By default the separator is ";" this can be changed with the --separator flag. Note that separators aren't escaped in the path so putting it last is a good strategy. @@ -100,6 +110,16 @@ func Lsf(fsrc fs.Fs, out io.Writer) error { return nil } for _, entry := range entries { + _, isDir := entry.(fs.Directory) + if isDir { + if filesOnly { + continue + } + } else { + if dirsOnly { + continue + } + } fmt.Fprintln(out, fs.ListFormatted(&entry, &list)) } return nil diff --git a/cmd/lsf/lsf_test.go b/cmd/lsf/lsf_test.go index 7f3fec1dc..55d60515c 100644 --- a/cmd/lsf/lsf_test.go +++ b/cmd/lsf/lsf_test.go @@ -112,6 +112,26 @@ subdir ;subdir `, buf.String()) + buf = new(bytes.Buffer) + format = "p" + filesOnly = true + err = Lsf(f, buf) + require.NoError(t, err) + assert.Equal(t, `file1 +file2 +file3 +`, buf.String()) + filesOnly = false + + buf = new(bytes.Buffer) + format = "p" + dirsOnly = true + err = Lsf(f, buf) + require.NoError(t, err) + assert.Equal(t, `subdir +`, buf.String()) + dirsOnly = false + buf = new(bytes.Buffer) format = "t" err = Lsf(f, buf)