feat(fn): add --help, --doc, and standalone file mode to AsMain#752
Open
efiacor wants to merge 7 commits intokptdev:mainfrom
Open
feat(fn): add --help, --doc, and standalone file mode to AsMain#752efiacor wants to merge 7 commits intokptdev:mainfrom
efiacor wants to merge 7 commits intokptdev:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the Go SDK’s fn.AsMain entrypoint to support self-contained CLI documentation output (--help, --doc) and a “standalone file mode” for running functions against local YAML files, backed by a new internal docs package for parsing embedded README/metadata content.
Changes:
- Added functional options to
fn.AsMain(viaWithDocs) and implemented--help/--dochandling before STDIN is read. - Added standalone file mode that assembles a
ResourceListfrom positional file arguments and runs the processor without STDIN piping. - Introduced
go/fn/internal/docs(marker parsing, metadata parsing, help/doc rendering) plus unit and property-based tests (rapid).
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| go/fn/run.go | Adds AsMain options, --help/--doc handling, and standalone file mode + helpers. |
| go/fn/run_flags_test.go | Adds tests for --help/--doc behaviors and error handling. |
| go/fn/run_filemode_test.go | Adds unit tests for standalone file mode and helper behavior. |
| go/fn/run_filemode_property_test.go | Adds rapid property test for file-mode vs STDIN-mode equivalence. |
| go/fn/internal/docs/render.go | Implements help/doc renderers and the DocOutput JSON schema. |
| go/fn/internal/docs/render_test.go | Adds unit + property tests for rendering behaviors. |
| go/fn/internal/docs/metadata.go | Adds metadata.yaml parsing into a typed struct. |
| go/fn/internal/docs/metadata_test.go | Adds unit + property tests for metadata parsing and round-trips. |
| go/fn/internal/docs/markers.go | Adds README mdtogo marker extraction with a fallback mode. |
| go/fn/internal/docs/markers_test.go | Adds property tests for marker parsing and missing-marker fallback. |
| go/fn/go.mod | Adds pgregory.net/rapid dependency (currently marked indirect). |
| go/fn/go.sum | Adds checksum entries for pgregory.net/rapid. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cee0802 to
4d4200f
Compare
Extend fn.AsMain with functional options to support: - --help: renders human-readable docs from embedded README markers - --doc: outputs machine-readable JSON for catalog tooling - Standalone file mode: accepts positional file args for local debugging New fn.WithDocs(readme, meta) option registers embedded content. Existing callers with no options are unaffected (backward-compatible). Adds internal/docs package with: - ParseMarkers: extracts mdtogo Short/Long/Examples sections - ParseMetadata: parses metadata.yaml content - RenderHelp/RenderDoc: formats output for --help and --doc Includes property-based tests (rapid) and unit tests for all new code. Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Mechanical changes from 'go fix ./...':
- interface{} → any (Go 1.18+ type alias)
- reflect.Ptr → reflect.Pointer (deprecated constant)
- strings.Index+slice → strings.Cut (Go 1.18+ idiom)
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
- --help now always takes precedence over --doc regardless of arg order - Updated AsMain doc comment to document all invocation modes - Removed no-op strings.TrimPrefix in test assertion Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
- Replace manual loops with slices.Contains for --help/--doc checks (matches what 'go fix' produces) - Change test file permissions from 0644 to 0600 (gosec G306) Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
ff8ab7b to
c715091
Compare
…tches catalog READMEs) The catalog functions use <- All existing tests continue tomdtogo--> (bare) as the section end marker, not <- All existing tests continue tomdtogo:End-->. Updated the parser and all tests to match the real-world README format. Signed-off-by: Fiachra Corcoran <fiachra.corcoran@est.tech>
This was referenced May 7, 2026
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.
Summary
Extends
fn.AsMainwith three new capabilities, eliminating the need for cobra, mdtogo, andcommand.Build()in KRM function implementations.Changes
--helpflag: Renders human-readable documentation from embedded README mdtogo markers. No cobra boilerplate. Compatible withkpt fn doc.--docflag: Outputs machine-readable JSON (theDocOutputschema) for catalog pipelines.fn.WithDocs(readme, meta): New functional option to register embedded README and metadata.yaml content.All changes are backward-compatible — existing
fn.AsMain(proc)calls work unchanged.New internal package
go/fn/internal/docs/provides:ParseMarkers: extracts Short/Long/Examples from<!--mdtogo:Short-->...<!--mdtogo-->markersParseMetadata: parses metadata.yamlRenderHelp/RenderDoc: formats output for the two flagsTesting
What this enables (downstream)
Once merged, the 21 catalog functions can:
command.Build()generated/docs.go(mdtogo output)//go:embed+fn.WithDocsfor documentationTested against the real
set-labelscatalog function —--helpand--docproduce correct output.Related Issues
kpt fn docfor all catalog fns kpt#4485 — SDK functions now support--helpnativelyfn.AsMainno longer reads stdin when--helpis presentfn docoutput and documentation kpt#2009 — help output no longer includes cobra boilerplateAI Disclosure
I have used AI in the creation of this PR.
I have used the following AI tools: