Documentation
¶
Index ¶
- Constants
- func AssertSync(t *testing.T, app *App, cfg any)
- type App
- func (a *App) AddConfigDir(dir string) *App
- func (a *App) AddConfigFile(path string) *App
- func (a *App) AddConfigPath(p string) *App
- func (a *App) Configure() error
- func (a *App) Get(key string) any
- 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) Set(key string, value any) *App
- 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
- func (a *App) WriteSubset(prefix, path string) error
- 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) AddConfigDir ¶ added in v0.1.4
AddConfigDir adds a directory whose files are read during Configure, in the position added. This is the "package.d" / *.conf.d pattern: every top-level file with a recognized config extension (json, yaml, toml, ...) is merged in ascending filename order (prefix files with 00-, 10- to control that order), each later file overriding the earlier ones. Dotfiles and subdirectories are skipped, and the directory is not searched recursively. A missing or empty directory is not an error.
func (*App) AddConfigFile ¶ added in v0.1.4
AddConfigFile adds a single config file to be read during Configure. Files are read in the order added; a later file overrides an earlier one at key granularity. A missing file is not an error (it is skipped); an unreadable or malformed file is. The file's format is inferred from its extension.
func (*App) AddConfigPath ¶ added in v0.1.0
AddConfigPath adds a directory searched for the config file named by SetConfigName/UseConfigFile. Paths are searched in the order added; viper reads the first match.
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) Get ¶ added in v0.1.3
Get returns the merged, resolved value for a config key across all sources (default < file < env < cli). It is the single-value counterpart to Unmarshal, intended for predicates such as CommandOptions.RequiredWhen that must inspect other options' resolved values at validation time.
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) Set ¶ added in v0.1.2
Set assigns value to key in the merged configuration, returning the App for chaining. Use it to inject values that no source supplies or that are not declared options — for example a managed "install.installed" marker written to a generated config file. Set values are serialized by WriteSubset like any other. Set does not attribute a source; Status reports such keys as "unknown".
func (*App) SetConfigName ¶ added in v0.1.0
SetConfigName sets the config file name (without directory) and appends a search source to the file list, capturing the paths added so far via AddConfigPath. If name has a recognizable extension, the config type is inferred from it. The file is read during Configure.
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. It also serves as a fallback for explicitly added files (AddConfigFile) whose path has no 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 (merged into the defaults/env state), not at this call.
func (*App) WriteSubset ¶ added in v0.1.2
WriteSubset writes every currently-merged key whose name begins with prefix to the file at path. The format is inferred from the file's extension (".json" -> JSON) and is re-readable by panfigure's normal file read (AddConfigPath + SetConfigName), so a write followed by a read round-trips. The directory at path must already exist; an existing file is overwritten.
Values are serialized from panfigure's own merged configuration. Keys need not be declared options: values injected with Set are written too, so long as they fall under prefix. An empty prefix writes the entire merged configuration.
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
// RequiredWhen, when non-nil, is evaluated during required validation (after
// sources merge) and its result takes the place of Required for this run: the
// option is required only when the predicate returns true. When nil, Required
// governs as usual, so the zero value preserves existing behavior. The
// predicate receives the *App so it can inspect other options' resolved values
// via App.Get (e.g. require a set of LDAP options only when user-management is
// "ldap"). RequiredWhen, when set, overrides Required.
RequiredWhen func(*App) 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. |