cfg

package
v0.0.0-...-15608e1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

cfg

import "github.com/ardanlabs/kit/cfg"

Package cfg provides configuration options that are loaded from the environment. Configuration is then stored in memory and can be retrieved by its proper type.

To initialize the configuration system from your environment, call Init:

cfg.Init(cfg.EnvProvider{Namespace: "configKey"})

To retrieve values from configuration:

proc, err := cfg.String("proc_id")
port, err := cfg.Int("port")
ms, err := cfg.Time("stamp")

Use the Must set of function to retrieve a single value but these calls will panic if the key does not exist:

proc := cfg.MustString("proc_id")
port := cfg.MustInt("port")
ms := cfg.MustTime("stamp")

func Bool

func Bool(key string) (bool, error)

Bool calls the default Config and returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.

func Init

func Init(p Provider) error

Init populates the package's default Config and should be called only once. A Provider must be supplied which will return a map of key/value pairs to be loaded.

func Int

func Int(key string) (int, error)

Int calls the Default config and returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.

func Log

func Log() string

Log returns a string to help with logging the package's default Config. It excludes any values whose key contains the string "PASS".

func MustBool

func MustBool(key string) bool

MustBool calls the default Config and returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.

func MustInt

func MustInt(key string) int

MustInt calls the default Config and returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.

func MustString

func MustString(key string) string

MustString calls the default Config and returns the value of the given key as a string, else it will panic if the key was not found.

func MustTime

func MustTime(key string) time.Time

MustTime calls the default Config ang returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.

func MustURL

func MustURL(key string) *url.URL

MustURL calls the default Config and returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.

func SetBool

func SetBool(key string, value bool)

SetBool adds or modifies the default Config for the specified key and value.

func SetInt

func SetInt(key string, value int)

SetInt adds or modifies the default Config for the specified key and value.

func SetString

func SetString(key string, value string)

SetString adds or modifies the default Config for the specified key and value.

func SetTime

func SetTime(key string, value time.Time)

SetTime adds or modifies the default Config for the specified key and value.

func SetURL

func SetURL(key string, value *url.URL)

SetURL adds or modifies the default Config for the specified key and value.

func String

func String(key string) (string, error)

String calls the default Config and returns the value of the given key as a string. It will return an error if key was not found.

func Time

func Time(key string) (time.Time, error)

Time calls the default Config and returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.

func URL

func URL(key string) (*url.URL, error)

URL calls the default Config and returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.

type Config

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

Config is a goroutine safe configuration store, with a map of values set from a config Provider.

func New
func New(p Provider) (*Config, error)

New populates a new Config from a Provider. It will return an error if there was any problem reading from the Provider.

func (*Config) Bool
func (c *Config) Bool(key string) (bool, error)

Bool returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.

func (*Config) Int
func (c *Config) Int(key string) (int, error)

Int returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.

func (*Config) Log
func (c *Config) Log() string

Log returns a string to help with logging your configuration. It excludes any values whose key contains the string "PASS".

func (*Config) MustBool
func (c *Config) MustBool(key string) bool

MustBool returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.

func (*Config) MustInt
func (c *Config) MustInt(key string) int

MustInt returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.

func (*Config) MustString
func (c *Config) MustString(key string) string

MustString returns the value of the given key as a string. It will panic if the key was not found.

func (*Config) MustTime
func (c *Config) MustTime(key string) time.Time

MustTime returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.

func (*Config) MustURL
func (c *Config) MustURL(key string) *url.URL

MustURL returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.

func (*Config) SetBool
func (c *Config) SetBool(key string, value bool)

SetBool adds or modifies the configuration for the specified key and value.

func (*Config) SetInt
func (c *Config) SetInt(key string, value int)

SetInt adds or modifies the configuration for the specified key and value.

func (*Config) SetString
func (c *Config) SetString(key string, value string)

SetString adds or modifies the configuration for the specified key and value.

func (*Config) SetTime
func (c *Config) SetTime(key string, value time.Time)

SetTime adds or modifies the configuration for the specified key and value.

