configo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2021 License: MIT Imports: 17 Imported by: 3

README

Configo

A Go port of node-config

Installing

go get -u github.com/affanshahid/configo

Documentation

Documentation

Overview

Example
package main

import (
	"fmt"
	"os"
	"testing/fstest"

	"github.com/affanshahid/configo"
)

func main() {
	dir := fstest.MapFS{
		"default.yml": {
			Data: []byte(`
                root:
                  prop1: foo
                  prop2: 100
                  prop3: false
                  prop4:
                    nestedProp1: 4
            `),
		},
		"production.yml": {
			Data: []byte(`
                root:
                  prop2: 200
            `),
		},
	}

	os.Setenv("APP_ENV", "production")

	err := configo.Initialize(dir, configo.WithDeploymentFromEnv("APP_ENV"))
	if err != nil {
		panic(err)
	}

	fmt.Println(configo.MustGetString("root.prop1"))
	fmt.Println(configo.MustGetInt("root.prop2"))
	fmt.Println(configo.MustGetBool("root.prop3"))
	fmt.Println(configo.MustGetInt("root.prop4.nestedProp1"))

}
Output:

foo
200
false
4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(path string) (interface{}, error)

Get returns the value at the given path as an interface from the globalConfig

func GetBool

func GetBool(path string) (bool, error)

GetBool returns the value at the given path as a boolean from the globalConfig

func GetDuration

func GetDuration(path string) (time.Duration, error)

GetDuration returns the value at the given path as a duration from the globalConfig

func GetFloat64

func GetFloat64(path string) (float64, error)

GetFloat64 returns the value at the given path as a float64 from the globalConfig

func GetInt

func GetInt(path string) (int, error)

GetInt returns the value at the given path as a int from the globalConfig

func GetInt32

func GetInt32(path string) (int32, error)

GetInt32 returns the value at the given path as a int32 from the globalConfig

func GetInt64

func GetInt64(path string) (int64, error)

GetInt64 returns the value at the given path as a int64 from the globalConfig

func GetIntSlice

func GetIntSlice(path string) ([]int, error)

GetIntSlice returns the value at the given path as a slice of int values from the globalConfig

func GetString

func GetString(path string) (string, error)

GetString returns the value at the given path as a string from the globalConfig

func GetStringMap

func GetStringMap(path string) (map[string]interface{}, error)

GetStringMap returns the value at the given path as a map with string keys from the globalConfig and values as interfaces

func GetStringSlice

func GetStringSlice(path string) ([]string, error)

GetStringSlice returns the value at the given path as a slice of string values from the globalConfig

func GetTime

func GetTime(path string) (time.Time, error)

GetTime returns the value at the given path as time

func GetUint

func GetUint(path string) (uint, error)

GetUint returns the value at the given path as a uint from the globalConfig

func GetUint32

func GetUint32(path string) (uint32, error)

GetUint32 returns the value at the given path as a uint32 from the globalConfig

func GetUint64

func GetUint64(path string) (uint64, error)

GetUint64 returns the value at the given path as a uint64 from the globalConfig

func Initialize

func Initialize(dir fs.FS, opts ...ConfigOption) (err error)

Initialize loads the global configuration

func MustGet

func MustGet(path string) interface{}

MustGet is the same as `Get` except it panics in case of an error

func MustGetBool

func MustGetBool(path string) bool

MustGetBool is the same as `GetBool` except it panics in case of an error

func MustGetDuration

func MustGetDuration(path string) time.Duration

MustGetDuration is the same as `GetDuration` except it panics in case of an error

func MustGetFloat64

func MustGetFloat64(path string) float64

MustGetFloat64 is the same as `GetFloat64` except it panics in case of an error

func MustGetInt

func MustGetInt(path string) int

MustGetInt is the same as `GetInt` except it panics in case of an error

func MustGetInt32

func MustGetInt32(path string) int32

MustGetInt32 is the same as `GetInt32` except it panics in case of an error

func MustGetInt64

func MustGetInt64(path string) int64

MustGetInt64 is the same as `GetInt64` except it panics in case of an error

func MustGetIntSlice

func MustGetIntSlice(path string) []int

MustGetIntSlice is the same as `GetIntSlice` except it panics in case of an error

func MustGetString

func MustGetString(path string) string

MustGetString is the same as `GetString` except it panics in case of an error

func MustGetStringMap

func MustGetStringMap(path string) map[string]interface{}

MustGetStringMap is the same as `GetStringMap` except it panics in case of an error

