Repository navigation
refactor(cmd): route command output and config write paths onto shared helpers - #1648
Merged
Merged
Conversation
…a writer AddFormatFlag collapses the per-command --format flag registrations and PrintJSON collapses the MarshalIndent-then-print pattern. Both take the shared output seam the Cobra writers provide. Command output writes stay best-effort like the fmt.Print* calls they replace, so errcheck now excludes the fmt.Fprint* variants.
…he Cobra writers Stage one of the issue-1643 sweep: every print site in the read-only commands goes through fmt.Fprint*(cmd.OutOrStdout(), ...) so a test can capture it with SetOut. Each command gains SetOut-based capture tests over an isolated HOME, --format lands through output.AddFormatFlag and the JSON emission through output.PrintJSON. conversations show no longer writes os.Stdout directly and plans show/list take their writer from the command.
…red AddProjectFlag WritePath(toProject, fileName) replaces the six home-dir-fallback copies: the agents and mcp write-path lookups will call it, KeybindingsConfigWritePath and PluginsConfigPath are gone (plugins keeps its userspace-only rule, the silent mcp fallback becomes an error like its siblings), skills resolve through it keeping their project-first rule, and ConfigWriteTarget delegates to it. SharedContainerRuntime takes over the byte-identical sharedRuntime helpers, and ParseDelimitedList is exported so config set can fold its own list splitter away.
The flag existed but only echoed 'Output format: json' after the text block. A gatewayStatus projection now backs all three formats, every print goes through cmd.OutOrStdout(), and the 'Checking...' preamble stays on the text format only so machine output parses.
--project used to write the full default-seeded config and then re-write it sparse, so a failed second write left the project file carrying every default and shadowing the userspace baseline. Pick the writer up front and save once. List values now go through runtime.ParseDelimitedList, whose nil slice marshals like the old empty one, so config set with an empty value keeps writing a bare empty sequence; a pinned test holds that equivalence.
Both had zero callers; the Gateway struct is typed and read directly where it is needed.
…flag relationships to Cobra --tag and --oci are mutually exclusive through Cobra flag groups instead of the hand-rolled check in resolveTagFlag, and update requires one of its flags through MarkFlagsOneRequired. --environment becomes a StringToString flag and the defaults merge collapses to maps.Clone plus maps.Copy. agentsConfigPath resolves through runtime.WritePath, the container runtime through the shared runtime.SharedContainerRuntime, list/show/status JSON goes through output.AddFormatFlag and output.PrintJSON, and every print site captures through cmd.OutOrStdout.
…airs, route output through the Cobra writers The seven success trailers share a saved(path) helper that consistently carries the restart note (remove and update previously lacked it), and the enable/disable server and global pairs collapse into one function each taking a bool. getMCPConfigPath resolves through runtime.WritePath and a missing home directory is now an error instead of a silent fallback to the project path. The shared container runtime comes from runtime.SharedContainerRuntime, status JSON goes through output.PrintJSON with the flag from output.AddFormatFlag, and every print site captures through cmd.OutOrStdout.
ValidateTool now returns an error for a disallowed command and when tools are disabled instead of reporting success, so infer tools validate can gate a shell conditional like validate a command and eval it. The friendly diagnostics still print to the Cobra writer. The fix calls out the exit-code change for the generated changelog. tools execute also routes its JSON and rendered output through the Cobra writer (the approval bridge keeps its compact encoder), gains its --format flag through output.AddFormatFlag, and drops the unreachable empty-args check MinimumNArgs already guards. export takes its writer from the command and validates exactly one argument through cobra.ExactArgs instead of a range plus a manual length check.
…nd the writer sweep Every StringP format registration is now output.AddFormatFlag (shortcuts, avatars, plugins, skills list and search) and their JSON emission goes through output.PrintJSON. The writer sweep reaches shortcuts list, avatars create/list/delete, plugins list, skills list/search/install/uninstall, config init and the root welcome line, all capturing through cmd.OutOrStdout. The plugins/skills plain-text trailers of write commands stay on this branch's follow-up list.
…ra writers EnsureEnvInGitignore takes the writer it prints to since its only caller holds the command.
…t through the Cobra writers The install summary, post-install notes, remove/update/enable/disable success lines and the empty-registry hint all capture through cmd.OutOrStdout, with the writer threaded through the helper chain.
…rom review The fmt.Fprint* errcheck exclusion applied repo-wide, so an unchecked write to a file in internal/ passed lint. It now covers cmd/ only. AddFormatFlag lists the default format in its help text again, and config get registers its flag through it. plugins list and the avatars progress line print through the Cobra writer. tools validate returns the tool service error unchanged, so a rejection prints once on stderr. status probes the gateway once, renders text from the same struct as json and yaml, and rejects an unknown format before probing. WritePath becomes ConfigPath plus UserConfigPath, and the shared container runtime factory moves to containerruntime.NewSharedRuntime. Helpers take a writer and an explicit format instead of the command. Adds tests for tools validate, the status formats, the config path resolver, the format flag help, PrintJSON, the environment flag forms and the empty list write of config set --project.
3 tasks
Contributor
|
🎉 This PR is included in version 0.239.0-rc.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Contributor
|
🎉 This PR is included in version 0.239.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #1643
Summary
Routes the cmd/ packages' raw
fmt.Print*output onto the Cobra writers (fmt.Fprint*(cmd.OutOrStdout(), ...)), staged per package starting with the read-only commands. It also lands the sharedcmd/outputhelpers, one config-path resolver, the Cobra flag-relationship declarations, and the dead-code and exit-code fixes the issue lists.Changes
cmd/outputgainsAddFormatFlag(cmd, def, allowed...)andPrintJSON(w, v).PrintMarkdownnow takes anio.Writer.--formatregistration goes throughAddFormatFlag: agents list/show/status, mcp status, tools execute, stats, traces, conversations list/show, plans list, config get, shortcuts, avatars, plugins, skills list/search and status. Headless keeps its ownjson-pretty/ag-uivalues.tools execute --format jsonstays compact because the approval bridge reads it.status --format json|yamlemits agatewayStatusobject (url,status,version,source,models,error) and rejects an unknown format before probing the gateway.runtime.ConfigPath(toProject, name)andruntime.UserConfigPath(name)plusruntime.AddProjectFlagreplace the six home-dir copies (agents, mcp, keybindings, plugins, skills,config set).getMCPConfigPathnow errors when the home directory is missing, like its siblings. Plugins stay userspace-only. Skills stay project-first with--user.containerruntime.NewSharedRuntimereplaces the two byte-identicalsharedRuntimehelpers.config set --projectwrites once with a plain if/else. List values split throughruntime.ParseDelimitedList, and a test holds that an empty value still writeskey: [].MarkFlagsMutuallyExclusive("tag", "oci"), update declaresMarkFlagsOneRequired, and--environmentbecomes aStringToStringflag (comma form or repeated flag).printSavedNote(w, path)with the restart note on all seven. The enable/disable server and global pairs collapse intosetMCPServerEnabled/setMCPGlobalEnabled.Config.GetGatewayURL/GetTimeout,splitListValue, the duplicated shared-runtime helpers, and the unreachablelen(args)checks behindMinimumNArgs/ExactArgs(tools execute, export).fmt.Fprint*undercmd/only, since command output stays best-effort like thefmt.Print*it replaces. Everywhere else an unchecked write is still flagged.User-facing changes
infer tools validateexits non-zero, with the reason on stderr, for a disallowed command or disabled tools, soinfer tools validate "$cmd" && eval "$cmd"works.infer status --format json|yamlprints the status object instead of the text block followed byOutput format: json.agents add/update --environmentacceptsKEY=VALUE,KEY=VALUE. The repeated form still works.--environment ""is now rejected by the flag parser, where it used to clear the set on update.Docs:
docs/commands-reference.mdanddocs/agents-configuration.mdare updated here. Docs site follow-up: inference-gateway/docs#1038Remaining work for #1643
147 raw
fmt.Print*sites remain, staged for follow-ups: init (43), keybindings (33), migrate (19), gpu (18), conversationtitle (18) and chat (16). Headless keeps its own format values.