diff --git a/cmd/ls.go b/cmd/ls.go index 9ee7750..1aafc68 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -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) diff --git a/cmd/root.go b/cmd/root.go index 18da322..05408f0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" @@ -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, @@ -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} + } + } + } + } + return }