Skip to content

Commit 9ad7667

Browse files
Merge pull request #260 from dropbox/share-link-revoke-path
Add --path flag to share-link revoke
2 parents b50588e + a09d734 commit 9ad7667

3 files changed

Lines changed: 363 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ $ dbxcli share-link info <url> --path /nested/file.txt # display information for
243243
$ dbxcli share-link list # list existing shared links
244244
$ dbxcli share-link list /file.txt # list direct shared links for a path
245245
$ dbxcli share-link revoke <url> # revoke a shared link
246+
$ dbxcli share-link revoke --path /file.txt # revoke direct shared links for a path
246247
$ dbxcli share-link update <url> --allow-download # update shared link settings
247248
$ dbxcli share-link update <url> --disallow-download # disable downloads from a shared link
248249
$ dbxcli share-link update <url> --audience public # update shared link audience
@@ -258,6 +259,8 @@ $ dbxcli share list folder # list shared folders
258259

259260
`share-link create --audience` and `share-link update --audience` support `public`, `team`, `members`, and `no-one`. Dropbox team and folder policies can still resolve the effective audience differently.
260261

262+
Dropbox account, team, and folder policies can reject shared-link settings such as passwords, expiration, audience, or disabled downloads. In that case, dbxcli returns the Dropbox API error, for example `settings_error/not_authorized/`.
263+
261264
`share-link create`, `share-link update`, `share-link info`, and `share-link download` support `--password <value>`, `--password-prompt`, and `--password-file <path>` for password-protected links. Use `--password-prompt` for interactive use so the password is not echoed.
262265

263266
`share-link download` writes to the metadata filename when `target` is omitted. Use `-` as the target to write file bytes to stdout. Folder shared links require `--recursive` and cannot be written to stdout.

cmd/share_link_revoke.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ package cmd
1616

1717
import (
1818
"errors"
19+
"fmt"
1920

2021
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/sharing"
2122
"github.com/spf13/cobra"
2223
)
2324

25+
type shareLinkRevokeOptions struct {
26+
path string
27+
}
28+
2429
func shareLinkRevoke(cmd *cobra.Command, args []string) error {
30+
opts, err := parseShareLinkRevokeOptions(cmd, args)
31+
if err != nil {
32+
return err
33+
}
34+
35+
if opts.path != "" {
36+
return revokeSharedLinksForPath(cmd, opts.path)
37+
}
38+
2539
if len(args) != 1 {
2640
return errors.New("`share-link revoke` requires a `url` argument")
2741
}
@@ -41,12 +55,74 @@ func shareLinkRevoke(cmd *cobra.Command, args []string) error {
4155
return nil
4256
}
4357

58+
func parseShareLinkRevokeOptions(cmd *cobra.Command, args []string) (shareLinkRevokeOptions, error) {
59+
var opts shareLinkRevokeOptions
60+
61+
if !localFlagChanged(cmd, "path") {
62+
return opts, nil
63+
}
64+
if len(args) != 0 {
65+
return opts, errors.New("`--path` cannot be used with a shared link URL")
66+
}
67+
68+
pathArg, err := localStringFlag(cmd, "path")
69+
if err != nil {
70+
return opts, err
71+
}
72+
if pathArg == "" {
73+
return opts, errors.New("`--path` requires a non-empty path")
74+
}
75+
76+
path, err := validatePath(pathArg)
77+
if err != nil {
78+
return opts, err
79+
}
80+
if path == "" {
81+
return opts, errors.New("cannot revoke shared links for Dropbox root")
82+
}
83+
84+
opts.path = path
85+
return opts, nil
86+
}
87+
88+
func revokeSharedLinksForPath(cmd *cobra.Command, path string) error {
89+
arg := sharing.NewListSharedLinksArg()
90+
arg.Path = path
91+
arg.DirectOnly = true
92+
93+
dbx := newSharedLinkClient(config)
94+
links, err := listSharedLinks(dbx, arg)
95+
if err != nil {
96+
return err
97+
}
98+
if len(links) == 0 {
99+
return fmt.Errorf("no direct shared links found for %q", path)
100+
}
101+
102+
for _, link := range links {
103+
url, ok := sharedLinkURL(link)
104+
if !ok {
105+
return errors.New("shared link response did not include a URL")
106+
}
107+
if err := dbx.RevokeSharedLink(sharing.NewRevokeSharedLinkArg(url)); err != nil {
108+
return fmt.Errorf("revoke shared link %s: %w", url, err)
109+
}
110+
}
111+
112+
commandVerboseStatus(cmd, "Revoked %d shared links for %s", len(links), path)
113+
return nil
114+
}
115+
44116
var shareLinkRevokeCmd = &cobra.Command{
45-
Use: "revoke <url>",
46-
Short: "Revoke a shared link",
47-
RunE: shareLinkRevoke,
117+
Use: "revoke [url]",
118+
Short: "Revoke shared links",
119+
Long: "Revoke a shared link by URL, or revoke all direct shared links for a Dropbox path with --path.",
120+
Example: ` dbxcli share-link revoke https://www.dropbox.com/s/example/file.txt
121+
dbxcli share-link revoke --path /file.txt`,
122+
RunE: shareLinkRevoke,
48123
}
49124

50125
func init() {
126+
shareLinkRevokeCmd.Flags().String("path", "", "Revoke direct shared links for a Dropbox path")
51127
shareLinkCmd.AddCommand(shareLinkRevokeCmd)
52128
}

0 commit comments

Comments
 (0)