config

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package config contains helpers for loading and dumping TOML config files

Index

Constants

View Source
const (
	// DefaultJSONIndent is the indentation used unless SetIndent
	// is called on the JSON Encoder
	DefaultJSONIndent = `  `
)
View Source
const (
	// DefaultTOMLIndent is the indentation used unless SetIndent
	// is called on the TOML Encoder
	DefaultTOMLIndent = `  `
)

Variables

View Source
var (
	// CmdName is the base name of default config files if none is
	// specified on the [Loader]
	CmdName = filepath.Base(os.Args[0])
	// DefaultExtensions is the list of extensions to test when trying
	// to find a config file.
	DefaultExtensions = []string{"conf", "json", "toml", "yaml", "yml"}
)
View Source
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

func LoadFile[T any](filename string, v *T,
	options ...config.Option[T]) error

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.

func New added in v0.1.0

func New[T any](options ...config.Option[T]) (*T, error)

New creates a new config, applying the initialization functions, filling gaps and validating its content.

func NewDecoderFactory added in v0.4.0

func NewDecoderFactory[T any](getenv func(string) string) func(string) (config.Decoder[T], error)

NewDecoderFactory returns a config.Decoder factory to use with config.Loader, using our registered decoders.

func Prepare

func Prepare(v any) error

Prepare fills any gap in the object and validates its content.

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

type Decoder interface {
	Decode(data string, v any) error
}

A Decoder uses text to populate a data structure.

func NewDecoder added in v0.0.8

func NewDecoder(name string) (Decoder, error)

NewDecoder returns the decoder associated with a name or extension.

func NewDecoderByFilename added in v0.0.8

func NewDecoderByFilename(filename string) (Decoder, error)

NewDecoderByFilename uses the file extension to determine the decoder.

type Encoder added in v0.0.8

type Encoder interface {
	SetIndent(indent string) bool
	WriteTo(v any, w io.Writer) (int64, error)
}

Encoder serializes a data structure

func NewEncoder added in v0.0.8

func NewEncoder(name string) (Encoder, error)

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.

func (JSON) Decode added in v0.0.8

func (JSON) Decode(data string, v any) error

Decode takes JSON encoded data to populate a data structure.

func (*JSON) SetIndent added in v0.0.8

func (p *JSON) SetIndent(indent string) bool

SetIndent specifies what to do for indenting the serialized JSON content.

func (*JSON) WriteTo added in v0.0.8

func (p *JSON) WriteTo(v any, w io.Writer) (int64, error)

WriteTo encodes data into a given io.Writer. If encoding fails, the writer won't be touched.

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

func (l *Loader[T]) Last() (fs.FS, string)

Last returns the filename of the last config file to be used

func (*Loader[T]) NewFromFile added in v0.1.1

func (l *Loader[T]) NewFromFile(configFile string, options ...config.Option[T]) (*T, error)

NewFromFile loads a config file by name, followed by initialization, filling gaps, and validation.

func (*Loader[T]) NewFromFlag added in v0.1.1

func (l *Loader[T]) NewFromFlag(flag *pflag.Flag, options ...config.Option[T]) (*T, error)

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

func (l *Loader[T]) NewFromKnownLocations(options ...config.Option[T]) (*T, error)

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.

func (*TOML) Decode added in v0.0.8

func (*TOML) Decode(data string, v any) error

Decode takes TOML encoded data to populate a data structure

func (*TOML) SetIndent added in v0.0.8

func (p *TOML) SetIndent(indent string) bool

SetIndent specifies what to do for indenting the serialized TOML content.

func (*TOML) WriteTo added in v0.0.8

func (p *TOML) WriteTo(v any, w io.Writer) (int64, error)

WriteTo encodes data into a given io.Writer. If encoding fails, the writer won't be touched.

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.

func (*YAML) Decode added in v0.0.8

func (*YAML) Decode(data string, v any) error

Decode takes YAML encoded data to populate a data structure

func (*YAML) SetIndent added in v0.0.8

func (p *YAML) SetIndent(s string) bool

SetIndent specifies what to use for indenting the serialized YAML content. Tabs are expanded to 8 spaces.

func (*YAML) WriteTo added in v0.0.8

func (p *YAML) WriteTo(v any, w io.Writer) (int64, error)

WriteTo encodes data into a given io.Writer. If encoding fails, the writer won't be touched.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL