orale

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2024 License: MIT Imports: 7 Imported by: 0

README

Órale (Oh-rah-leh)

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?", and is prenounced like "Oh-rah-leh".



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.GetAll(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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Test_SetArgs added in v1.2.0

func Test_SetArgs(args []string)

func Test_SetEnvironment added in v1.2.0

func Test_SetEnvironment(env []string)

func Test_SetWorkingDir added in v1.2.0

func Test_SetWorkingDir(dir string)

Types

type File

type File struct {
	// Path is the absolute path to the configuration file.
	Path string
	// Values is a map of configuration values loaded from the file. Note that
	// these values are flattened into paths separated by periods. Slice indexes
	// are represented by square brackets with the index inside. The value is
	// always a slice of any. It's a slice because theoretically a configuration
	// file could have multiple values for the same path. This is not the case with
	// toml so as of now it's always a slice of length 1.
	Values map[string][]any
}

File represents a configuration file loaded from disk.

type Loader

type Loader struct {
	// FlagValues is a map of flag values by path.
	FlagValues map[string][]any
	// EnvironmentValues is a map of environment variable values by path.
	EnvironmentValues map[string][]any
	// ConfigurationFiles is a slice of configuration files.
	ConfigurationFiles []*File
}

Loader is a struct that contains all the values loaded from flags, environment variables, and configuration files. It can be used to marshal the values into a struct.

func Load

func Load(applicationName string) (*Loader, error)

Load loads configuration values from flags, environment variables, and configuration files. Flags are taken from `os.Args[1:]`. Environment variables are taken from `os.Environ()`. Configuration files are taken from the working directory and all parent directories. The configuration file name is the application name with the extension `.config.toml`. If the name contain

func LoadFromValues

func LoadFromValues(programArgs []string, envVarPrefix string, envVars []string, configSearchStartPath string, configFileNames []string) (*Loader, error)

LoadFromValues works like Load, but allows the caller to specify configuration such as flag and environment values, as well as which path to start searching for configuration files and which configuration file names to look for.

func (*Loader) Get

func (l *Loader) Get(path string, target any) error

```go

type TestConfig struct {
	Database struct {
		ConnectionUri string `config:"connection_uri"`
	} `config:"database"`
	Server struct {
		Port int `config:"port"`
	} `config:"server"`
	Channels []struct {
		Name string `config:"name"`
		Id   int    `config:"id"`
	} `config:"channels"`
}

loader, err := orale.Load("my-app")
if err != nil {
	panic(err)
}

var testConfig TestConfig
if err := loader.Get("", &testConfig); err != nil {
	panic(err)
}

```

As you can see in the example above, the TestConfig struct is populated with the loaded configuration values. The property names of each field are specified by the `config` tag. If the `config` tag is not specified, the property name is converted to snake case. For example `ConnectionUri` becomes `connection_uri` path.

func (*Loader) GetAll added in v1.1.0

func (l *Loader) GetAll(target any) error

func (*Loader) MustGet

func (l *Loader) MustGet(path string, target any)

MustGet is the same as Get except it panics if an error occurs.

func (*Loader) MustGetAll added in v1.1.0

func (l *Loader) MustGetAll(target any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL