Help us shape the future of Go! Take the 2026 Go Developer Survey

dcfg

package module
v0.0.0-...-e1d76b3 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2017 License: MIT Imports: 9 Imported by: 0

README

GoDoc Go Report Card Build Status

dcfg

dcfg retrieves configuration values for twelve-factor apps. In addition to values from the environment, this package can also retrieve values from docker secret files and set default values.

Installation

go get github.com/sprungknoedl/dcfg

Usage

package main

import "github.com/sprungknoedl/dcfg"

func main() {
    // optional: set defaults
    dcfg.Defaults.Set("HOST", "localhost")
    dcfg.Defaults.Set("PORT", "8080")

    // ignore errors for example, don't do this in production ;)
    host, _ := dcfg.GetString("HOST")
    port, _ := dcfg.GetInt("PORT")
}

Documentation

Overview

Package dcfg retrieves configuration values for twelve-factor apps. In addition to values from the environment, this package can also retrieve values from docker secret files and set default values.

Index

Constants

This section is empty.

Variables

View Source
var Defaults = NewMemoryProvider()

Defaults is a global instance of an MemoryProvider that can be used to provide default values when last in the Providers list.

View Source
var ErrKeyMissing = errors.New("key not set")

ErrKeyMissing indicated that for the given key no value could be retrieved.

Providers is asked in order for values until the first provider produces one.

Functions

func FatalBool

func FatalBool(key string) bool

FatalBool is a wrapper arount GetBool that fails with log.Fatal on error.

func FatalFloat

func FatalFloat(key string) float64

FatalFloat is a wrapper arount GetFloat that fails with log.Fatal on error.

func FatalInt

func FatalInt(key string) int64

FatalInt is a wrapper arount GetInt that fails with log.Fatal on error.

func FatalString

func FatalString(key string) string

FatalString is a wrapper arount GetString that fails with log.Fatal on error.

func GetBool

func GetBool(key string) (bool, error)

GetBool returns the value associated with the key as a boolean.

func GetFloat

func GetFloat(key string) (float64, error)

GetFloat returns the value associated with the key as a float.

func GetInt

func GetInt(key string) (int64, error)

GetInt returns the value associated with the key as an integer.

func GetString

func GetString(key string) (string, error)

GetString returns the value associated with the key as a string.

func MustBool

func MustBool(key string) bool

MustBool is a wrapper arount GetBool that fails with panic on error.

func MustFloat

func MustFloat(key string) float64

MustFloat is a wrapper arount GetFloat that fails with panic on error.

func MustInt

func MustInt(key string) int64

MustInt is a wrapper arount GetInt that fails with panic on error.

func MustString

func MustString(key string) string

MustString is a wrapper arount GetString that fails with panic on error.

Types

type DockerSecretsProvider

type DockerSecretsProvider struct {
	SecretsDir string
}

DockerSecretsProvider provides values from files populated by the docker secrets mechanism.

func NewDockerSecretsProvider

func NewDockerSecretsProvider() *DockerSecretsProvider

NewDockerSecretsProvider initializes a new DockerSecretsProvider instance

func (DockerSecretsProvider) Get

func (p DockerSecretsProvider) Get(key string) (string, error)

Get retrieves a value from a docker secrets file. If no file for the given key exists, ErrKeyMissing is returned.

type EnvProvider

type EnvProvider struct{}

EnvProvider provides values from the environment.

func NewEnvProvider

func NewEnvProvider() *EnvProvider

NewEnvProvider initializes a new EnvProvider instance

func (*EnvProvider) Get

func (p *EnvProvider) Get(key string) (string, error)

Get retrieves a value from environment variables. If no environment variable is set or it is empty, ErrKeyMissing is returned

type MemoryProvider

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

MemoryProvider provides values from an memory store.

func NewMemoryProvider

func NewMemoryProvider() *MemoryProvider

NewMemoryProvider initializes a new DefaultsProvider instance

func (MemoryProvider) Get

func (p MemoryProvider) Get(key string) (string, error)

Get returns a previously set default value for the given key or ErrKeyMissing when no default is set.

func (*MemoryProvider) Set

func (p *MemoryProvider) Set(key string, value string)

Set stores a value for the given key that the provider returns when queried.

type Provider

type Provider interface {
	Get(key string) (string, error)
}

Provider is used to retrieve a value for a given key. If the provider has no value for a key, it should return ErrMissingKey. If the provider fails to retrieve a key, it can return an error, which will stop the retrieval and returns the error to the caller.

Jump to

Keyboard shortcuts

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