config

package
v0.0.0-...-0cc4b45 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Env

func Env(key string, val *string)

Env loads string value from environment variable if it exists

func EnvBool

func EnvBool(key string, val *bool)

EnvBool loads bool value from environment variable if it exists

func EnvFile

func EnvFile(fn string) (string, error)

EnvFile returns full path to .env file if it exists in the current working directory. If file doesn't exists, empty string returned

suffix is used to append to the .env file, for example for "dev" suffix, ".env.dev" will be looked up

func EnvInt

func EnvInt(key string, val *int)

EnvInt loads int value from environment variable if it exists

func EnvInt64

func EnvInt64(key string, val *int64)

EnvInt64 loads Int64 value from environment variable if it exists

func EnvList

func EnvList(key string, val *[]string)

EnvList loads a comma-separated values from environment variable if it exists

func IsValid

func IsValid(cfg any) error

IsValid validates Config. If any of the fields in the config struct implement Section interface (i.e. have IsValid() func) it is called.

Error returned if any of the IsValid returns false

func LoadEnv

func LoadEnv(envFile string) error

LoadEnv loads .env file from the current working directory if it exists envFile is just an ".env" or ".env.dev" without full file path

To load using full file path, use LoadEnvFile

func LoadEnvFile

func LoadEnvFile(fn string) error

LoadEnvFile reads all strings from fn file and loads it into the environment The format of the string is key=value

func LoadOverrides

func LoadOverrides(cfg any) error

LoadOverrides loads data into struct from environment variables.

Names of environment vars are specified using "env" structure tag.

The following patterns are supported. Example:

type Config struct {

	// Standalone field with a tag
	Var1 string `env:"SOME_VAR"`

	// pointer to basic types
	Var1 string `env:"SOME_VAR"`

 	// Nested structure without tag
	Section ConfigSection

	// Nested structure with a tag, which will be used as a prefix
	// with underscore for any tags in the nested structure.
	//
	// Fore example in this case "SERVICE_" will be prefixed
	// for any var name specified in the ConfigSection structue
	Section2 ConfigSection `env:"SERVICE"`
}

func LoadToml

func LoadToml(cfg any, fn string) error

Types

type FieldError

type FieldError struct {
	FieldName string
	Message   string
}

func (FieldError) Error

func (e FieldError) Error() string

type Section

type Section interface {
	IsValid() error
}

Section defines interface used by configuration sections

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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