Skip to content

Refactor: extract shared mv/cp relocation driver#343

Merged
AndreyVMarkelov merged 1 commit into
masterfrom
refactor/relocation-driver
Jul 8, 2026
Merged

Refactor: extract shared mv/cp relocation driver#343
AndreyVMarkelov merged 1 commit into
masterfrom
refactor/relocation-driver

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

mv and cp had ~115 lines of near-identical body — the same two-phase validate/execute loop, destination resolution, skip and conflict handling, dry-run preview, and JSON accumulation — differing only in a handful of values (command name, verb, success status, missing-args message, and which API call to make).

This introduces a relocationSpec describing those differences and a single runRelocation driver in cmd/relocation.go. mv and cp become one-line delegations:

func mv(cmd *cobra.Command, args []string) error { return runRelocation(cmd, args, moveSpec) }
func cp(cmd *cobra.Command, args []string) error { return runRelocation(cmd, args, copySpec) }

No behavior change. All command-specific strings are preserved via the spec, including mv's distinct "mv command requires…" vs cp's "cp requires…" missing-args wording.

Net: 4 files, +367/−229 (cp.go −116, mv.go −115, relocation.go +168, relocation_test.go +197).

Tests

New cmd/relocation_test.go exercises runRelocation over both specs (table-driven mv/cp):

  • distinct missing-args message per command
  • dry-run source-lookup, destination-check, and result-build error branches — each asserting the correct verb and (for dry-run) that no API is called
  • Move-vs-Copy API routing with the correct success status

Raises runRelocation coverage from 85% → 97%. The only uncovered block is a pre-existing unreachable validation branch (makeRelocationArgvalidatePath never errors), carried over verbatim; a candidate for separate cleanup.

mv and cp had ~115 lines of near-identical body: the same two-phase
validate/execute loop, destination resolution, skip and conflict handling,
dry-run preview, and JSON accumulation, differing only in a handful of
values (command name, verb, success status, missing-args message, and which
API call to make).

Introduce a relocationSpec describing those differences and a single
runRelocation driver in relocation.go. mv and cp become one-line delegations
to runRelocation with moveSpec / copySpec. No behavior change: all
command-specific strings (including mv's distinct missing-args wording) are
preserved via the spec.

Add relocation_test.go covering runRelocation over both specs: the distinct
missing-args messages, the dry-run/skip/result-build error branches (each
asserting the correct verb), and Move-vs-Copy API routing with the correct
status. Raises runRelocation coverage from 85% to 97%; the only uncovered
block is a pre-existing unreachable validation branch.
@AndreyVMarkelov AndreyVMarkelov merged commit ac226fc into master Jul 8, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant