feat: add stdin/stdout pipelining support for all CLI commands#151
feat: add stdin/stdout pipelining support for all CLI commands#151TristanSpeakEasy merged 5 commits intomainfrom
Conversation
…elining Enable reading OpenAPI specs from stdin using '-' as the file argument across all CLI commands. This allows standard Unix piping patterns like: cat spec.yaml | openapi spec validate - cat spec.yaml | openapi spec inline - | openapi spec clean - curl -s https://api.example.com/spec | openapi spec lint -f json - Changes: - Add IsStdin() helper and ReadFromStdin field to OpenAPIProcessor - Support '-' as stdin indicator in all commands: validate, lint, inline, bundle, upgrade, clean, sanitize, optimize, snip, explore, localize, arazzo validate - Move all status/info/warning messages to stderr so stdout is reserved for data output (document content, lint results) - enables clean piping - Guard incompatible flags with stdin: --write, --fix, --fix-interactive, interactive snip mode - Add comprehensive unit and integration tests for stdin/pipelining - Update all command help text to document stdin usage Closes #67 https://claude.ai/code/session_01E9drkMjTuKTfQsLhTLdZxX
When stdin is connected to a pipe (not a terminal), all commands now automatically read from stdin without requiring the '-' argument. The explicit '-' still works as an override. This means you can now write: cat spec.yaml | openapi spec validate cat spec.yaml | openapi spec inline | openapi spec clean curl -s https://api.example.com/spec | openapi spec lint -f json Instead of requiring: cat spec.yaml | openapi spec validate - Implementation: - Add StdinIsPiped() helper using os.Stdin.Stat() to detect pipe mode - Add stdinOrFileArgs() custom cobra validator that allows 0 args when stdin is piped, with clear error messages otherwise - Add inputFileFromArgs() that returns '-' when no args given - Update all commands (validate, lint, inline, bundle, upgrade, clean, sanitize, optimize, snip, explore, arazzo validate) to use the new helpers - When stdin is not piped and no file given, shows helpful error: "requires at least 1 arg(s), or pipe data to stdin" - Add tests for inputFileFromArgs and stdinOrFileArgs https://claude.ai/code/session_01E9drkMjTuKTfQsLhTLdZxX
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive stdin/stdout support to OpenAPI and Arazzo CLI commands, enabling Unix-style piping workflows. The implementation follows Unix conventions where - indicates stdin/stdout and keeps stdout clean for data output by writing all diagnostic messages to stderr.
Changes:
- Added core stdin detection and handling infrastructure (
IsStdin,StdinIsPiped,inputFileFromArgs,stdinOrFileArgs) - Updated 11 commands (validate, lint, explore, optimize, snip, inline, bundle, clean, sanitize, upgrade for OpenAPI; validate for Arazzo) to accept stdin input
- Redirected all diagnostic/status messages to stderr to preserve stdout for data piping
- Added comprehensive unit and integration tests for stdin functionality
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| cmd/openapi/commands/openapi/shared.go | Core infrastructure for stdin detection, argument parsing, and processor configuration with stdin support |
| cmd/openapi/commands/openapi/shared_test.go | Unit tests for stdin detection, argument parsing, and processor configuration |
| cmd/openapi/commands/openapi/stdin_integration_test.go | Integration tests for stdin-to-stdout pipelines and file output workflows |
| cmd/openapi/commands/openapi/validate.go | Added stdin support and stderr output for validation results |
| cmd/openapi/commands/openapi/lint.go | Added stdin support with validation against --fix flags and stderr diagnostics |
| cmd/openapi/commands/openapi/explore.go | Added stdin support for interactive TUI exploration |
| cmd/openapi/commands/openapi/optimize.go | Added stdin support and stderr output for interactive prompts |
| cmd/openapi/commands/openapi/snip.go | Added stdin support with validation against interactive mode and stderr diagnostics |
| cmd/openapi/commands/openapi/inline.go | Added stdin support with empty TargetLocation for stdin input |
| cmd/openapi/commands/openapi/bundle.go | Added stdin support with empty TargetLocation for stdin input |
| cmd/openapi/commands/openapi/clean.go | Added stdin support and updated argument handling |
| cmd/openapi/commands/openapi/sanitize.go | Added stdin support and updated argument handling |
| cmd/openapi/commands/openapi/upgrade.go | Added stdin support with help text and argument handling |
| cmd/openapi/commands/openapi/localize.go | Updated diagnostic messages to write to stderr (no stdin support as it requires target directory) |
| cmd/openapi/commands/openapi/join.go | Updated diagnostic messages to write to stderr (no stdin support as it requires multiple input files) |
| cmd/openapi/commands/arazzo/validate.go | Added stdin support for Arazzo workflow validation with local implementations of stdin helpers |
| cmd/openapi/commands/overlay/validate.go | Updated validation success message to write to stderr |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Test Coverage ReportCurrent Coverage: Coverage Change: ✅ No change Coverage by Package
📋 Detailed Coverage by Function (click to expand)
Generated by GitHub Actions |
|
Reviewed this branch and found one blocking issue. Blocking:
Evidence:
Suggested fix:
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…deduplicate shared CLI utilities
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 29 out of 29 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
This PR adds comprehensive stdin/stdout support across all CLI command groups (OpenAPI, Arazzo, Swagger, and Overlay), enabling Unix-style piping workflows. Users can now pipe data to stdin and pipe output to stdout, following standard Unix conventions.
Key Changes
Shared CLI Utilities (cmdutil package)
cmd/openapi/commands/cmdutil/cmdutil.gowith shared stdin/output utilitiesStdinIndicator,IsStdin(),StdinIsPiped(),InputFileFromArgs(),StdinOrFileArgs(),Die(),Dief()into a shared packageopenapi,arazzo, andoverlaycommand packages to usecmdutilinstead of duplicating these functionsOpenAPI Commands (existing)
All
openapi specsubcommands already support stdin from prior commits:validate,lint,explore,optimize,snip,inline,bundle,clean,sanitize,upgradeArazzo Commands (updated)
openapi arazzo validate- updated to use sharedcmdutilpackageSwagger Commands (NEW)
openapi swagger validate- now accepts stdin (cat swagger.yaml | openapi swagger validate)openapi swagger upgrade- now accepts stdin with stdout output for pipingSwaggerProcessorwithReadFromStdinfield and stdin-awareLoadDocument()Overlay Commands (NEW)
openapi overlay validate- now accepts overlay from stdinopenapi overlay apply- now accepts spec from stdin (cat spec.yaml | openapi overlay apply overlay.yaml -)openapi overlay compare- now accepts before spec from stdin (cat spec1.yaml | openapi overlay compare - spec2.yaml)Library Additions (overlay package)
overlay.ParseReader(io.Reader)for parsing overlays from readersloader.LoadOverlayFromReader(io.Reader)for loading overlays from readersloader.LoadSpecificationFromReader(io.Reader)for loading specs from readersBug Fixes (PR feedback)
--non-interactivewhen reading from stdin, since interactive mode also reads from stdincmdutilpackage, eliminating code duplication across arazzo, openapi, and overlay packagesOutput Handling
Test plan