Welcome to the world of gonfiguration, where setting up your Golang configs is as easy as stealing candy from a baby, but way more ethical. We’re talking about slapping defaults and env vars into your structs without the usual headache.
The Code: Where the Magic Happens
package main
import (
"fmt"
"log"
"os"
"github.com/psyb0t/gonfiguration"
)
type config struct {
ListenAddress string `env:"LISTEN_ADDRESS"`
DBDSN string `env:"DB_DSN"`
DBName string `env:"DB_NAME"`
DBUser string `env:"DB_USER"`
DBPass string `env:"DB_PASS"`
}
func main() {
cfg := config{}
gonfiguration.SetDefaults(map[string]interface{}{
"LISTEN_ADDRESS": "127.0.0.1:8080",
"DB_DSN": "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable",
})
// Env var setting, because we can
if err := os.Setenv("DB_NAME", "postgres"); err != nil {
log.Fatalf("holy fuque! can't set env: %v", err)
}
// Repeat for other vars...
gonfiguration.Parse(&cfg)
fmt.Printf("%+v\n", cfg)
}
Installation: Get Your Hands Dirty
go get github.com/psyb0t/gonfiguration
Usage: Because Reading Docs is for Chumps
Step 1: Define Your Config Struct
type MyAwesomeConfig struct {
ListenAddress string `env:"LISTEN_ADDRESS"`
DBDSN string `env:"DB_DSN"`
DBName string `env:"DB_NAME"`
DBUser string `env:"DB_USER"`
DBPass string `env:"DB_PASS"`
}
Step 2: Set Defaults, Because We’re Not Savages
gonfiguration.SetDefaults(map[string]interface{}{
"LISTEN_ADDRESS": "127.0.0.1:8080",
// ... and so on for your other config variables.
})
Step 3: Parse It, Don’t Curse It
cfg := MyAwesomeConfig{}
if err := gonfiguration.Parse(&cfg); err != nil {
log.Fatalf("whoa there, partner! can't parse config: %v", err)
}
Step 4: Snoop Around with GetAllValues()
allTheSecrets := gonfiguration.GetAllValues()
Step 5: Reset, Because Sometimes We Screw Up
gonfiguration.Reset()
Development: For the Code Warriors
make dep
– Dependency chaos controlmake lint
– Linting, because even rebels need rulesmake test
&make test-coverage
– Test your mettle
Contributing: Join the Anarchy
PRs? Issues? Bring ’em on. We’re all about causing a ruckus together.
License: The Wild West of Copyright
Do whatever the hell you want with this. Just keep my name on it and don’t come crying if it blows up your system. I’m just here for the chaos.
Now you got to the best part where I’m sending you away: https://github.com/psyb0t/gonfiguration