A basic configuration loading system for Go
Find a file
2024-06-18 21:27:04 +01:00
cfger.go Fix docs using wrong function name 2024-06-18 21:27:04 +01:00
CHANGELOG Add CHANGELOG 2023-08-21 16:49:24 +01:00
go.mod Initial commit 2023-08-21 16:47:32 +01:00
go.sum Initial commit 2023-08-21 16:47:32 +01:00
LICENSE Initial commit 2023-08-21 16:47:32 +01:00
README.md Update README.md 2023-08-21 16:48:17 +01:00
types.go Initial commit 2023-08-21 16:47:32 +01:00

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.