refactor(output): share the JSON, created-resource and batch tails

Mutations end with a.jsonResult or a.created instead of hand-rolled
--json branches, the two identical jsonResult/writeMutationJSON helpers
become one, and writeBatchJSON checks --json itself so batch commands
share one tail.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-12 10:43:46 +02:00
1 parent 27e39ec4be
commit 6b204a34c0
32 files changed
+87 -217

No files matched your search

+5 -16
View File
@@ -278,7 +278,7 @@ $ tea repo clone alice/project -- --branch develop --recurse-submodules`,
if _, err := a.cloneRepository(cmd, t, item, opts); err != nil {
return err
}
return a.writeMutationJSON(cmd, item)
return a.jsonResult(cmd, item)
},
}
command.Flags().IntVarP(&opts.depth, "depth", "d", 0, "Number of commits to fetch (0: full history)")
@@ -352,10 +352,7 @@ $ tea repo fork alice/project --clone -- --depth 1`,
return err
}
}
if a.options.json != "" {
return a.writeJSON(cmd, fork)
}
return a.printURL(cmd, fork.URL)
return a.created(cmd, fork, fork.URL)
},
}
command.Flags().StringVar(&opts.Organization, "org", "", "Fork into this organization")
@@ -552,7 +549,7 @@ $ tea repo archive alice/old-project --yes`,
} else {
a.warn(cmd, "%s is not archived", item.FullName)
}
return a.writeMutationJSON(cmd, item)
return a.jsonResult(cmd, item)
}
if archive {
if err := a.confirmAction(cmd, terminal.Confirmation{Prompt: "Archive " + t.Repo + inferred + "? It becomes read-only.", NonTTY: "archive " + t.Repo}); err != nil {
@@ -568,20 +565,12 @@ $ tea repo archive alice/old-project --yes`,
} else {
a.done(cmd, "Unarchived repository %s", item.FullName)
}
return a.writeMutationJSON(cmd, item)
return a.jsonResult(cmd, item)
}
command.ValidArgsFunction = a.completeRepos()
return command
}
// writeMutationJSON writes the result of a mutation with --json; people get done lines instead.
func (a *application) writeMutationJSON(cmd *cobra.Command, item any) error {
if a.options.json == "" {
return nil
}
return a.writeJSON(cmd, item)
}
func (a *application) repoRenameCommand() *cobra.Command {
command := &cobra.Command{
Use: "rename <new-name>",
@@ -624,7 +613,7 @@ $ tea repo rename new-name -R alice/old-name`,
if remote, ok := strings.CutPrefix(t.RepoSource, "remote "); ok {
a.warn(cmd, "Git remote %s still points to %s; update it with git remote set-url", remote, t.Repo)
}
return a.writeMutationJSON(cmd, item)
return a.jsonResult(cmd, item)
},
}
return command