Skip to content

refactor(cmd): route command output and config write paths onto shared helpers - #1648

Merged
edenreich merged 13 commits into
mainfrom
fix/issue-1643
Oct 30, 2026
Merged

edenreich merged 13 commits into
mainfrom
fix/issue-1643

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

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 shared cmd/output helpers, one config-path resolver, the Cobra flag-relationship declarations, and the dead-code and exit-code fixes the issue lists.

Changes

  • cmd/output gains AddFormatFlag(cmd, def, allowed...) and PrintJSON(w, v). PrintMarkdown now takes an io.Writer.
  • Every --format registration goes through AddFormatFlag: 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 own json-pretty/ag-ui values. tools execute --format json stays compact because the approval bridge reads it.
  • Writer sweep with SetOut capture tests over an isolated HOME for stats, traces and conversations. Also swept: agents, avatars, binaries, config, env, export, mcp, plans, plugins, root, shortcuts, skills, status, tools and version.
  • status --format json|yaml emits a gatewayStatus object (url, status, version, source, models, error) and rejects an unknown format before probing the gateway.
  • One path resolver: runtime.ConfigPath(toProject, name) and runtime.UserConfigPath(name) plus runtime.AddProjectFlag replace the six home-dir copies (agents, mcp, keybindings, plugins, skills, config set). getMCPConfigPath now errors when the home directory is missing, like its siblings. Plugins stay userspace-only. Skills stay project-first with --user.
  • containerruntime.NewSharedRuntime replaces the two byte-identical sharedRuntime helpers.
  • config set --project writes once with a plain if/else. List values split through runtime.ParseDelimitedList, and a test holds that an empty value still writes key: [].
  • agents add/update declare MarkFlagsMutuallyExclusive("tag", "oci"), update declares MarkFlagsOneRequired, and --environment becomes a StringToString flag (comma form or repeated flag).
  • The mcp success trailers share printSavedNote(w, path) with the restart note on all seven. The enable/disable server and global pairs collapse into setMCPServerEnabled/setMCPGlobalEnabled.
  • Dead code removed: Config.GetGatewayURL/GetTimeout, splitListValue, the duplicated shared-runtime helpers, and the unreachable len(args) checks behind MinimumNArgs/ExactArgs (tools execute, export).
  • errcheck ignores fmt.Fprint* under cmd/ only, since command output stays best-effort like the fmt.Print* it replaces. Everywhere else an unchecked write is still flagged.

User-facing changes

  • infer tools validate exits non-zero, with the reason on stderr, for a disallowed command or disabled tools, so infer tools validate "$cmd" && eval "$cmd" works.
  • infer status --format json|yaml prints the status object instead of the text block followed by Output format: json.
  • agents add/update --environment accepts KEY=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.
  • mcp remove and update now print the restart note like the other mcp writes.

Docs: docs/commands-reference.md and docs/agents-configuration.md are updated here. Docs site follow-up: inference-gateway/docs#1038

Remaining 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.

…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.
@edenreich
edenreich marked this pull request as ready for review October 30, 2026 18:54
@edenreich
edenreich requested a review from a team as a code owner October 30, 2026 18:54
…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.
@edenreich
edenreich merged commit ea0609d into main Oct 30, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-1643 branch October 30, 2026 19:41
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.239.0-rc.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

@edenreich edenreich added the rc Shipped in a release candidate label Oct 31, 2026
@edenreich edenreich moved this from Todo to Done in 2026 Oct 31, 2026
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.239.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rc Shipped in a release candidate released Shipped in a release

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant