config

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2017 License: MIT Imports: 13 Imported by: 0

README

Config Build Status GoDoc Go Report Card

Go package for dealing with configuration files, has built in support for environment variables and JSON files and support for YAML and Toml via plugins.

Installation

go get github.com/frozzare/go-config

Example

package main

import (
	"io"
	"net/http"

	"github.com/frozzare/go-config"

	_ "github.com/frozzare/go-config/plugins/yaml"
)

func main() {
	// Use read and watch file + env as middlewares.
	config.Use(config.NewFromFile("config.yml", true))
	config.Use(config.NewEnv())

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		v, _ := config.String("name")
		io.WriteString(w, v)
	})

	http.ListenAndServe(":8899", nil)
}

License

MIT © Fredrik Forsmo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoValueFound it's the error when no value is found.
	ErrNoValueFound = errors.New("Value not found for the given key")
)

Functions

func Bool

func Bool(key string, def ...interface{}) (bool, error)

Bool returns a bool from the config file.

func Float

func Float(key string, def ...interface{}) (float64, error)

Float returns a float64 from the config file.

func Get

func Get(key string, def ...interface{}) (interface{}, error)

Get returns the value for the given key from the config file as a interface.

func Int

func Int(key string, def ...interface{}) (int64, error)

Int returns a int64 from the config file.

func List

func List(key string, def ...interface{}) ([]string, error)

List returns a slice of strings from the config file.

func MustBool

func MustBool(key string, def ...interface{}) bool

MustBool returns a bool from the config file, it will panic if a error is created.

func MustFloat

func MustFloat(key string, def ...interface{}) float64

MustFloat returns a float64 from the config file, it will panic if a error is created.

func MustGet added in v1.1.0

func MustGet(key string, def ...interface{}) interface{}

MustGet returns a interface from the config file, it will panic if a error is created.

func MustInt

func MustInt(key string, def ...interface{}) int64

MustInt returns a int64 from the config file, it will panic if a error is created.

func MustList

func MustList(key string, def ...interface{}) []string

MustList returns a slice of strings from the config file, it will panic if a error is created.

func MustString

func MustString(key string, def ...interface{}) string

MustString returns a string from the config file, it will panic if a error is created.

func MustUint

func MustUint(key string, def ...interface{}) uint64

MustUint returns a unsigned int64 from the config file, it will panic if a error is created.

func ReadAndWatchFile

func ReadAndWatchFile(path string)

ReadAndWatchFile reads and watches file for changes and reload the configuration file.

func RegisterFileType

func RegisterFileType(ext string, callback fileCallback)

RegisterFileType register a file type with a callback.

func Reset

func Reset()

Reset will reset the config instance.

func String

func String(key string, def ...interface{}) (string, error)

String returns a string from the config file.

func Uint

func Uint(key string, def ...interface{}) (uint64, error)

Uint returns a unsigned int64 from the config file.

func Use

func Use(middleware ...Middleware)

Use adds a middleware to the stack list.

func WatchFile

func WatchFile(path string)

WatchFile watches file for changes and reload the configuration file.

Types

type Config

type Config struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Config is the struct for the config.

func (*Config) Data

func (c *Config) Data() map[interface{}]interface{}

Data returns all config values.

func (*Config) Get

func (c *Config) Get(key interface{}) interface{}

Get returns a config value by key or nil.

func (*Config) Middlewares

func (c *Config) Middlewares() []Middleware

Middlewares returns a existing middlewares.

func (*Config) Set

func (c *Config) Set(key interface{}, value interface{})

Set will set a config value by key.

type Env

type Env struct {
}

Env middleware struct that handles environment variables.

func NewEnv

func NewEnv() *Env

NewEnv creates a new environment middleware.

func (*Env) Bool

func (s *Env) Bool(key string) (bool, error)

Bool returns a bool or a error.

func (*Env) Float

func (s *Env) Float(key string) (float64, error)

Float returns a float64 or a error.

func (*Env) Get

func (s *Env) Get(key string) (interface{}, error)

Get returns a interface or a error.

func (*Env) ID

func (s *Env) ID() string

ID returns the values struct identifier.

func (*Env) Int

func (s *Env) Int(key string) (int64, error)

Int returns a int or a error.

func (*Env) List

func (s *Env) List(key string) ([]string, error)

List returns a slice of strings or a error.

func (*Env) Setup

func (s *Env) Setup() error

Setup returns a error if the middleware setup is failing.

func (*Env) String

func (s *Env) String(key string) (string, error)

String returns a string or a error.

func (*Env) Uint

func (s *Env) Uint(key string) (uint64, error)

Uint returns a unsigned int or a error.

type File

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

File struct is a config file.

func (*File) Bool

func (s *File) Bool(key string) (bool, error)

Bool returns a bool or a error.

func (*File) Float

func (s *File) Float(key string) (float64, error)

Float returns a float64 or a error.

func (*File) Get

func (s *File) Get(key string) (interface{}, error)

Get returns a interface or a error.

func (*File) ID

func (s *File) ID() string

ID returns the values struct identifier.

func (*File) Int

func (s *File) Int(key string) (int64, error)

Int returns a int or a error.

func (*File) List

func (s *File) List(key string) ([]string, error)

List returns a slice of strings or a error.

func (*File) Setup

func (s *File) Setup() error

Setup returns a error if the middleware setup is failing.

func (*File) String

func (s *File) String(key string) (string, error)

String returns a string or a error.

func (*File) Uint

func (s *File) Uint(key string) (uint64, error)

Uint returns a unsigned int or a error.

type Middleware

type Middleware interface {
	ID() string
	Bool(name string) (bool, error)
	Float(name string) (float64, error)
	Int(name string) (int64, error)
	Get(name string) (interface{}, error)
	List(name string) ([]string, error)
	Setup() error
	String(name string) (string, error)
	Uint(name string) (uint64, error)
}

Middleware is the interface that external middlewares must implement.

func NewFromBytes

func NewFromBytes(typ string, body []byte) Middleware

NewFromBytes creates a new middleware from bytes as the given type, e.g: json.

func NewFromFile

func NewFromFile(path string, watch ...bool) Middleware

NewFromFile creates a new middleware from file. Optional bool argument to watch file.

type Values

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

Values middleware struct that handles predefined values.

func NewFromValues

func NewFromValues(values map[string]interface{}) *Values

NewFromValues creates a new values middleware.

func (*Values) Bool

func (s *Values) Bool(key string) (bool, error)

Bool returns a bool or a error.

func (*Values) Float

func (s *Values) Float(key string) (float64, error)

Float returns a float64 or a error.

func (*Values) Get

func (s *Values) Get(key string) (interface{}, error)

Get returns a interface or a error.

func (*Values) ID

func (s *Values) ID() string

ID returns the values struct identifier.

func (*Values) Int

func (s *Values) Int(key string) (int64, error)

Int returns a int or a error.

func (*Values) List

func (s *Values) List(key string) ([]string, error)

List returns a slice of strings or a error.

func (*Values) Setup

func (s *Values) Setup() error

Setup returns a error if the middleware setup is failing.

func (*Values) String

func (s *Values) String(key string) (string, error)

String returns a string or a error.

func (*Values) Uint

func (s *Values) Uint(key string) (uint64, error)

Uint returns a unsigned int or a error.

Directories

Path Synopsis
plugins

Jump to

Keyboard shortcuts

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