conf

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 11 Imported by: 3

README

Conf

Configuration package used for handling environment variables

import "github.com/rsb/conf"

Usage

Make sure you have your environment variables set

export MYAPP_SOME_API_URL=localhost:5000
export MYAPP_SOME_API_TIMEOUT=5s
export MYAPP_DB_HOST=localhost
export MYAPP_DB_USER=postgres
export MYAPP_DB_NAME=my-db
export MYAPP_DB_PASSWORD=abc
export MYAPP_CODES="codeA:A,codeB:B,codeC:C"
export MYAPP_ID_LIST="id1,id2,id3"

Define your config struct and use it to initialize your app

package main

import(
	"fmt"
	"log"
	"time"
	
	"github.com/rsb/conf"
)


type AppConfig struct {
	SomeAPI
	DB
	Debug bool
  Codes map[string]string
	IDList []string
}

type SomeAPI struct {
  URL string	
	Timeout time.Duration
}

type DB struct {
	Name string
  Port int	
	User string
	Password string
  	
}

func main() {
  var config AppConfig	

	if err := conf.ProcessEnv(&config, "MYAPP"); err != nil {
		log.Fatal(err.Error())
  }
  
	fmt.Println(config.SomeAPI.URL)
	fmt.Println(config.SomeAPI.Timeout)

	...
}

Documentation

Overview

Package conf is a package that specializes in parsing out environment variables using structs with annotated tags to control how it is done.

Index

Constants

View Source
const (
	AWSLambdaFunctionNameVar = "AWS_LAMBDA_FUNCTION_NAME"
	AWSProfile               = "AWS_PROFILE"
	AWSRegion                = "AWS_REGION"
	AppName                  = "APP_NAME"
	GlobalParamStoreKey      = "global"
)

Variables

View Source
var (
	InvalidSpecFailure = failure.Config("specification must be a struct pointer")
)

Functions

func BinaryUnmarshaler

func BinaryUnmarshaler(field reflect.Value) (b encoding.BinaryUnmarshaler)

func BindCLI

func BindCLI(cmd *cobra.Command, v *viper.Viper, spec interface{}, prefix ...string) error

func CollectParamsFromEnv added in v0.3.0

func CollectParamsFromEnv(appTitle string, spec interface{}, skipDefaults bool, prefix ...string) (map[string]string, error)

func EnvNames

func EnvNames(spec interface{}, prefix ...string) ([]string, error)

func EnvNamesNoDefaults added in v0.3.0

func EnvNamesNoDefaults(spec interface{}, prefix ...string) ([]string, error)

func EnvReport

func EnvReport(spec interface{}, prefix ...string) (map[string]string, error)

func EnvToMap

func EnvToMap(spec interface{}, prefix ...string) (map[string]string, error)

func EnvVar

func EnvVar(key string) (string, error)

EnvVar ensures the variable you are looking for is set. If you don't care about that use EnvVarOptional instead

func EnvVarOptional

func EnvVarOptional(key string) string

EnvVarOptional is a wrapper around os.Getenv with the intent that by using this method you are declaring in code that you don't care about empty env vars. This is better than just using os.Getenv because that intent is not conveyed. So this simple wrapper has the purpose of reveal intent and not wrapping for the sake of wrapping

func EnvVarStrict

func EnvVarStrict(key string) (string, error)

EnvVarStrict ensures the variable is set and not empty

func PStoreKey

func PStoreKey(field Field, appTitle, env string) string

func ParamEnvField added in v0.3.0

func ParamEnvField(appTitle, env string, field Field) (string, string, error)

func ParamNames added in v0.3.0

func ParamNames(appTitle string, spec interface{}, skipDefaults bool, prefix ...string) ([]string, error)

func ProcessCLI

func ProcessCLI(cmd *cobra.Command, v *viper.Viper, spec interface{}, prefix ...string) error

func ProcessEnv

func ProcessEnv(spec interface{}, prefix ...string) error

func ProcessField

func ProcessField(value string, field reflect.Value) error

func TextUnmarshaler

func TextUnmarshaler(field reflect.Value) (t encoding.TextUnmarshaler)

Types

type Config

