Documentation
¶
Index ¶
- Constants
- func AssertSync(t *testing.T, app *App, cfg any)
- type App
- func (a *App) AddConfigPath(p string) *App
- func (a *App) Configure() error
- func (a *App) On(cmd *cobra.Command, opts ...*CommandOptions) *App
- func (a *App) OnGroup(cmd *cobra.Command, name string, opts ...*CommandOptions) *App
- func (a *App) Reload() error
- func (a *App) Root(opts ...*CommandOptions) *App
- func (a *App) RootGroup(name string, opts ...*CommandOptions) *App
- func (a *App) Run() error
- func (a *App) SetConfigName(name string) *App
- func (a *App) SetConfigType(t string) *App
- func (a *App) Source(key string) string
- func (a *App) Status(keys []string) []*StatusInfo
- func (a *App) StatusTable(keys []string) string
- func (a *App) SyncErrors(cfg any) []error
- func (a *App) Unmarshal(dst any) error
- func (a *App) UseConfigFile(name string) *App
- type CommandOptions
- type OptType
- type Options
- type StatusError
- type StatusInfo
Constants ¶
const StatusNotFound = "not found"
StatusNotFound is the error reported for a requested key that has no value.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶ added in v0.1.0
type App struct {
// contains filtered or unexported fields
}
App is a configured application: it owns a cobra root command, its own *viper.Viper, a registry of declared options, and metadata for status reporting. Construct one with New, register option packages (Root/RootGroup/ On/OnGroup), then Run. The same App can be Reloaded to re-read sources.
func (*App) AddConfigPath ¶ added in v0.1.0
AddConfigPath adds a directory searched for the config file.
func (*App) Configure ¶ added in v0.1.0
Configure reads configuration from all sources in precedence order: defaults, CLI bindings, file, then environment. Source attribution is recorded as each source merges; CLI is attributed later, during Run's PreRun, once flags parse. Configure is safe to re-run after Reload.
func (*App) On ¶ added in v0.1.0
func (a *App) On(cmd *cobra.Command, opts ...*CommandOptions) *App
On registers options on cmd under a namespace derived from cmd's path in the tree (cmd "server start" => "server.start.addr"). Flags are local to cmd unless an option's Persistent field is set.
func (*App) OnGroup ¶ added in v0.1.0
OnGroup registers options on cmd under an explicit namespace. Flags are local to cmd unless an option's Persistent field is set.
func (*App) Reload ¶ added in v0.1.0
Reload discards all merged configuration (fresh viper + metadata) and re-runs Configure. The registry, root, and file-config state are kept, so declared options and the command tree persist.
func (*App) Root ¶ added in v0.1.0
func (a *App) Root(opts ...*CommandOptions) *App
Root registers persistent options on the root command under the flat (empty) namespace, so their keys are un-nested (e.g. "env_prefix", "log_level").
func (*App) RootGroup ¶ added in v0.1.0
func (a *App) RootGroup(name string, opts ...*CommandOptions) *App
RootGroup registers a persistent, namespaced package of options on root, so keys nest under name (name "db" => "db.host", "db.port") and inherit to every subcommand. This is the seam for cross-cutting config such as db/mail.
func (*App) Run ¶ added in v0.1.0
Run configures, installs panfigure's PreRun hooks (CLI source attribution and required validation), and executes the root command.
func (*App) SetConfigName ¶ added in v0.1.0
SetConfigName sets the config file name (without directory). If name has a recognizable extension, the config type is inferred from it.
func (*App) SetConfigType ¶ added in v0.1.0
SetConfigType sets the config file format (e.g. "json", "yaml"). Needed only when the config name has no recognizable extension.
func (*App) Status ¶ added in v0.1.0
func (a *App) Status(keys []string) []*StatusInfo
Status returns StatusInfo for the requested keys; an empty slice returns all.
func (*App) StatusTable ¶ added in v0.1.0
StatusTable renders a text table of keys, values, and sources suitable for a terminal "status" command, prefixed with the files parsed.
func (*App) SyncErrors ¶ added in v0.1.0
SyncErrors reflects the App's declared options against the struct pointed to by cfg and returns one error per mismatch:
- a declared option whose key has no compatible struct field;
- a struct field that resolves to no declared option (catches typos like a field "Net" that should be "Network").
It uses the same key normalization as Unmarshal, so a tag-free struct that round-trips through Unmarshal should pass. Embedding and struct tags are not supported in v0.1.0. Returns nil when declarations and the struct agree. SyncErrors does not require Configure to have run.
func (*App) Unmarshal ¶ added in v0.1.0
Unmarshal populates dst from the merged configuration. Config keys (snake_case, dot-nested) match struct fields case- and separator-insensitively, so "db.host" maps to field DB.Host and "base_url" maps to BaseURL without struct tags. dst must be a pointer to a struct.
func (*App) UseConfigFile ¶ added in v0.1.0
UseConfigFile is shorthand for SetConfigName. The file is read during Configure (it merges into the defaults/env state), not at this call.
type CommandOptions ¶
type CommandOptions struct {
// LongOpt is the long CLI flag name without "--", e.g. "db-host" or "addr".
LongOpt string
// ShortOpt is the optional one-letter flag, e.g. "h".
ShortOpt string
// OptName is the config-key leaf; if empty it is derived from LongOpt (with a
// leading "<namespace>_" prefix stripped when present, then '-' -> '_').
OptName string
// Description is shown in --help.
Description string
// OptType selects the parser; omit for a string. Validated up front.
OptType OptType
// NoCLI hides the option from CLI flags (file/env only).
NoCLI bool
// Persistent exposes the flag on subcommands too. Only meaningful for options
// registered via App.On/App.OnGroup; root options are always persistent.
Persistent bool
// Required makes App.Run fail when the resolved value is empty, regardless of
// which source (flag, env, file) supplies it.
Required bool
// DefaultValue is applied when no source provides the option.
DefaultValue any
}
CommandOptions declares a single configuration option: its CLI flag, its config key (leaf), its type, default, and required-ness. Declarations are the source of truth; a plain typed struct populated by App.Unmarshal is the read view. Keep them aligned with App.SyncErrors / AssertSync.
type OptType ¶ added in v0.1.0
type OptType string
OptType identifies the Go type used to parse a CommandOptions value from CLI flags, files, and environment variables. The zero value is equivalent to OptString.
type Options ¶ added in v0.1.0
type Options []*CommandOptions
Options is a named slice of *CommandOptions for readable declarations.
type StatusError ¶
type StatusError struct {
// contains filtered or unexported fields
}
StatusError reports that a requested key has no value.
func (*StatusError) Error ¶
func (e *StatusError) Error() string
type StatusInfo ¶
StatusInfo describes one config key's value and its source.
func (*StatusInfo) String ¶
func (s *StatusInfo) String() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Example application showing panfigure's instance + option packages + typed config model, including precedence and source attribution.
|
Example application showing panfigure's instance + option packages + typed config model, including precedence and source attribution. |