cfger

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: 0BSD Imports: 7 Imported by: 1

README

cfger

A basic configuration loading system


Go Reference

Install

go get git.tdpain.net/pkg/cfger

Example usage

package config

import (
	"git.tdpain.net/pkg/cfger"
)

type HTTP struct {
	Host string
	Port int
}

type Database struct {
	DSN string
}

type Config struct {
	Debug       bool
	HTTP        *HTTP
	Database    *Database
}

func Load() (*Config, error) {
    cl := cfger.New()
	if err := cl.Load("config.yml"); err != nil {
		return nil, err
	}

	conf := &Config{
		Debug: cl.Required("debug").AsBool(),
		HTTP: &HTTP{
			Host: cl.WithDefault("http.host", "127.0.0.1").AsString(),
			Port: cl.WithDefault("http.port", 8080).AsInt(),
		},
		Database: &Database{
			DSN: cl.WithDefault("database.dsn", "website.db").AsString(),
		},
	}

	return conf, nil
}

License

This project is licensed under the BSD Zero Clause License. See ./LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FatalErrorHandler = func(err error) {
	_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
}

FatalErrorHandler is called with a fatal error before cfger calls os.Exit. This is to allow fatal errors to be processed correctly depending on the application itself.

Functions

This section is empty.

Types

type ConfigLoader

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

func New

func New() *ConfigLoader

func (*ConfigLoader) Get

func (cl *ConfigLoader) Get(key string) OptionalItem

Get gets a given key from the currently loaded configuration.

Within a key representation, a dot represents a recursion into a named object and square brackets represent an index in a named array. You could access the value "hello" using the key `alpha[0].beta` in the below example.

alpha:
 - beta: "hello"

func (*ConfigLoader) Load

func (cl *ConfigLoader) Load(fname string) error

Load reads a YAML config file from fname and holds it in memory.

If fname cannot be found, a blank config file will be loaded instead.

func (*ConfigLoader) Required

func (cl *ConfigLoader) Required(key string) OptionalItem

Required gets a given key from the currently loaded configuration and raises a fatal error if it cannot be found.

See documentation for Get for key format.

func (*ConfigLoader) WithDefault

func (cl *ConfigLoader) WithDefault(key string, defaultValue any) OptionalItem

Required gets a given key from the currently loaded configuration and returns a default value if it cannot be found.

See documentation for Get for key format.

type OptionalItem

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

func (OptionalItem) AsBool

func (x OptionalItem) AsBool() bool

func (OptionalItem) AsInt

func (x OptionalItem) AsInt() int

func (OptionalItem) AsString

func (x OptionalItem) AsString() string

Jump to

Keyboard shortcuts

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