A basic configuration loading system for Go
Find a file
2024-10-04 17:15:09 +01:00
cfger.go Add global GetEnv function 2024-10-04 17:14:12 +01:00
CHANGELOG v0.2.1 2024-10-04 17:15:09 +01:00
go.mod Change import path and update example in README 2024-06-18 21:52:21 +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 Change import path and update example in README 2024-06-18 21:52:21 +01:00
types.go Add parsing postprocessing functions and better doc comments 2024-06-18 21:49:14 +01:00

cfger

A basic configuration loading system


Go Reference

Install

go get go.akpain.net/cfger

Example usage

package config

import (
	"cmp"
	"go.akpain.net/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: cmp.Or(cl.Get("debug").AsBool(), cl.GetEnv("DEBUG").AsBool()),
		HTTP: &HTTP{
			Host: cl.Get("http.host").WithDefault("127.0.0.1").AsString(),
			Port: cl.Get("http.port").WithDefault(8080).AsInt(),
		},
		Database: &Database{
			DSN: cl.Get("database.dsn").Required().AsString(),
		},
	}

	return conf, nil
}

License

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