Documentation
¶
Overview ¶
Package kongcue provides CUE-based configuration loading with Kong CLI integration. It supports YAML, JSON, and CUE file formats using CUE as the underlying parser, and provides a Kong resolver for seamless CLI flag and config file merging.
Index ¶
- func AllowUnknownFields(paths ...string) kong.Option
- func GenerateSchema(ctx *cue.Context, app *kong.Application, opts *schemaOptions) (cue.Value, error)
- func GenerateSchemaFile(app *kong.Application, opts *schemaOptions) *ast.File
- func LoadAndUnifyPaths(patterns []string) (cue.Value, error)
- func NewResolver(value cue.Value) kong.Resolver
- func Options() kong.Option
- func ValidateConfig(schema cue.Value, config cue.Value) error
- type Config
- type ConfigDoc
- type SchemaOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowUnknownFields ¶ added in v0.1.2
AllowUnknownFields returns a Kong option that allows unknown config keys. With no arguments, unknown fields are allowed everywhere (backwards compatible). With path arguments, unknown fields are only allowed at those paths and their descendants.
Paths use dot notation matching the config structure (e.g., "server", "server.tls").
Usage:
kong.Parse(&cli, kongcue.AllowUnknownFields()) // allow everywhere
kong.Parse(&cli, kongcue.AllowUnknownFields("extra", "legacy")) // allow at specific paths
func GenerateSchema ¶ added in v0.1.2
func GenerateSchema(ctx *cue.Context, app *kong.Application, opts *schemaOptions) (cue.Value, error)
GenerateSchema creates a CUE schema from a Kong application model. The schema uses named definitions (#Root, #CommandName) and closed structs to reject unknown config keys unless allowUnknownFields is set in options. Returns the #Root definition as a cue.Value for validation.
func GenerateSchemaFile ¶ added in v0.1.4
func GenerateSchemaFile(app *kong.Application, opts *schemaOptions) *ast.File
GenerateSchemaFile creates a CUE AST file with named definitions. Each command becomes a separate definition (e.g., #Agent, #Server). The root schema is in #Root and references command definitions.
Example output:
#Root: close({
verbose?: int
agent?: #Agent
})
#Agent: close({
ca_url?: string
})
func LoadAndUnifyPaths ¶
LoadAndUnifyPaths loads multiple config files and unifies them into a single CUE value. Supports glob patterns and mixed file types (.cue, .yaml, .yml, .json). Missing files are silently skipped. Returns error if files have conflicting values.
The ~ character is expanded to the user's home directory.
func NewResolver ¶
NewResolver creates a Kong resolver backed by a CUE value. The resolver automatically maps CLI flag names (kebab-case) to config keys (snake_case).
Config paths are built from the command hierarchy. For example, a flag "--ca-url" on command "agent" will look up "agent.ca_url" in the CUE value. Global flags (not under a subcommand) use just the flag name.
Example:
config, _ := LoadAndUnifyPaths([]string{"~/.myapp/*.yaml"})
ctx := kong.Parse(&cli, kong.Resolvers(NewResolver(config)))
Types ¶
type ConfigDoc ¶ added in v0.1.4
type ConfigDoc struct {
// Output is the writer for schema output. Defaults to os.Stdout.
// Exposed for testing.
Output io.Writer `kong:"-"`
}
ConfigDoc is a Kong command that outputs the CUE schema for the CLI. Embed this in your CLI struct to add a command that prints the config schema.
The generated schema uses named CUE definitions (#Root, #CommandName, etc.) and can be used to validate configuration files.
Usage:
type cli struct {
Agent agentCmd `cmd:""`
ConfigDoc kongcue.ConfigDoc `cmd:"config-doc" help:"Print CUE schema for config file"`
}
Running `./myapp config-doc` outputs the schema to stdout.
func (*ConfigDoc) BeforeApply ¶ added in v0.1.4
func (c *ConfigDoc) BeforeApply(app *kong.Kong, schemaOpts *SchemaOptions) error
BeforeApply is called by Kong before validation. Using BeforeApply (instead of AfterApply) allows this command to run without requiring other flags to be set, similar to --help.
type SchemaOptions ¶ added in v0.1.4
SchemaOptions holds configuration for schema generation. Exported so it can be received via Kong's dependency injection in hooks.