config

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package config provides a config manager for any type that implements the config.Interface.

Example

This example demonstrate the basics of the config interface. For more details check the documentation.

package main

import (
	"github.com/patrickascher/gofw/config/json"

	"github.com/patrickascher/gofw/config"
)

func main() {
	// import the provider package
	// import _ "github.com/patrickascher/gofw/config/json"

	// Cfg is the configuration struct which should get marshaled.
	type Cfg struct {
		Database string
		Port     int
	}

	// define the config variable
	cfg := Cfg{}

	// SetEnv can be used to set a environment variable. By default the os.Env("ENV") will be used.
	config.SetEnv("staging")

	// New calls the given config provider and passes the struct, env and options.
	// cfg must be a pointer, otherwise the value can not be set.
	err := config.New(config.JSON, &cfg, json.Options{Filepath: "config/app.json"})
	if err != nil {
		return
	}

	// The value of the cfg variable will be set.
	// Cfg{Database:"127.0.0.1",Port:3306}
}
Output:

Index

Examples

Constants

View Source
const (
	// JSON pre-defined config provider.
	JSON = "json"
	// ENV is the default name to check the system environment variable os.GetEnv().
	ENV = "ENV"
)
View Source
const (
	CallbackBeforeParse = "BeforeParse"
	CallbackAfterParse  = "AfterParse"
	CallbackBeforeValid = "BeforeValid"
	CallbackAfterValid  = "AfterValid"
)

Variables

View Source
var (
	ErrNoProvider            = errors.New("config: empty config-name or config-provider is nil")
	ErrUnknownProvider       = errors.New("config: unknown config-provider %q")
	ErrProviderAlreadyExists = errors.New("config: config-provider %#v is already registered")
	ErrConfigPtr             = errors.New("config: struct must be a ptr")
)

Error messages.

Functions

func Env

func Env() string

Env returns the actual environment variable.

func New

func New(provider string, config interface{}, options interface{}) error

New will call the parse function on the given provider. The config must be a ptr to the given struct. Options and environment are passed through to the provider. For more information check the provider documentation. If no specific environment was set by SetEnv() before, the os.Env("ENV") will be used. If the provider is not registered, the parsing fails or the cfg kind is not ptr, an error will return. By default validate can be used on the struct to ensure all mandatory data is set. Callbacks BeforeParse, BeforeValid, AfterValid, AfterParse can be used.

func Register

func Register(provider string, fn provider) error

Register the config provider. This should be called in the init() of the providers. If the config provider/name is empty or is already registered, an error will return.

func SetEnv

func SetEnv(e string)

SetEnv allows a custom environment variable. This must be set before New() is called.

Types

type Interface

type Interface interface {
	// Parse the given ptr struct.
	Parse(config interface{}, env string, options interface{}) error
}

Interface is used by config providers.

Directories

Path Synopsis
Package json implements the config.Interface and registers a json provider.
Package json implements the config.Interface and registers a json provider.

Jump to

Keyboard shortcuts

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