primordius package - github.com/KaiserWerk/primordius - Go Packages

primordius

package module
v0.0.0-...-a970915 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 8 Imported by: 0

README

primordius

Primordius is a Go library to read configuration values from different sources (YAML, JSON, TOML, env vars).

Installation

Just your typical go get github.com/KaiserWerk/primordius.

Usage

First, define your configuration struct. Can be any flat struct you like, e.g.

type Config struct {
    BaseURL      string `json:"base_url" yaml:"base_url" env:"PROXY_BASEURL"`
    Key          string `json:"key" yaml:"key" env:"KEY"`
    NumBackups   uint   `yaml:"num_backups" env:"NUM_BACKUPS"`
    ProxyEnabled bool   `env:"PROXY_ENABLED"`
}

Make sure you define the tags as required. Use the tag env for values that should be read from environment variables.

Then, create an instance of your configuration struct and maybe set some default values:

c := Config{
    BaseURL: "https://my.greenhouse.lan/",
    Key:     "mysupersecretkey",
}

Then, a new Primordius instance:

pr := primordius.New(&c)

It is important to note that you MUST supply a pointer to a struct as target.

The next step is to set up the desired sources. There are some default sources you can add directly on the Primordius struct:

// Reads from a JSON block, maybe obtained by an external service
pr.FromJSON([]byte(`{"key": "some-key"}`))
// Reads from the supplied file
pr.FromJSONFile(`C:\Local\app.prod.json`)
// Reads from an io.Reader
pr.FromJSONReader(r.Body)
// Reads from a YAML block, maybe obtained by an external service
pr.FromYAML([]byte(`num_backups: 16`))
// Reads from the supplied file
pr.FromYAMLFile(`/opt/local/app.yaml`)
// Reads from an io.Reader
pr.FromYAMLReader(strings.NewReader(`base_url: "http://some-url"`))
// Reads from a YAML block, maybe obtained by an external service
pr.FromTOML([]byte(`data = [ ["delta", "phi"], [3.14] ]`))
// Reads from the supplied file
pr.FromTOMLFile("C:\\Users\\SomeUser\\AppData\\Local\\my-app\\config.prod.toml")
// Reads from an io.Reader
pr.FromTOMLReader(resp.Body)
// Reads from the env vars defined in the 'env' tag combined with the supplied
// prefix. If you don't need a prefix, supply an empty string.
pr.FromEnv("MY_APP_")

Sources are processed in the order they were registered meaning the last source has the highest priority.

Lastly, call pr.Process():

err := pr.Process()
if err != nil {
    log.Fatal(err)
}

Now your configuration is populated with the values read from the sources and ready to be used!

Custom sources

You have a different resource you want to read configuration values from? All sources must implement the primordius.Source interface which defines just a single method:

// Source defines an origin writing found configuration values into t.
Source interface {
    // ToTarget writes configuration values into t. t MUST be a pointer to a struct.
    ToTarget(t any) error
}

You can add your custom Source like this:

type mySource struct {}
func (ms *mySource) ToTarget(target any) error { /* TODO implement */ }
s := &mySource{}
pr.AddSource(s)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSpecification = errors.New("specification must be a struct pointer")

Functions

This section is empty.

Types

type Primordius

type Primordius struct {
	// contains filtered or unexported fields
}

Primordius manages sources and processes them into the set target.

func New

func New(target any) *Primordius

New allocates and returns a new instance of Primordius with the supplied target. target MUST be a pointer to a struct.

func (*Primordius) AddSource

func (pr *Primordius) AddSource(s Source)

AddSource adds a Source s to pr to obtain arbitrary configuration values from. Can also be used to add a custom Source.

func (*Primordius) FromEnv

func (pr *Primordius) FromEnv(prefix string)

FromEnv adds a Source to pr which reads values from environment variables.

func (*Primordius) FromJSON

func (pr *Primordius) FromJSON(content []byte)

FromJSON adds a Source to pr which reads values from a JSON block.

func (*Primordius) FromJSONFile

func (pr *Primordius) FromJSONFile(name string)

FromJSONFile adds a Source to pr which reads values from a JSON file.

func (*Primordius) FromJSONReader

func (pr *Primordius) FromJSONReader(r io.Reader)

FromJSONReader adds a Source to pr which reads YAML content from r.

func (*Primordius) FromTOML

func (pr *Primordius) FromTOML(content []byte)

func (*Primordius) FromTOMLFile

func (pr *Primordius) FromTOMLFile(name string)

func (*Primordius) FromTOMLReader

func (pr *Primordius) FromTOMLReader(r io.Reader)

func (*Primordius) FromYAML

func (pr *Primordius) FromYAML(content []byte)

FromYAML adds a Source to pr which reads values from a YAML block.

func (*Primordius) FromYAMLFile

func (pr *Primordius) FromYAMLFile(name string)

FromYAMLFile adds a Source to pr which reads values from a YAML file.

func (*Primordius) FromYAMLReader

func (pr *Primordius) FromYAMLReader(r io.Reader)

FromYAMLReader adds a Source to pr which reads JSON content from r.

func (*Primordius) Process

func (pr *Primordius) Process() error

Process calls all registered Sources to write values into pr.target. Registered sources are processed in the order they were initially added.

func (*Primordius) ResetSources

func (pr *Primordius) ResetSources()

ResetSources empties the internal list of registered Sources.

type Source

type Source interface {
	// ToTarget writes configuration values into t. t MUST be a pointer to a struct.
	ToTarget(t any) error
}

Source defines an origin writing found configuration values into t.

Jump to

Keyboard shortcuts

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