chu

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 10 Imported by: 0

README

chu

Configuration library to load from multiple sources.

go get github.com/rakunlabs/chu

Usage

Define a struct to hold the configuration.

type Config struct {
    Name string `cfg:"name"`
    Age  int    `cfg:"age"`
}

And load the configuration.

cfg := Config{}

if err := chu.Load(ctx, "test", &cfg); err != nil {
    return fmt.Errorf("failed to load config: %w", err)
}

The configuration will be loaded from the following sources in order:
- Default
- File
- Environment

Loaders

Check example folder to see how to use loaders with different kind of configuration.

Default

Default loader is used to set default values from tag default.

type Config struct {
    Name string `cfg:"name" default:"John"`
    Age  int    `cfg:"age"  default:"30"`
}

Default supports numbers, string, bool, time.Duration and pointer of that types.

File

File loader is used to load configuration from file.

First checking CONFIG_PATH env value and try current location to find in order of .toml, .yaml, .yml, .json extension with using given name.

Environment

Environment loader is used to load configuration from environment variables.

env or cfg tag can usable for environment loader.

export NAME=John
export AGE=30
type Config struct {
    Name string `cfg:"name"`
    Age  int    `cfg:"age"`
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLoaders = []LoadHolder{
		{Name: "default", Loader: defaultloader.New()},
		{Name: "file", Loader: fileloader.New()},
		{Name: "env", Loader: envloader.New()},
	}
	DefaultHooks = []loader.HookFunc{
		loader.HookTimeDuration,
	}
	DefaultOptions = []Option{}
)

Functions

func Load

func Load(ctx context.Context, name string, to any, opts ...Option) error

Load loads the configuration from loaders.

Types

type LoadHolder

type LoadHolder struct {
	Name   string
	Loader Loader
}

type Loader

type Loader interface {
	LoadChu(ctx context.Context, to any, opts ...loader.Option) error
}

type Option

type Option func(*option)

func WithHook

func WithHook(hooks ...loader.HookFunc) Option

WithHook adds hooks for conversion.

func WithHookSet

func WithHookSet(hooks ...loader.HookFunc) Option

WithHookSet sets the hooks for conversion.

func WithLoaders

func WithLoaders(loaders ...LoadHolder) Option

WithLoaders sets the loaders to use when loading the configuration.

  • order matters

func WithLogger

func WithLogger(logger logadapter.Adapter) Option

WithLogger sets the logger for logging.

func WithTag

func WithTag(tag string) Option

WithTag sets the tag for the configuration.

  • default is "cfg"

func WithWeaklyDashUnderscore

func WithWeaklyDashUnderscore(v bool) Option

WithWeaklyDashUnderscore sets the weakly dash underscore option.

  • default is false

func WithWeaklyIgnoreSeperator

func WithWeaklyIgnoreSeperator(v bool) Option

WithWeaklyIgnoreSeperator sets the weakly ignore separator option.

  • default is true

Jump to

Keyboard shortcuts

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