Skip to content

Commit b292561

Browse files
Merge pull request #355 from dropbox/feature/dry-run-optional-enabled
Add dryRunOptionalEnabled and collapse duplicated flag guards
2 parents 57be9d7 + 8936175 commit b292561

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

cmd/dry_run.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ func dryRunEnabled(cmd *cobra.Command) (bool, error) {
3838
return cmd.Flags().GetBool(dryRunFlagName)
3939
}
4040

41+
// dryRunOptionalEnabled returns the dry-run flag state, or false when the flag
42+
// is not registered. Option parsers shared with callers that do not register
43+
// --dry-run use this instead of dryRunEnabled to avoid a missing-flag error.
44+
func dryRunOptionalEnabled(cmd *cobra.Command) (bool, error) {
45+
if cmd == nil || cmd.Flags().Lookup(dryRunFlagName) == nil {
46+
return false, nil
47+
}
48+
return dryRunEnabled(cmd)
49+
}
50+
4151
// Dry-run JSON results use status "planned" and include dry_run=true in the
4252
// result input. They do not preserve the real mutation status, because no
4353
// mutation happened.

cmd/relocation_if_exists.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ func parseRelocationOptions(cmd *cobra.Command) (relocationOptions, error) {
2323
if err != nil {
2424
return relocationOptions{}, err
2525
}
26-
dryRun := false
2726
// Some tests and callers use relocation parsing without registering --dry-run.
28-
if cmd != nil && cmd.Flags().Lookup(dryRunFlagName) != nil {
29-
dryRun, err = dryRunEnabled(cmd)
30-
if err != nil {
31-
return relocationOptions{}, err
32-
}
27+
dryRun, err := dryRunOptionalEnabled(cmd)
28+
if err != nil {
29+
return relocationOptions{}, err
3330
}
3431
return relocationOptions{ifExists: ifExists, dryRun: dryRun}, nil
3532
}

cmd/share_link_create.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,11 @@ func parseShareLinkCreateOptions(cmd *cobra.Command) (shareLinkCreateOptions, er
213213
return opts, err
214214
}
215215
opts.password = password
216-
if cmd.Flags().Lookup(dryRunFlagName) != nil {
217-
dryRun, err := dryRunEnabled(cmd)
218-
if err != nil {
219-
return opts, err
220-
}
221-
opts.dryRun = dryRun
216+
dryRun, err := dryRunOptionalEnabled(cmd)
217+
if err != nil {
218+
return opts, err
222219
}
220+
opts.dryRun = dryRun
223221

224222
if opts.expires != nil && opts.removeExpiration {
225223
return opts, invalidArgumentsErrorWithDetails("`--expires` and `--remove-expiration` cannot be used together", flagsErrorDetails("expires", "remove-expiration"))

cmd/share_link_revoke.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ func shareLinkRevokeOperationResults(revoked []shareLinkRevokeResult, dryRun boo
9797
func parseShareLinkRevokeOptions(cmd *cobra.Command, args []string) (shareLinkRevokeOptions, error) {
9898
var opts shareLinkRevokeOptions
9999

100-
if cmd.Flags().Lookup(dryRunFlagName) != nil {
101-
dryRun, err := dryRunEnabled(cmd)
102-
if err != nil {
103-
return opts, err
104-
}
105-
opts.dryRun = dryRun
100+
dryRun, err := dryRunOptionalEnabled(cmd)
101+
if err != nil {
102+
return opts, err
106103
}
104+
opts.dryRun = dryRun
107105

108106
if !localFlagChanged(cmd, "path") {
109107
return opts, nil

cmd/share_link_update.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,9 @@ func parseShareLinkUpdateOptions(cmd *cobra.Command) (shareLinkUpdateOptions, er
182182
if err != nil {
183183
return shareLinkUpdateOptions{}, err
184184
}
185-
var dryRun bool
186-
if cmd.Flags().Lookup(dryRunFlagName) != nil {
187-
dryRun, err = dryRunEnabled(cmd)
188-
if err != nil {
189-
return shareLinkUpdateOptions{}, err
190-
}
185+
dryRun, err := dryRunOptionalEnabled(cmd)
186+
if err != nil {
187+
return shareLinkUpdateOptions{}, err
191188
}
192189

193190
if expiresChanged && removeExpiration {

0 commit comments

Comments
 (0)