Skip to content

Commit

Permalink
Merge pull request #11 from waits/ls-on-files
Browse files Browse the repository at this point in the history
Support `ls` on files
  • Loading branch information
diwakergupta committed Jun 7, 2016
2 parents 651d726 + 44f15d7 commit 9919edb
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ import (
"fmt"
"io"
"os"
"strings"
"text/tabwriter"

"github.com/dropbox/dropbox-sdk-go-unofficial/files"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

// Sends a get_metadata request for a given path and returns the response
func getFileMetadata(path string) (*files.Metadata, error) {
arg := files.NewGetMetadataArg(path)

res, err := dbx.GetMetadata(arg)
if err != nil {
return nil, err
}

return res, nil
}

func printFolderMetadata(w io.Writer, e *files.FolderMetadata, longFormat bool) {
if longFormat {
fmt.Fprintf(w, "-\t-\t-\t")
Expand All @@ -43,28 +56,41 @@ func ls(cmd *cobra.Command, args []string) (err error) {
path := ""
if len(args) > 0 {
if path, err = validatePath(args[0]); err != nil {
return
return err
}
}

arg := files.NewListFolderArg(path)

res, err := dbx.ListFolder(arg)
var entries []*files.Metadata
if err != nil {
return
}

entries := res.Entries

for res.HasMore {
arg := files.NewListFolderContinueArg(res.Cursor)
// Don't treat a "not_folder" error as fatal; recover by sending a
// get_metadata request for the same path and using that response instead.
if strings.Contains(err.Error(), "path/not_folder/") {
var metaRes *files.Metadata
metaRes, err = getFileMetadata(path)
entries = []*files.Metadata{metaRes}
}

res, err = dbx.ListFolderContinue(arg)
// Return if there's an error other than "not_folder" or if the follow-up
// metadata request fails.
if err != nil {
return
return err
}
} else {
entries = res.Entries

entries = append(entries, res.Entries...)
for res.HasMore {
arg := files.NewListFolderContinueArg(res.Cursor)

res, err = dbx.ListFolderContinue(arg)
if err != nil {
return err
}

entries = append(entries, res.Entries...)
}
}

w := new(tabwriter.Writer)
Expand All @@ -84,22 +110,17 @@ func ls(cmd *cobra.Command, args []string) (err error) {
}
w.Flush()

return
return err
}

// lsCmd represents the ls command
var lsCmd = &cobra.Command{
Use: "ls [flags] [<path>]",
Short: "List folders",
Long: `List Folders.
Attempting ls on files will fail with 'Error: path/not_folder/.'
Examples:
$ dbxcli ls / # Or, dbxcli ls
$ dbxcli ls some-folder
$ dbxcli ls /some-folder # Or dbxcli ls some-folder/
$ dbxcli ls -l # Or, dbxcli ls --long
`,
Short: "List files and folders",
Example: ` dbxcli ls / # Or just 'ls'
dbxcli ls /some-folder # Or 'ls some-folder'
dbxcli ls /some-folder/some-file.pdf
dbxcli ls -l`,
RunE: ls,
}

Expand Down

0 comments on commit 9919edb

Please sign in to comment.