Documentation ¶
Overview ¶
Package config contains helpers for loading and dumping TOML config files
Index ¶
- Constants
- Variables
- func Encoders() []string
- func LoadFile[T any](filename string, v *T, options ...func(*T) error) error
- func New[T any](options ...func(*T) error) (*T, error)
- func Prepare(v any) error
- type Autodetect
- type Decoder
- type Encoder
- type JSON
- type Loader
- func (l *Loader[T]) Last() string
- func (l *Loader[T]) NewFromFile(configFile string, options ...func(*T) error) (*T, error)
- func (l *Loader[T]) NewFromFlag(flag *pflag.Flag, options ...func(*T) error) (*T, error)
- func (l *Loader[T]) NewFromKnownLocations(options ...func(*T) error) (*T, error)
- func (l *Loader[T]) SetDefaults()
- type TOML
- type YAML
Constants ¶
const ( // DefaultJSONIndent is the indentation used unless SetIndent // is called on the JSON Encoder DefaultJSONIndent = ` ` )
const ( // DefaultTOMLIndent is the indentation used unless SetIndent // is called on the TOML Encoder DefaultTOMLIndent = ` ` )
Variables ¶
var ( // CmdName is the base name of default config files if none is // specified on the [Loader] CmdName = filepath.Base(os.Args[0]) // DefaultDirectories is the list of directories to test when trying // to find a config file. DefaultDirectories = []string{".", "/etc", "/etc/" + CmdName} // DefaultExtensions is the list of extensions to test when trying // to find a config file. DefaultExtensions = []string{"conf", "json", "toml", "yaml", "yml"} )
var ( // ErrUnknownFormat indicates we failed to identify the format of the file ErrUnknownFormat = errors.New("file format not identified") )
Functions ¶
func Encoders ¶ added in v0.0.8
func Encoders() []string
Encoders returns all the formats we know to encode
func LoadFile ¶
LoadFile loads a config file by name, expanding environment variables, filling gaps and validating its content. LoadFile uses the file extension to determine the format.
Types ¶
type Autodetect ¶ added in v0.0.8
type Autodetect struct{}
Autodetect is a Decoder that tries all Decoders
func (*Autodetect) Decode ¶ added in v0.0.8
func (p *Autodetect) Decode(data string, v any) error
Decode takes encoded data and tries all registered decoders. Decode returns ErrUnknownFormat if all decoders fail.
type Decoder ¶ added in v0.0.8
A Decoder uses text to populate a data structure.
func NewDecoder ¶ added in v0.0.8
NewDecoder returns the decoder associated with a name or extension.
func NewDecoderByFilename ¶ added in v0.0.8
NewDecoderByFilename uses the file extension to determine the decoder.
type Encoder ¶ added in v0.0.8
Encoder serializes a data structure
func NewEncoder ¶ added in v0.0.8
NewEncoder returns a encoder for the specified format
type JSON ¶ added in v0.0.8
type JSON struct {
Indent string
}
JSON is a Encoder/Decoder for JSON
func NewJSON ¶ added in v0.0.8
func NewJSON() *JSON
NewJSON creates a new JSON Encoder/Decoder with two space indentation.
type Loader ¶ added in v0.1.1
type Loader[T any] struct { Base string Directories []string Extensions []string Others []string // contains filtered or unexported fields }
Loader helps finding and location configuration files
func (*Loader[T]) Last ¶ added in v0.1.1
Last returns the filename of the last config file to be used
func (*Loader[T]) NewFromFile ¶ added in v0.1.1
NewFromFile loads a config file by name, followed by initialization, filling gaps, and validation.
func (*Loader[T]) NewFromFlag ¶ added in v0.1.1
NewFromFlag uses a cobra Flag for the config file name. if not specifies the known locations will be tried, and if all fails, it will create a default one.
func (*Loader[T]) NewFromKnownLocations ¶ added in v0.1.1
NewFromKnownLocations scans locations specified in the Loader for a config file, if not possible it will create a default one.
func (*Loader[T]) SetDefaults ¶ added in v0.1.1
func (l *Loader[T]) SetDefaults()
SetDefaults fills the gaps in the Loader config. This is not required as LoadFileFlag() and LoadKnownLocations() will call it automatically before trying to find
type TOML ¶ added in v0.0.8
type TOML struct {
Indent string
}
TOML is a Encoder/Decoder for TOML
func NewTOML ¶ added in v0.0.8
func NewTOML() *TOML
NewTOML creates a new TOML Encoder/Decoder with two space indentation.
type YAML ¶ added in v0.0.8
type YAML struct {
Spaces int
}
YAML is a Encoder/Decoder for YAML
func NewYAML ¶ added in v0.0.8
func NewYAML() *YAML
NewYAML creates a new YAML Encoder/Decoder with two spaces as default indentation.