fig

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

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

Go to latest
Published: Sep 13, 2020 License: MIT Imports: 4 Imported by: 0

README

Figs

fig

Juicy config from environment in Go

Fig wraps John Barton's godotenv with caching and typed config variables. I wrote this as a subpackage for another project and extracted it to this repo for easy reuse. It's probably not production-worthy but it's a useful way to manage configs.

Initializing

Calling fig.Make() with no parameters initializes a Config object with the contents of ./.env. Pass one or more filenames to read environment variables from specific files: fig.Make("./config.env", "./example.env")

Retrieving config variables

Retrieving a value from the environment will cache it in the Config object to (minimally) speed up retrieval.

conf := fig.Make()

// first call reads from environment
confStr, _ := conf.GetString("ENV_NAME")

// subsequent calls read from cache
confStrCached, _ := conf.GetString("ENV_NAME")

fig.Config has methods for retrieving strings, ints, int64s, float64s and bools.

  • GetString
  • GetInt
  • GetInt64
  • GetBool
  • GetFloat64

These methods return an error if the variable is not configured or if the string in the environment cannot be parsed into the appropriate type. The following methods will panic on missing or malformed variables:

  • MustGetString
  • MustGetInt
  • MustGetInt64
  • MustGetBool
  • MustGetFloat64

These are useful for required configuration variables for which repeated error checks are a PITA.

For retrieving variables with hard-coded defaults, use the ...Or set of methods:

  • GetStringOr
  • GetIntOr
  • GetInt64Or
  • GetBoolOr
  • GetFloatOr

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config map[string]string

Config caches and retrieves configurations from the environment

func Make

func Make(filenames ...string) (Config, error)

Make initializes the config object

func (Config) GetBool

func (c Config) GetBool(key string) (bool, error)

GetBool retrieves the configured bool

func (Config) GetBoolOr

func (c Config) GetBoolOr(key string, defaultBool bool) bool

GetBoolOr retrieves the configured bool or the provided default

func (Config) GetFloat64

func (c Config) GetFloat64(key string) (float64, error)

GetFloat64 retrieves the configured float64

func (Config) GetFloat64Or

func (c Config) GetFloat64Or(key string, defaultFloat64 float64) float64

GetFloat64Or retrieves the configured float64 or the provided default

func (Config) GetInt

func (c Config) GetInt(key string) (int, error)

GetInt retrieves the configured int

func (Config) GetInt64

func (c Config) GetInt64(key string) (int64, error)

GetInt64 retrieves the configured int64

func (Config) GetInt64Or

func (c Config) GetInt64Or(key string, defaultInt64 int64) int64

GetInt64Or retrieves the configured int64 or the provided default

func (Config) GetIntOr

func (c Config) GetIntOr(key string, defaultInt int) int

GetIntOr retrieves the configured int or the provide ddefault

func (Config) GetString

func (c Config) GetString(key string) (string, error)

GetString retrieves the configured string

func (Config) GetStringOr

func (c Config) GetStringOr(key, defaultString string) string

GetStringOr retrieves the configured string or the provided default

func (Config) MustGetBool

func (c Config) MustGetBool(key string) bool

MustGetBool retrieves the configured bool or panics if missing or malformed

func (Config) MustGetFloat64

func (c Config) MustGetFloat64(key string) float64

MustGetFloat64 retrieves the configured float64 or panics if missing or malformed

func (Config) MustGetInt

func (c Config) MustGetInt(key string) int

MustGetInt retrieves the configured int or panics

func (Config) MustGetInt64

func (c Config) MustGetInt64(key string) int64

MustGetInt64 retrieves the configured int64 or panics if missing or malformed

func (Config) MustGetString

func (c Config) MustGetString(key string) string

MustGetString retrieves the configured string or panics if undefined

Jump to

Keyboard shortcuts

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