No description
Find a file
2024-10-23 18:07:34 -07:00
testdata Initial commit 2024-10-23 18:07:34 -07:00
conflib.go Initial commit 2024-10-23 18:07:34 -07:00
conflib_test.go Initial commit 2024-10-23 18:07:34 -07:00
go.mod Initial commit 2024-10-23 18:07:34 -07:00
go.sum Initial commit 2024-10-23 18:07:34 -07:00
LICENSE Initial commit 2024-10-23 18:07:34 -07:00
README.md Initial commit 2024-10-23 18:07:34 -07:00

conflib

yet another configuration library

conflib is a small library to load configuration from environment variables or disk. Desired configuration is defined in a struct, then populated from a list of potential files, as well as environment variables.

Let's look at example usage:

package main

import (
	"log"

	"git.janky.solutions/finn/conflib"
)

type Config struct {
	Sample    string `env:"SAMP"`
	MyExample int    `default:"9"`
	Nested    NestedConfig
}

type NestedConfig struct {
	SomeVariable string
}

func main() {
	config, err := conflib.Load[Config]("/etc/myapp.json", "myapp.json")
	if err != nil {
		log.Fatal(err)
	}

	log.Println("loaded config: ", config)
}

This will attempt to read /etc/myapp.json and myapp.json and unmarshal them into a Config struct. It will also check for environment variables that match the struct field names, unless overwridden. So the Sample field will be populated from the SAMP environment variable due to the env tag on that field. config.MyExample will be populated from environment variable MY_EXAMPLE, and config.Nested.SomeVariable will be populated by environment variable NESTED_SOME_VARIABLE. If none of the listed files are found and no environment variables match any field, an error will be returned.