type Config struct {
	Data        interface{}
	SkipDefault bool
	Prefix      string
}

func NewConfig

func NewConfig(d interface{}, prefixOpt ...string) *Config

func (*Config) CollectParamsFromEnv added in v0.3.0

func (c *Config) CollectParamsFromEnv(appTitle string) (map[string]string, error)

func (*Config) EnvNames

func (c *Config) EnvNames() ([]string, error)

func (*Config) EnvReport

func (c *Config) EnvReport() (map[string]string, error)

func (*Config) EnvToMap

func (c *Config) EnvToMap() (map[string]string, error)

func (*Config) GetPrefix added in v0.3.0

func (c *Config) GetPrefix() string

func (*Config) IsDefaultsExcluded added in v0.3.0

func (c *Config) IsDefaultsExcluded() bool

func (*Config) IsPrefixEnabled added in v0.3.0

func (c *Config) IsPrefixEnabled() bool

func (*Config) MarkDefaultsAsExcluded added in v0.3.0

func (c *Config) MarkDefaultsAsExcluded()

func (*Config) MarkDefaultsAsIncluded added in v0.3.0

func (c *Config) MarkDefaultsAsIncluded()

func (*Config) ParamNames added in v0.3.0

func (c *Config) ParamNames(appTitle string) ([]string, error)

func (*Config) ProcessCLI

func (c *Config) ProcessCLI(cmd *cobra.Command, v *viper.Viper) error

func (*Config) ProcessEnv

func (c *Config) ProcessEnv() error

func (*Config) SetExcludeDefaults added in v0.3.0

func (c *Config) SetExcludeDefaults(value bool)

func (*Config) SetPrefix added in v0.3.0

func (c *Config) SetPrefix(prefix string)

type Decoder

type Decoder interface {
	Decode(value string) error
}

Decoder has the same semantics as Setter, but takes higher precedence. It is provided for historical compatibility.

func DecoderFrom

func DecoderFrom(field reflect.Value) (d Decoder)

type Field

type Field struct {
	StructName   string
	Name         string
	Prefix       string
	EnvVar       string
	ReflectValue reflect.Value
	ReflectTag   reflect.StructTag

	Tag Tag
	// contains filtered or unexported fields
}

Field holds information about the current configuration variable

func Fields

func Fields(spec interface{}, prefixParam ...string) ([]Field, error)

func NewField

func NewField(name string, prefix string, sn string, v reflect.Value, t reflect.StructTag, opts Tag) Field

func (Field) BindName

func (f Field) BindName() string

func (Field) CLIFlag

func (f Field) CLIFlag() string

func (Field) CLIShortFlag

func (f Field) CLIShortFlag() string

func (Field) CLIUsage

func (f Field) CLIUsage() string

func (Field) DefaultValue

func (f Field) DefaultValue() string

func (Field) EnvVariable

func (f Field) EnvVariable() string

func (Field) IsCLI

func (f Field) IsCLI() bool

func (Field) IsDefault

func (f Field) IsDefault() bool

func (Field) IsGlobalParamStore

func (f Field) IsGlobalParamStore() bool

func (Field) IsParamStore

func (f Field) IsParamStore() bool

func (Field) IsPersistentFlag

func (f Field) IsPersistentFlag() bool

func (Field) IsRequired

func (f Field) IsRequired() bool

func (Field) ParamStoreKey

func (f Field) ParamStoreKey() string

type Setter

type Setter interface {
	Set(value string) error
}

Setter is implemented by types can self-deserialize values. Any type that implements flag.Value also implements Setter.

func SetterFrom

func SetterFrom(field reflect.Value) (s Setter)

type Tag

type Tag struct {
	EnvVar         string
	CLIFlag        string
	CLIShort       string
	CLIUsage       string
	PStoreVar      string
	IsPStoreGlobal bool
	Default        string
	IsCLIPFlag     bool
	IsDefault      bool
	NoCLIBind      bool
	NoPrint        bool
	NoPrefix       bool
	Required       bool
	Mask           bool
}

Tag represents the annotated tag `conf` used to control how we will parse that property.

func ParseTag

func ParseTag(t string) (Tag, error)

Jump to

Keyboard shortcuts

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