func MustGetStringSlice

func MustGetStringSlice(path string) []string

MustGetStringSlice is the same as `GetStringSlice` except it panics in case of an error

func MustGetTime

func MustGetTime(path string) time.Time

MustGetTime is the same as `GetTime` except it panics in case of an error

func MustGetUint

func MustGetUint(path string) uint

MustGetUint is the same as `GetUint` except it panics in case of an error

func MustGetUint32

func MustGetUint32(path string) uint32

MustGetUint32 is the same as `GetUint32` except it panics in case of an error

func MustGetUint64

func MustGetUint64(path string) uint64

MustGetUint64 is the same as `GetUint64` except it panics in case of an error

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config is a hierarchical loader and access point for configurations. It allows loading configurations from mutiple files while being cognizant of the einvironment.

Files are loading in the following order:

default.EXT
default-{instance}.EXT
{deployment}.EXT
{deployment}-{instance}.EXT
{short_hostname}.EXT
{short_hostname}-{instance}.EXT
{short_hostname}-{deployment}.EXT
{short_hostname}-{deployment}-{instance}.EXT
{full_hostname}.EXT
{full_hostname}-{instance}.EXT
{full_hostname}-{deployment}.EXT
{full_hostname}-{deployment}-{instance}.EXT
local.EXT
local-{instance}.EXT
local-{deployment}.EXT
local-{deployment}-{instance}.EXT

EXT can be: `yaml`, `yml`, `json`, `json5`, `hjson`, `toml`

deployment defines your current environment i.e dev, test, prod etc (defaults to "dev")

instance can be the node ID in a multi-node deployment (defaults to "")

shortHostname is the hostname till the first `.` (derived from `os.Hostname()` by default)

fullHostname is the full host name (defaults to `os.Hostname()`)

Each file overrides configurations from the file above. There is a special file called `env.EXT` which allows overriding configurations using environment variables

Example
package main

import (
	"fmt"
	"os"
	"testing/fstest"

	"github.com/affanshahid/configo"
)

func main() {
	dir := fstest.MapFS{
		"default.yml": {
			Data: []byte(`
                root:
                  prop1: foo
                  prop2: 100
                  prop3: false
                  prop4:
                    nestedProp1: 4
            `),
		},
		"production.yml": {
			Data: []byte(`
                root:
                  prop2: 200
            `),
		},
	}

	os.Setenv("APP_ENV", "production")

	config, err := configo.NewConfig(dir, configo.WithDeploymentFromEnv("APP_ENV"))
	if err != nil {
		panic(err)
	}

	err = config.Initialize()
	if err != nil {
		panic(err)
	}

	fmt.Println(config.MustGetString("root.prop1"))
	fmt.Println(config.MustGetInt("root.prop2"))
	fmt.Println(config.MustGetBool("root.prop3"))
	fmt.Println(config.MustGetInt("root.prop4.nestedProp1"))

}
Output:

foo
200
false
4

func NewConfig

func NewConfig(dir fs.FS, opts ...ConfigOption) (*Config, error)

NewConfig creates a new Config dir is a FS of the directory containing the config files opts are functional options

func (*Config) Get

func (c *Config) Get(path string) (interface{}, error)

Get returns the value at the given path as an interface

func (*Config) GetBool

func (c *Config) GetBool(path string) (bool, error)

GetBool returns the value at the given path as a boolean

func (*Config) GetDuration

func (c *Config) GetDuration(path string) (time.Duration, error)

GetDuration returns the value at the given path as a duration

func (*Config) GetFloat64

func (c *Config) GetFloat64(path string) (float64, error)

GetFloat64 returns the value at the given path as a float64

func (*Config) GetInt

func (c *Config) GetInt(path string) (int, error)

GetInt returns the value at the given path as a int

func (*Config) GetInt32

func (c *Config) GetInt32(path string) (int32, error)

GetInt32 returns the value at the given path as a int32

func (*Config) GetInt64

func (c *Config) GetInt64(path string) (int64, error)

GetInt64 returns the value at the given path as a int64

func (*Config) GetIntSlice

func (c *Config) GetIntSlice(path string) ([]int, error)

GetIntSlice returns the value at the given path as a slice of int values

func (*Config) GetString

func (c *Config) GetString(path string) (string, error)

GetString returns the value at the given path as a string

func (*Config) GetStringMap

func (c *Config) GetStringMap(path string) (map[string]interface{}, error)

GetStringMap returns the value at the given path as a map with string keys and values as interfaces

func (*Config) GetStringSlice