func (*Config) SetURL
func (c *Config) SetURL(key string, value *url.URL)

SetURL adds or modifies the configuration for the specified key and value.

func (*Config) String
func (c *Config) String(key string) (string, error)

String returns the value of the given key as a string. It will return an error if key was not found.

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

Time returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.

func (*Config) URL
func (c *Config) URL(key string) (*url.URL, error)

URL returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.

type EnvProvider

type EnvProvider struct {
    Namespace string
}

EnvProvider provides configuration from the environment. All keys will be made uppercase.

func (EnvProvider) Provide
func (ep EnvProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type FileProvider

type FileProvider struct {
    Filename string
}

FileProvider describes a file based loader which loads the configuration from a file listed.

func (FileProvider) Provide
func (fp FileProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type MapProvider

type MapProvider struct {
    Map map[string]string
}

MapProvider provides a simple implementation of the Provider whereby it just returns a stored map.

func (MapProvider) Provide
func (mp MapProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type Provider

type Provider interface {
    Provide() (map[string]string, error)
}

Provider is implemented by the user to provide the configuration as a map. There are currently two Providers implemented, EnvProvider and MapProvider.


Generated by godoc2md

Documentation

Overview

Package cfg provides configuration options that are loaded from the environment. Configuration is then stored in memory and can be retrieved by its proper type.

To initialize the configuration system from your environment, call Init:

cfg.Init(cfg.EnvProvider{Namespace: "configKey"})

To retrieve values from configuration:

proc, err := cfg.String("proc_id")
port, err := cfg.Int("port")
ms, err := cfg.Time("stamp")

Use the Must set of function to retrieve a single value but these calls will panic if the key does not exist:

proc := cfg.MustString("proc_id")
port := cfg.MustInt("port")
ms := cfg.MustTime("stamp")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string) (bool, error)

Bool calls the default Config and returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.

func Init

func Init(p Provider) error

Init populates the package's default Config and should be called only once. A Provider must be supplied which will return a map of key/value pairs to be loaded.

func Int

func Int(key string) (int, error)

Int calls the Default config and returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.

func Log

func Log() string

Log returns a string to help with logging the package's default Config. It excludes any values whose key contains the string "PASS".

func MustBool

func MustBool(key string) bool

MustBool calls the default Config and returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.

func MustInt

func MustInt(key string) int

MustInt calls the default Config and returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.

func MustString

func MustString(key string) string

MustString calls the default Config and returns the value of the given key as a string, else it will panic if the key was not found.

func MustTime

func MustTime(key string) time.Time

MustTime calls the default Config ang returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.

func MustURL

func MustURL(key string) *url.URL

MustURL calls the default Config and returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.

func SetBool

func SetBool(key string, value bool)

SetBool adds or modifies the default Config for the specified key and value.

func SetInt

func SetInt(key string, value int)

SetInt adds or modifies the default Config for the specified key and value.

func SetString

func SetString(key string, value string)

SetString adds or modifies the default Config for the specified key and value.

func SetTime

func SetTime(key string, value time.Time)

SetTime adds or modifies the default Config for the specified key and value.

func SetURL

func SetURL(key string, value *url.URL)

SetURL adds or modifies the default Config for the specified key and value.

func String

func String(key string) (string, error)

String calls the default Config and returns the value of the given key as a string. It will return an error if key was not found.

func Time

func Time(key string) (time.Time, error)

Time calls the default Config and returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.

func URL

func URL(key string) (*url.URL, error)

URL calls the default Config and returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.

Types

type Config

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

Config is a goroutine safe configuration store, with a map of values set from a config Provider.

func New

func New(p Provider) (*Config, error)

New populates a new Config from a Provider. It will return an error if there was any problem reading from the Provider.

Example

ExampleNew shows how to create and use a new config which can be passed around.

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/ardanlabs/kit/cfg"
)

func main() {
	c, err := cfg.New(cfg.MapProvider{
		Map: map[string]string{
			"IP":   "80.23.233.10",
			"PORT": "8044",
			"INIT_STAMP": time.Date(2009, time.November,
				10, 23, 0, 0, 0, time.UTC).UTC().Format(time.UnixDate),
			"FLAG": "off",
		},
	})

	if err != nil {
		log.Fatal(err)
	}

	// To get the ip.
	fmt.Println(c.MustString("IP"))

	// To get the port number.
	fmt.Println(c.MustInt("PORT"))

	// To get the timestamp.
	fmt.Println(c.MustTime("INIT_STAMP"))

	// To get the flag.
	fmt.Println(c.MustBool("FLAG"))

}
Output:

80.23.233.10
8044
2009-11-10 23:00:00 +0000 UTC
false

func (*Config) Bool

func (c *Config) Bool(key string) (bool, error)

Bool returns the bool value of a given key as a bool. It will return an error if the key was not found or the value can't be converted to a bool.

func (*Config) Int

func (c *Config) Int(key string) (int, error)

Int returns the value of the given key as an int. It will return an error if the key was not found or the value can't be converted to an int.

func (*Config) Log

func (c *Config) Log() string

Log returns a string to help with logging your configuration. It excludes any values whose key contains the string "PASS".

func (*Config) MustBool

func (c *Config) MustBool(key string) bool

MustBool returns the bool value of a given key as a bool. It will panic if the key was not found or the value can't be converted to a bool.

func (*Config) MustInt

func (c *Config) MustInt(key string) int

MustInt returns the value of the given key as an int. It will panic if the key was not found or the value can't be converted to an int.

func (*Config) MustString

func (c *Config) MustString(key string) string

MustString returns the value of the given key as a string. It will panic if the key was not found.

func (*Config) MustTime

func (c *Config) MustTime(key string) time.Time

MustTime returns the value of the given key as a Time. It will panic if the key was not found or the value can't be converted to a Time.

func (*Config) MustURL

func (c *Config) MustURL(key string) *url.URL

MustURL returns the value of the given key as a URL. It will panic if the key was not found or the value can't be converted to a URL.

func (*Config) SetBool

func (c *Config) SetBool(key string, value bool)

SetBool adds or modifies the configuration for the specified key and value.

func (*Config) SetInt

func (c *Config) SetInt(key string, value int)

SetInt adds or modifies the configuration for the specified key and value.

func (*Config) SetString

func (c *Config) SetString(key string, value string)

SetString adds or modifies the configuration for the specified key and value.

func (*Config) SetTime

func (c *Config) SetTime(key string, value time.Time)

SetTime adds or modifies the configuration for the specified key and value.

func (*Config) SetURL

func (c *Config) SetURL(key string, value *url.URL)

SetURL adds or modifies the configuration for the specified key and value.

func (*Config) String

func (c *Config) String(key string) (string, error)

String returns the value of the given key as a string. It will return an error if key was not found.

func (*Config) Time

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

Time returns the value of the given key as a Time. It will return an error if the key was not found or the value can't be converted to a Time.

func (*Config) URL

func (c *Config) URL(key string) (*url.URL, error)

URL returns the value of the given key as a URL. It will return an error if the key was not found or the value can't be converted to a URL.

type EnvProvider

type EnvProvider struct {
	Namespace string
}

EnvProvider provides configuration from the environment. All keys will be made uppercase.

func (EnvProvider) Provide

func (ep EnvProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type FileProvider

type FileProvider struct {
	Filename string
}

FileProvider describes a file based loader which loads the configuration from a file listed.

func (FileProvider) Provide

func (fp FileProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type MapProvider

type MapProvider struct {
	Map map[string]string
}

MapProvider provides a simple implementation of the Provider whereby it just returns a stored map.

func (MapProvider) Provide

func (mp MapProvider) Provide() (map[string]string, error)

Provide implements the Provider interface.

type Provider

type Provider interface {
	Provide() (map[string]string, error)
}

Provider is implemented by the user to provide the configuration as a map. There are currently two Providers implemented, EnvProvider and MapProvider.

Jump to

Keyboard shortcuts

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