RobertWHurst / Orale

A great little configuration loader for go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Órale

A fantastic little config loader for go that collects configuration from flags, environment variables, and configuration files, then marshals the found values into your given structs.

Órale is pronounced Odelay in English. The name is Mexican slang which translates roughly to "listen up" or "what's up?"






Usage

Below is an example of how you can use Orale. Note that in this example we are loading all config values into a single struct, but it is possible to load just a path by it as the first argument to MustGet or Get.

package main

import (
  "github.com/RobertWHurst/Orale"

  ...
)

type Config struct {
  Database *database.Config `config:"db"`
  ServerPorts []string `config:"server_port"`
}

func main() {
  oraleConf, err := orale.Load("my-app")
  if err != nil {
    ...
  }

  var conf *Config
  if err := oraleConf.Get("", conf); err != nil {
    ...
  }

  db, err := database.Connect(conf.Database)
  if err != nil {
    ...
  }

  var servers []server.Server
  for _, port := range conf.ServerPorts {
    servers = append(servers, server.Listen(db, port))
  }

  ...
}

For this example lets assume database.Config looks like this:

package database

...

type Config struct {
  Uri string `config:"connection_string"`
  ConnectionPoolSize int
}

One thing to note is that internally Orale treats all configuration paths as snake case, so like the example above, when you annotate the path of your config value, make sure to use snake case.

If we gave the following flags...:

my-app --server-port=8000 --server-port=7080

environment variable...:

MY_APP__DB__CONNECTION_POOL_SIZE=3

and config file (my-app.config.toml)...:

[db]
connection_string="protocol://..."

The config struct in the first example would contain the following values:

&Config{
  Database: &database.Config{
    Uri: "protocol://...",
    ConnectionPoolSize: 3,
  },
  ServerPorts: [8000, 7080],
}

This project is still under development, but the above should at least give you some things to try out.

About

A great little configuration loader for go

License:MIT License


Languages

Language:Go 100.0%