Documentation
¶
Index ¶
- Variables
- func LocateFromExeUp(filename string) (string, error)
- func LocateFromWorkingDirUp(filename string) (string, error)
- func ToJSON(src []byte) []byte
- func ToJSONInPlace(src []byte) []byte
- type AntConfig
- func (a *AntConfig) BindConfigFlags(fs *flag.FlagSet) error
- func (a *AntConfig) ConfigPath() string
- func (a *AntConfig) EnvHelpString() string
- func (a *AntConfig) EnvPath() string
- func (a *AntConfig) FlagArgs() []string
- func (a *AntConfig) FlagPrefix() string
- func (a *AntConfig) ListFlags(c any) ([]FlagSpec, error)
- func (a *AntConfig) MustBindConfigFlags(fs *flag.FlagSet) *AntConfig
- func (a *AntConfig) MustSetConfig(cfg any) *AntConfig
- func (a *AntConfig) SetConfig(cfg any) error
- func (c *AntConfig) SetConfigPath(path string) error
- func (c *AntConfig) SetEnvPath(path string) error
- func (c *AntConfig) SetFlagArgs(args []string)
- func (c *AntConfig) SetFlagPrefix(prefix string)
- func (a *AntConfig) WriteConfigValues() error
- type FlagSpec
Constants ¶
This section is empty.
Variables ¶
var ErrConfigNotFound = errors.New("config file not found")
Errors
var ErrEnvFileNotFound = errors.New("environment file not found")
Functions ¶
func LocateFromExeUp ¶
LocateFromExeUp searches for filename starting from the directory of the current executable and then walking upward up to 10 levels. Returns the first match or ErrConfigNotFound.
func LocateFromWorkingDirUp ¶
LocateFromWorkingDirUp searches for filename starting from the current working directory and then walking upward up to 10 levels. Returns the first match or ErrConfigNotFound.
func ToJSON ¶
ToJSON converts JSONC (JSON with // and /* */ comments and trailing commas) into strict JSON suitable for json.Unmarshal. It allocates a new buffer.
func ToJSONInPlace ¶
ToJSONInPlace is the same as ToJSON, but this method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.
Types ¶
type AntConfig ¶
type AntConfig struct {
// contains filtered or unexported fields
}
AntConfig is a small, zero-dependency configuration helper that applies values to a tagged struct from (in order): defaults, config file (JSON/JSONC), .env file, OS environment variables, and command-line flags.
Use New() to construct, MustSetConfig/SetConfig to register your struct pointer, optionally BindConfigFlags to register flags on a flag.FlagSet, then call WriteConfigValues() to apply.
func (*AntConfig) BindConfigFlags ¶
BindConfigFlags registers flags for all fields tagged with `flag:"name"` onto the provided FlagSet. It respects the configured prefix (via SetFlagPrefix) for the CLI names. This method does not parse or apply flags; call fs.Parse(...) yourself, then WriteConfigValues to apply. It also binds the FlagSet to AntConfig so WriteConfigValues reads values from it. Requires SetConfig to be called first.
func (*AntConfig) ConfigPath ¶
ConfigPath returns the configured config file path, if any.
func (*AntConfig) EnvHelpString ¶
EnvHelpString builds a help section for environment variables that can configure fields of the registered config struct. It returns a string formatted to append after flag usage output, using the same two-space indentation convention as flag.PrintDefaults. Requires SetConfig to have been called; otherwise returns an empty string.
func (*AntConfig) FlagPrefix ¶
FlagPrefix returns the CLI flag prefix, if any.
func (*AntConfig) ListFlags ¶
ListFlags returns the set of CLI flags for fields tagged with `flag:"name"`. If a flag prefix is set, the returned CLI names include the prefix.
func (*AntConfig) MustBindConfigFlags ¶
MustBindConfigFlags is like BindConfigFlags but panics on error. It returns the receiver to allow simple chaining with New()/MustSetConfig.
func (*AntConfig) MustSetConfig ¶
MustSetConfig is like SetConfig but panics on error. It returns the receiver to allow simple chaining: antconfig.New().MustSetConfig(&cfg).
func (*AntConfig) SetConfig ¶
SetConfig stores a reference to the config pointer for later operations like BindConfigFlags. cfg must be a non-nil pointer to a struct.
func (*AntConfig) SetConfigPath ¶
SetConfigPath sets the path to a JSON/JSONC config file and validates it exists. When not set, WriteConfigValues will auto-discover config.jsonc or config.json by walking upward from the current working directory.
func (*AntConfig) SetEnvPath ¶
SetEnvPath sets the path to a .env file and validates it exists. When not set, WriteConfigValues will auto-discover a .env in the current working directory.
func (*AntConfig) SetFlagArgs ¶
SetFlagArgs sets the CLI arguments that should be used for flag overrides. If not provided, WriteConfigValues falls back to os.Args[1:].
func (*AntConfig) SetFlagPrefix ¶
SetFlagPrefix sets an optional CLI flag prefix (e.g., "config-").
func (*AntConfig) WriteConfigValues ¶
WriteConfigValues applies configuration values to the struct registered via SetConfig/MustSetConfig, in this precedence order:
- default values from `default:"…"` tags
- config file (JSON/JSONC) from SetConfigPath or auto-discovery
- .env file from SetEnvPath or auto-discovery (does not override existing OS env)
- OS environment variables from `env:"NAME"` tags (non-empty values override)
- command-line flags from a bound FlagSet (BindConfigFlags) or from SetFlagArgs/os.Args
Returns an error on invalid inputs, I/O, or parsing failures.
type FlagSpec ¶
type FlagSpec struct {
// Name is the logical flag name from the tag (without prefix), e.g., "secret".
Name string
// CLI is the concrete CLI flag including any configured prefix, e.g., "config-secret".
CLI string
// Kind is the Go kind for the target field (string, int, bool, float64, slice).
Kind string
}
FlagSpec describes a single CLI flag derived from a struct field with a `flag:"name"` tag.
