1
mirror of https://github.com/rclone/rclone synced 2024-12-19 09:05:56 +01:00

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

Factored common code from lsf/lsjson into operations.ListJSON
This commit is contained in:
calistri 2019-04-05 16:24:09 -04:00 committed by Nick Craig-Wood
parent 120de505a9
commit 5855714474
3 changed files with 23 additions and 9 deletions

View File

@ -164,6 +164,8 @@ func Lsf(fsrc fs.Fs, out io.Writer) error {
list.SetAbsolute(absolute) list.SetAbsolute(absolute)
var opt = operations.ListJSONOpt{ var opt = operations.ListJSONOpt{
NoModTime: true, NoModTime: true,
DirsOnly: dirsOnly,
FilesOnly: filesOnly,
Recurse: recurse, 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 { 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)) _, _ = fmt.Fprintln(out, list.Format(item))
return nil return nil
}) })

View File

@ -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.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.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.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{ 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 --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. 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" 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". will be "subfolder/file.txt", not "remote:path/subfolder/file.txt".

View File

@ -70,6 +70,8 @@ type ListJSONOpt struct {
ShowEncrypted bool `json:"showEncrypted"` ShowEncrypted bool `json:"showEncrypted"`
ShowOrigIDs bool `json:"showOrigIDs"` ShowOrigIDs bool `json:"showOrigIDs"`
ShowHash bool `json:"showHash"` 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 // 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()) format := formatForPrecision(fsrc.Precision())
err := walk.ListR(fsrc, remote, false, ConfigMaxDepth(opt.Recurse), walk.ListAll, func(entries fs.DirEntries) (err error) { err := walk.ListR(fsrc, remote, false, ConfigMaxDepth(opt.Recurse), walk.ListAll, func(entries fs.DirEntries) (err error) {
for _, entry := range entries { 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{ item := ListJSONItem{
Path: entry.Remote(), Path: entry.Remote(),
Name: path.Base(entry.Remote()), Name: path.Base(entry.Remote()),