Skip to content

feat(fn): add --help, --doc, and standalone file mode to AsMain#752

Open
efiacor wants to merge 7 commits intokptdev:mainfrom
Nordix:feat/asmain-help-doc-filemode
Open

feat(fn): add --help, --doc, and standalone file mode to AsMain#752
efiacor wants to merge 7 commits intokptdev:mainfrom
Nordix:feat/asmain-help-doc-filemode

Conversation

@efiacor
Copy link
Copy Markdown
Contributor

@efiacor efiacor commented May 6, 2026

Summary

Extends fn.AsMain with three new capabilities, eliminating the need for cobra, mdtogo, and command.Build() in KRM function implementations.

Changes

  • --help flag: Renders human-readable documentation from embedded README mdtogo markers. No cobra boilerplate. Compatible with kpt fn doc.
  • --doc flag: Outputs machine-readable JSON (the DocOutput schema) for catalog pipelines.
  • Standalone file mode: Accepts positional file arguments for local debugging without piping through STDIN.
  • 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--> markers
  • ParseMetadata: parses metadata.yaml
  • RenderHelp / RenderDoc: formats output for the two flags

Testing

  • 7 property-based tests (rapid) covering round-trip, containment, exclusion, and equivalence properties
  • Unit tests for all new code paths (flags, file mode, error handling)
  • All existing tests continue to pass

What this enables (downstream)

Once merged, the 21 catalog functions can:

  1. Drop cobra and command.Build()
  2. Delete generated/docs.go (mdtogo output)
  3. Use //go:embed + fn.WithDocs for documentation

Tested against the real set-labels catalog function — --help and --doc produce correct output.

Related Issues

AI Disclosure

I have used AI in the creation of this PR.

I have used the following AI tools:

  • Kiro for implementation, tests, and review iteration

Copilot AI review requested due to automatic review settings May 6, 2026 23:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (via WithDocs) and implemented --help/--doc handling before STDIN is read.
  • Added standalone file mode that assembles a ResourceList from 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.

Comment thread go/fn/internal/docs/render.go
Comment thread go/fn/run.go
Comment thread go/fn/run_filemode_test.go
Comment thread go/fn/run.go Outdated
Comment thread go/fn/run.go
@efiacor efiacor closed this May 7, 2026
@efiacor efiacor reopened this May 7, 2026
@efiacor efiacor self-assigned this May 7, 2026
@efiacor efiacor added dependencies Pull requests that update a dependency file go Pull requests that update Go code enhancement New feature or request labels May 7, 2026
@efiacor efiacor marked this pull request as draft May 7, 2026 09:20
@efiacor efiacor force-pushed the feat/asmain-help-doc-filemode branch from cee0802 to 4d4200f Compare May 7, 2026 12:38
@efiacor efiacor marked this pull request as ready for review May 7, 2026 12:39
efiacor added 6 commits May 7, 2026 13:59
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>
@efiacor efiacor force-pushed the feat/asmain-help-doc-filemode branch from ff8ab7b to c715091 Compare May 7, 2026 13:00
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file enhancement New feature or request go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request for Approval: Fix kpt fn doc stdin handling for KRM functions fn doc output and documentation

2 participants