envconf

package module
v0.0.0-...-416ddec Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: MIT Imports: 3 Imported by: 1

README

Go Reference Build Status

Envconf

Envconf is a simple, zero dependency library for managing configuration from the environment. It adds default values, variable descriptions, and the ability to inject a environment fetching function.

Example usage:

config := envconf.New()

// Register configuration items.
config.Register(envconf.Item{
  Name:        "FOO",
  Default:     "default-foo",
  Description: "the foo",
})

config.Register(envconf.Item{
  Name:        "BAR",
  Default:     "default-bar",
  Description: "the bar",
})

foo := config.Value("FOO") // returns ENV["FOO"] or "default-foo"
bar = config.Value("BAR") // returns ENV["BAR"] or "default-bar"

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/jackc/envconf"
)

func main() {
	config := envconf.New()

	// Register configuration items.
	config.Register(envconf.Item{
		Name:        "FOO",
		Default:     "default-foo",
		Description: "the foo",
	})

	config.Register(envconf.Item{
		Name:        "BAR",
		Default:     "default-bar",
		Description: "the bar",
	})

	// You can override the source of environment variables by setting LookupEnvFunc. Defaults to os.LookupEnv.
	config.LookupEnvFunc = func(key string) (string, bool) {
		if key == "FOO" {
			return "foo from environment", true
		}
		return "", false
	}

	// You can use a config to create help output.
	fmt.Println("Configuration items:")
	for _, item := range config.Items() {
		fmt.Printf("%s: %s (default: %s)\n", item.Name, item.Description, item.Default)
	}

	fmt.Println()

	value := config.Value("FOO")
	fmt.Println("FOO", value)

	value = config.Value("BAR")
	fmt.Println("BAR", value)

}
Output:

Configuration items:
BAR: the bar (default: default-bar)
FOO: the foo (default: default-foo)

FOO foo from environment
BAR default-bar

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {

	// LookupEnvFunc is used to fetch value of an environment variable.
	LookupEnvFunc func(string) (string, bool)
	// contains filtered or unexported fields
}

Config handles access to configuration stored in the environment.

func New

func New() *Config

New creates a new Config.

func (*Config) Item

func (c *Config) Item(name string) (Item, bool)

Item returns a configuration item by name.

func (*Config) Items

func (c *Config) Items() []Item

Items returns all registered configuration items sorted by name alphabetically.

func (*Config) MustItem

func (c *Config) MustItem(name string) Item

MustItem returns a configuration item by name. It panics if the item is not found.

func (*Config) Register

func (c *Config) Register(item Item)

Register registers a configuration item.

func (*Config) Value

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

Value returns the value of an environment variable. If the environment variable is not set or is the empty string, it returns the default value.

type Item

type Item struct {
	Name        string
	Default     string
	Description string
}

Jump to

Keyboard shortcuts

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