Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

lsjson: added --files-only and --dirs-only flags

calistri585571447431
diff --git a/cmd/lsf/lsf.go b/cmd/lsf/lsf.go
index c2f07c8c8..21fc38690 100644
--- a/cmd/lsf/lsf.go
+++ b/cmd/lsf/lsf.go
@@ -164,6 +164,8 @@ func Lsf(fsrc fs.Fs, out io.Writer) error {
list.SetAbsolute(absolute)
var opt = operations.ListJSONOpt{
NoModTime: true,
+ DirsOnly: dirsOnly,
+ FilesOnly: filesOnly,
Recurse: recurse,
}
@@ -195,15 +197,6 @@ func Lsf(fsrc fs.Fs, out io.Writer) error {
}
return operations.ListJSON(fsrc, "", &opt, func(item *operations.ListJSONItem) error {
- if item.IsDir {
- if filesOnly {
- return nil
- }
- } else {
- if dirsOnly {
- return nil
- }
- }
_, _ = fmt.Fprintln(out, list.Format(item))
return nil
})
diff --git a/cmd/lsjson/lsjson.go b/cmd/lsjson/lsjson.go
index 28a38982f..21bf90bfe 100644
--- a/cmd/lsjson/lsjson.go
+++ b/cmd/lsjson/lsjson.go
@@ -23,6 +23,8 @@ func init() {
commandDefintion.Flags().BoolVarP(&opt.NoModTime, "no-modtime", "", false, "Don't read the modification time (can speed things up).")
commandDefintion.Flags().BoolVarP(&opt.ShowEncrypted, "encrypted", "M", false, "Show the encrypted names.")
commandDefintion.Flags().BoolVarP(&opt.ShowOrigIDs, "original", "", false, "Show the ID of the underlying Object.")
+ commandDefintion.Flags().BoolVarP(&opt.FilesOnly, "files-only", "", false, "Show only files in the listing.")
+ commandDefintion.Flags().BoolVarP(&opt.DirsOnly, "dirs-only", "", false, "Show only directories in the listing.")
}
var commandDefintion = &cobra.Command{
@@ -55,6 +57,10 @@ If --no-modtime is specified then ModTime will be blank.
If --encrypted is not specified the Encrypted won't be emitted.
+If --dirs-only is not specified files in addition to directories are returned
+
+If --files-only is not specified directories in addition to the files will be returned.
+
The Path field will only show folders below the remote path being listed.
If "remote:path" contains the file "subfolder/file.txt", the Path for "file.txt"
will be "subfolder/file.txt", not "remote:path/subfolder/file.txt".
diff --git a/fs/operations/lsjson.go b/fs/operations/lsjson.go
index a5ad7eacb..876a71ef5 100644
--- a/fs/operations/lsjson.go
+++ b/fs/operations/lsjson.go
@@ -70,6 +70,8 @@ type ListJSONOpt struct {
ShowEncrypted bool `json:"showEncrypted"`
ShowOrigIDs bool `json:"showOrigIDs"`
ShowHash bool `json:"showHash"`
+ DirsOnly bool `json:"dirsOnly"`
+ FilesOnly bool `json:"filesOnly"`
}
// ListJSON lists fsrc using the options in opt calling callback for each item
@@ -91,6 +93,19 @@ func ListJSON(fsrc fs.Fs, remote string, opt *ListJSONOpt, callback func(*ListJS
format := formatForPrecision(fsrc.Precision())
err := walk.ListR(fsrc, remote, false, ConfigMaxDepth(opt.Recurse), walk.ListAll, func(entries fs.DirEntries) (err error) {
for _, entry := range entries {
+ switch entry.(type) {
+ case fs.Directory:
+ if opt.FilesOnly {
+ continue
+ }
+ case fs.Object:
+ if opt.DirsOnly {
+ continue
+ }
+ default:
+ fs.Errorf(nil, "Unknown type %T in listing", entry)
+ }
+
item := ListJSONItem{
Path: entry.Remote(),
Name: path.Base(entry.Remote()),