From 4a915b9e0f0b95bdc68d7876b10f31b95b0365eb Mon Sep 17 00:00:00 2001 From: Andrey Markelov Date: Thu, 9 Jul 2026 18:02:11 -0700 Subject: [PATCH] Add renderOperation helper and migrate mkdir/restore dry-run Introduce renderOperation, a shared helper that renders a command's operation output (text closure plus the standard JSON operation envelope) so dry-run branches no longer duplicate the commandOutput/newJSONCommandOperationOutput glue. Migrate the two simplest dry-run commands, mkdir and restore, onto it. Output is unchanged; the existing byte-exact dry-run snapshots and the cross-command contract tests both continue to pass. --- cmd/dry_run.go | 8 ++++++++ cmd/mkdir.go | 4 ++-- cmd/restore.go | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/dry_run.go b/cmd/dry_run.go index f1d4f3d..c83d90a 100644 --- a/cmd/dry_run.go +++ b/cmd/dry_run.go @@ -48,6 +48,14 @@ func plannedStatus(dryRun bool, realStatus string) string { return realStatus } +// renderOperation renders a command's operation output through the shared +// command output writer: the text closure produces the human-readable form and +// the JSON form is the standard operation envelope. Dry-run branches use this +// to avoid duplicating the commandOutput/newJSONCommandOperationOutput glue. +func renderOperation(cmd *cobra.Command, input any, results []jsonOperationResult, warnings []jsonWarning, text func(io.Writer) error) error { + return commandOutput(cmd).Render(text, newJSONCommandOperationOutput(cmd, input, results, warnings)) +} + func writeDryRunLine(w io.Writer, verb, path string) error { _, err := fmt.Fprintf(w, "Would %s %s\n", verb, path) return err diff --git a/cmd/mkdir.go b/cmd/mkdir.go index e7e859c..a1c5815 100644 --- a/cmd/mkdir.go +++ b/cmd/mkdir.go @@ -65,9 +65,9 @@ func mkdir(cmd *cobra.Command, args []string) (err error) { if opts.dryRun { result := newPlannedMkdirResult(dst, opts) - return commandOutput(cmd).Render(func(w io.Writer) error { + return renderOperation(cmd, result.Input, []jsonOperationResult{mkdirOperationResult(result)}, nil, func(w io.Writer) error { return writeDryRunLine(w, "create directory", result.displayPath()) - }, newJSONCommandOperationOutput(cmd, result.Input, []jsonOperationResult{mkdirOperationResult(result)}, nil)) + }) } arg := files.NewCreateFolderArg(dst) diff --git a/cmd/restore.go b/cmd/restore.go index 389b806..8f92965 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -63,9 +63,9 @@ func restore(cmd *cobra.Command, args []string) (err error) { } if opts.dryRun { result := newPlannedRestoreResult(path, rev, opts) - return commandOutput(cmd).Render(func(w io.Writer) error { + return renderOperation(cmd, result.Input, []jsonOperationResult{restoreOperationResult(result)}, nil, func(w io.Writer) error { return renderPlannedRestoreResult(w, result) - }, newJSONCommandOperationOutput(cmd, result.Input, []jsonOperationResult{restoreOperationResult(result)}, nil)) + }) } arg := files.NewRestoreArg(path, rev)