func (c *Config) GetStringSlice(path string) ([]string, error)

GetStringSlice returns the value at the given path as a slice of string values

func (*Config) GetTime

func (c *Config) GetTime(path string) (time.Time, error)

GetTime returns the value at the given path as time

func (*Config) GetUint

func (c *Config) GetUint(path string) (uint, error)

GetUint returns the value at the given path as a uint

func (*Config) GetUint32

func (c *Config) GetUint32(path string) (uint32, error)

GetUint32 returns the value at the given path as a uint32

func (*Config) GetUint64

func (c *Config) GetUint64(path string) (uint64, error)

GetUint64 returns the value at the given path as a uint64

func (*Config) Initialize

func (c *Config) Initialize() error

Initialize initializes and loads in the configurations This must be called before attempting to get values

func (*Config) MustGet

func (c *Config) MustGet(path string) interface{}

MustGet is the same as `Get` except it panics in case of an error

func (*Config) MustGetBool

func (c *Config) MustGetBool(path string) bool

MustGetBool is the same as `GetBool` except it panics in case of an error

func (*Config) MustGetDuration

func (c *Config) MustGetDuration(path string) time.Duration

MustGetDuration is the same as `GetDuration` except it panics in case of an error

func (*Config) MustGetFloat64

func (c *Config) MustGetFloat64(path string) float64

MustGetFloat64 is the same as `GetFloat64` except it panics in case of an error

func (*Config) MustGetInt

func (c *Config) MustGetInt(path string) int

MustGetInt is the same as `GetInt` except it panics in case of an error

func (*Config) MustGetInt32

func (c *Config) MustGetInt32(path string) int32

MustGetInt32 is the same as `GetInt32` except it panics in case of an error

func (*Config) MustGetInt64

func (c *Config) MustGetInt64(path string) int64

MustGetInt64 is the same as `GetInt64` except it panics in case of an error

func (*Config) MustGetIntSlice

func (c *Config) MustGetIntSlice(path string) []int

MustGetIntSlice is the same as `GetIntSlice` except it panics in case of an error

func (*Config) MustGetString

func (c *Config) MustGetString(path string) string

MustGetString is the same as `GetString` except it panics in case of an error

func (*Config) MustGetStringMap

func (c *Config) MustGetStringMap(path string) map[string]interface{}

MustGetStringMap is the same as `GetStringMap` except it panics in case of an error

func (*Config) MustGetStringSlice

func (c *Config) MustGetStringSlice(path string) []string

MustGetStringSlice is the same as `GetStringSlice` except it panics in case of an error

func (*Config) MustGetTime

func (c *Config) MustGetTime(path string) time.Time

MustGetTime is the same as `GetTime` except it panics in case of an error

func (*Config) MustGetUint

func (c *Config) MustGetUint(path string) uint

MustGetUint is the same as `GetUint` except it panics in case of an error

func (*Config) MustGetUint32

func (c *Config) MustGetUint32(path string) uint32

MustGetUint32 is the same as `GetUint32` except it panics in case of an error

func (*Config) MustGetUint64

func (c *Config) MustGetUint64(path string) uint64

MustGetUint64 is the same as `GetUint64` except it panics in case of an error

type ConfigOption

type ConfigOption func(*Config)

ConfigOption is a functional option to configure a Config instance

func WithDeployment

func WithDeployment(deployment string) ConfigOption

WithDeployment sets the given deployment

func WithDeploymentFromEnv

func WithDeploymentFromEnv(env string) ConfigOption

WithDeploymentFromEnv loads the deployment label from the given environment variable

func WithHostname

func WithHostname(hostname string) ConfigOption

WithHostname uses the given string to set shortHostname and fullHostname

func WithHostnameFromEnv

func WithHostnameFromEnv(env string) ConfigOption

WithHostnameFromEnv loads the hostname from the given environment variable

func WithInstance

func WithInstance(instance string) ConfigOption

WithInstance sets the given instance

func WithInstanceFromEnv

func WithInstanceFromEnv(env string) ConfigOption

WithInstanceFromEnv loads the instance id from the given environment variable

type Provider

type Provider interface {
	Parse(in []byte) (map[string]interface{}, error)
}

Provider is an interface for parsing a map from binary data

type ProviderFunc

type ProviderFunc func(in []byte) (map[string]interface{}, error)

func (ProviderFunc) Parse

func (p ProviderFunc) Parse(in []byte) (map[string]interface{}, error)

Jump to

Keyboard shortcuts

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