From 893617534a7e167daade3ba2f8043ae8acb31354 Mon Sep 17 00:00:00 2001 From: Andrey Markelov Date: Fri, 10 Jul 2026 12:33:14 -0700 Subject: [PATCH] Add dryRunOptionalEnabled and collapse duplicated flag guards Several option parsers are shared with callers that do not register the --dry-run flag, so each guarded the lookup with if cmd.Flags().Lookup(dryRunFlagName) != nil { ... } before reading it. Extract that pattern into dryRunOptionalEnabled, which returns false when the flag is absent, and use it in share-link create/update/revoke and the relocation (cp/mv) parser. Behavior is unchanged: verified byte-for-byte against the pre-refactor binary for share-link create and cp dry-run output, and the relocation no-flag parser paths still pass. --- cmd/dry_run.go | 10 ++++++++++ cmd/relocation_if_exists.go | 9 +++------ cmd/share_link_create.go | 10 ++++------ cmd/share_link_revoke.go | 10 ++++------ cmd/share_link_update.go | 9 +++------ 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cmd/dry_run.go b/cmd/dry_run.go index c83d90a..a0d6a80 100644 --- a/cmd/dry_run.go +++ b/cmd/dry_run.go @@ -38,6 +38,16 @@ func dryRunEnabled(cmd *cobra.Command) (bool, error) { return cmd.Flags().GetBool(dryRunFlagName) } +// dryRunOptionalEnabled returns the dry-run flag state, or false when the flag +// is not registered. Option parsers shared with callers that do not register +// --dry-run use this instead of dryRunEnabled to avoid a missing-flag error. +func dryRunOptionalEnabled(cmd *cobra.Command) (bool, error) { + if cmd == nil || cmd.Flags().Lookup(dryRunFlagName) == nil { + return false, nil + } + return dryRunEnabled(cmd) +} + // Dry-run JSON results use status "planned" and include dry_run=true in the // result input. They do not preserve the real mutation status, because no // mutation happened. diff --git a/cmd/relocation_if_exists.go b/cmd/relocation_if_exists.go index 9830b7c..d6d3faa 100644 --- a/cmd/relocation_if_exists.go +++ b/cmd/relocation_if_exists.go @@ -23,13 +23,10 @@ func parseRelocationOptions(cmd *cobra.Command) (relocationOptions, error) { if err != nil { return relocationOptions{}, err } - dryRun := false // Some tests and callers use relocation parsing without registering --dry-run. - if cmd != nil && cmd.Flags().Lookup(dryRunFlagName) != nil { - dryRun, err = dryRunEnabled(cmd) - if err != nil { - return relocationOptions{}, err - } + dryRun, err := dryRunOptionalEnabled(cmd) + if err != nil { + return relocationOptions{}, err } return relocationOptions{ifExists: ifExists, dryRun: dryRun}, nil } diff --git a/cmd/share_link_create.go b/cmd/share_link_create.go index bb22e2b..eece794 100644 --- a/cmd/share_link_create.go +++ b/cmd/share_link_create.go @@ -213,13 +213,11 @@ func parseShareLinkCreateOptions(cmd *cobra.Command) (shareLinkCreateOptions, er return opts, err } opts.password = password - if cmd.Flags().Lookup(dryRunFlagName) != nil { - dryRun, err := dryRunEnabled(cmd) - if err != nil { - return opts, err - } - opts.dryRun = dryRun + dryRun, err := dryRunOptionalEnabled(cmd) + if err != nil { + return opts, err } + opts.dryRun = dryRun if opts.expires != nil && opts.removeExpiration { return opts, invalidArgumentsErrorWithDetails("`--expires` and `--remove-expiration` cannot be used together", flagsErrorDetails("expires", "remove-expiration")) diff --git a/cmd/share_link_revoke.go b/cmd/share_link_revoke.go index cac30ee..58a1970 100644 --- a/cmd/share_link_revoke.go +++ b/cmd/share_link_revoke.go @@ -97,13 +97,11 @@ func shareLinkRevokeOperationResults(revoked []shareLinkRevokeResult, dryRun boo func parseShareLinkRevokeOptions(cmd *cobra.Command, args []string) (shareLinkRevokeOptions, error) { var opts shareLinkRevokeOptions - if cmd.Flags().Lookup(dryRunFlagName) != nil { - dryRun, err := dryRunEnabled(cmd) - if err != nil { - return opts, err - } - opts.dryRun = dryRun + dryRun, err := dryRunOptionalEnabled(cmd) + if err != nil { + return opts, err } + opts.dryRun = dryRun if !localFlagChanged(cmd, "path") { return opts, nil diff --git a/cmd/share_link_update.go b/cmd/share_link_update.go index e6e58cf..0e13752 100644 --- a/cmd/share_link_update.go +++ b/cmd/share_link_update.go @@ -182,12 +182,9 @@ func parseShareLinkUpdateOptions(cmd *cobra.Command) (shareLinkUpdateOptions, er if err != nil { return shareLinkUpdateOptions{}, err } - var dryRun bool - if cmd.Flags().Lookup(dryRunFlagName) != nil { - dryRun, err = dryRunEnabled(cmd) - if err != nil { - return shareLinkUpdateOptions{}, err - } + dryRun, err := dryRunOptionalEnabled(cmd) + if err != nil { + return shareLinkUpdateOptions{}, err } if expiresChanged && removeExpiration {