Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,29 @@ func ls(cmd *cobra.Command, args []string) (err error) {

dbx := files.New(config)

// check if given object exists
var metaRes files.IsMetadata
metaRes, err = getFileMetadata(dbx, path)
if err != nil {
return err
}

if long {
fmt.Fprint(w, "Revision\tSize\tLast modified\tPath\n")
}

// handle case when path to file was given
switch f := metaRes.(type) {
case *files.FileMetadata:
if !onlyDeleted {
printItem(formatFileMetadata(f, long))
err = w.Flush()
// Check whether the path exists and is a file rather than a folder.
// Skip this for the root path ("") since get_metadata returns an error for
// it ("The root folder is unsupported") and the root is always a folder.
if path != "" {
var metaRes files.IsMetadata
metaRes, err = getFileMetadata(dbx, path)
if err != nil {
return err
}

// handle case when path to file was given
switch f := metaRes.(type) {
case *files.FileMetadata:
if !onlyDeleted {
printItem(formatFileMetadata(f, long))
err = w.Flush()
return err
}
}
}

res, err := dbx.ListFolder(arg)
Expand Down
28 changes: 28 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"golang.org/x/oauth2"

"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/common"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/users"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"golang.org/x/net/context"
Expand Down Expand Up @@ -197,6 +199,7 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
if verbose {
logLevel = dropbox.LogInfo
}

config = dropbox.Config{
Token: tokens[tokType],
LogLevel: logLevel,
Expand All @@ -208,6 +211,31 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
URLGenerator: nil,
}

// Auto-detect the root namespace so that team folders are accessible.
// Team manage tokens are for administrative operations and don't need
// a path root; skip auto-detection for those.
if tokType != tokenTeamManage {
usersClient := users.New(config)
account, accountErr := usersClient.GetCurrentAccount()
if accountErr != nil {
config.LogInfo("Warning: could not auto-detect root namespace (%v); team folders may not be accessible", accountErr)
} else {
var rootNamespaceID string
switch ri := account.RootInfo.(type) {
case *common.TeamRootInfo:
rootNamespaceID = ri.RootNamespaceId
case *common.UserRootInfo:
rootNamespaceID = ri.RootNamespaceId
}
if rootNamespaceID != "" {
pathRootHeader := fmt.Sprintf(`{".tag": "root", "root": "%s"}`, rootNamespaceID)
config.HeaderGenerator = func(_, _, _, _ string) map[string]string {
return map[string]string{"Dropbox-API-Path-Root": pathRootHeader}
}
Comment on lines +231 to +234
}
}
Comment on lines +218 to +236
}

return
}

Expand Down
Loading