envconf

package
v0.0.0-...-d0cd436 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package envconf allows for loading configuration from environment variables.

While multiple packages offer similar functionalities (e.g., github.com/kelseyhightower/envconfig, github.com/caarlos0/env), envconf distinguishes itself by its configuration method. Unlike tag-based approaches, envconf employs a method-based API, similar to flag.FlagSet.

A basic usage example is provided below:

    package main

    type configuration struct {
	    Port int
	    Debug bool
    }

    func main() {
	    env := envconf.LoadEnv()

	    conf := configuration{
		    Port: env.Int("PORT", 8000, "The port number used by the server."),
		    Debug: env.Bool("DEBUG", false, "Enable debug functionality.")
	    }

	    if envconf.HasHelp(os.Args[1:]) {
		    env.WriteHelp(os.Stdout)
		    os.Exit(0)
	    }

	    // ...
    }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasHelp

func HasHelp(args []string) bool

HasHelp returns true the argument list represents a help command or contains a help flag.

Types

type Configuration

type Configuration struct {
	// OnFali callback function, if set, is called when a configuration
	// cannot be parsed.
	// If not set, default behaviour is to write the error to stderr and
	// exit the current process with code 2.
	OnFail func(err error)
	// contains filtered or unexported fields
}

func Load

func Load(environment []string) *Configuration

Load returns a new Configuration instance, populated from the given list of "NAME=VALUE" serialized pairs.

func LoadEnv

func LoadEnv() *Configuration

LoadEnv returns a new Configuration instance, populated from the environment variables.

func (*Configuration) Bool

func (c *Configuration) Bool(name string, fallback bool, description string) bool

Bool returns the boolean value represented by the variable with given name. If the variable is not specified, fallback value is returned.

func (*Configuration) Dur

func (c *Configuration) Dur(name string, fallback time.Duration, description string) time.Duration

Dur returns a time duration represented by the variable with given name. If the variable is not specified, fallback value is returned.

func (*Configuration) Int

func (c *Configuration) Int(name string, fallback int, description string) int

Int returns an integer value of a variable with given name. If the variable is not specified, fallback value is returned.

func (*Configuration) Ints

func (c *Configuration) Ints(name string, separator string, fallback []int, description string) []int

Ints returns a list of integer values represented by a variable with given name. If the variable is not specified, fallback value is returned.

func (*Configuration) Str

func (c *Configuration) Str(name string, fallback string, description string) string

Str returns the string representation of a variable.

func (*Configuration) WriteHelp

func (c *Configuration) WriteHelp(out io.Writer)

WriteHelp writers a tabular representation of all requested environment variable names, their corresponding default values and description.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