openvvar

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 9 Imported by: 1

README

github codecov

openvvar - Opinionated Environment Variables

Package openvvar provides an easy way to manage flags and environment variables at same time. Making use of struct tags to structure your configurations, providing neat features like nested structs for correlated configurations, required fields, default values for all the "primitive" types, like ints, uints, strings, booleans, floats, time.Duration and slices for any of those types.

Usage
package main

import (
    "fmt"
    "time"
    "os"
    "github.com/fogodev/openvvar"
)

type DatabaseConfig struct {
    Name     string `config:"name;default=postgresql,options=postgresql,mysql"`
    Host     string `config:"host;default=localhost"`
    Port     int    `config:"port;default=5432"`
    User     string `config:"user;required"`
    Password string `config:"password;required"`
}

type Config struct {
    Database          DatabaseConfig
    Debug             bool          `config:"debug;default=false;description=Set this config to true for debug log"`
    AcceptedHeroNames []string      `config:"hero-names;default=Deadpool,Iron Man,Dr. Strange,Rocket Raccon"`
    UniversalAnswer   uint8         `config:"universal-answer;default=42;short=u;description=THE ANSWER TO LIFE, THE UNIVERSE AND EVERYTHING"`
    SomeRandomFloat   float64       `config:"random-float;default=149714.1241"`
    OneSecond         time.Duration `config:"second;default=1s"`
}

func main() {
    configs := Config{}
    if err := openvvar.Load(&configs); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    /*
        ...
    */
}

Nested fields have their parent field name concatenated to its own name

$ DATABASE_USER=root # For environment variables

$ ./your_program -database-password=1234 # for flags

To load configurations, just instantiate an object from your struct and pass its pointer to Load function, checking for errors afterward, just like in the example above, or you can pass one or more paths to dot env files

For more examples check unit tests file

Documentation

Overview

Package openvvar provides an easy way to manage flags and environment variables at same time. Making use of struct tags to structure your configurations, providing neat features like nested structs for correlated configurations, required fields, default values for all the "primitive" types, like ints, uints, strings, booleans, floats, time.Duration and slices for any of those types.

type DatabaseConfig struct {
	Name     string `config:"name;default=postgresql"`
	Host     string `config:"host;default=localhost"`
	Port     int    `config:"port;default=5432"`
	User     string `config:"user;required"`
	Password string `config:"password;required"`
}

type Config struct {
	Database          DatabaseConfig
	Debug             bool          `config:"debug;default=false;description=Set this config to true for debug log"`
	AcceptedHeroNames []string      `config:"hero-names;default=Deadpool,Iron Man,Dr. Strange,Rocket Raccon"`
	UniversalAnswer   uint8         `config:"universal-answer;default=42;short=u;description=THE ANSWER TO LIFE, THE UNIVERSE AND EVERYTHING"`
	SomeRandomFloat   float64       `config:"random-float;default=149714.1241"`
	OneSecond         time.Duration `config:"second;default=1s"`
}

Nested fields have their parent field name concatenated to its own name

$ DATABASE_USER=root # For environment variables

$ ./your_program -database-password=1234 # for flags

To load configurations, just instantiate an object from your struct and pass its pointer to Load function, checking for errors afterward:

configs := Config{}
if err := openvvar.Load(&configs); err != nil {
	fmt.Fprintln(os.Stderr, err)
	os.Exit(1)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(receiverStruct interface{}, envFiles ...string) error

Load analyses all the Fields of the given struct for a "config" tag and queries flags and env vars

Types

type DotEnvNotFoundError

type DotEnvNotFoundError struct {
	Err error
}

DotEnvNotFoundError for when the file is not found

func (*DotEnvNotFoundError) Error

func (e *DotEnvNotFoundError) Error() string

func (*DotEnvNotFoundError) Is

func (e *DotEnvNotFoundError) Is(target error) bool

Is method to comply with new errors functions

func (*DotEnvNotFoundError) Unwrap

func (e *DotEnvNotFoundError) Unwrap() error

type FlagCollectionError

type FlagCollectionError struct {
	Errors map[error]bool // Using this map as a hash set
}

FlagCollectionError when we receive errors from flag library

func (*FlagCollectionError) Error

func (e *FlagCollectionError) Error() string

func (*FlagCollectionError) Is

func (e *FlagCollectionError) Is(target error) bool

Is method to comply with new errors functions

type FlagParseError

type FlagParseError struct {
	Err error
}

FlagParseError is for when we fail to parse a specific flag

func (*FlagParseError) Error

func (e *FlagParseError) Error() string

func (*FlagParseError) Is

func (e *FlagParseError) Is(target error) bool

Is method to comply with new errors functions

type InvalidReceiverError

type InvalidReceiverError struct{}

InvalidReceiverError when developer pass something that isn't a point to struct to receive configs

func (*InvalidReceiverError) Error

func (e *InvalidReceiverError) Error() string

func (*InvalidReceiverError) Is

func (e *InvalidReceiverError) Is(target error) bool

Is method to comply with new errors functions

type InvalidTypeForDefaultValuesError

type InvalidTypeForDefaultValuesError struct {
	Type string
}

InvalidTypeForDefaultValuesError when developer puts a bogus default value for some type

func (*InvalidTypeForDefaultValuesError) Error

func (*InvalidTypeForDefaultValuesError) Is

Is method to comply with new errors functions

type MissingRequiredFieldError

type MissingRequiredFieldError struct {
	Key   string
	Field string
}

MissingRequiredFieldError when user forgets to fill a required config

func (*MissingRequiredFieldError) Error

func (e *MissingRequiredFieldError) Error() string

func (*MissingRequiredFieldError) Is

func (e *MissingRequiredFieldError) Is(target error) bool

Is method to comply with new errors functions

type TypeConversionError

type TypeConversionError struct {
	Err error
}

TypeConversionError occurs on string parsing for some types

func (*TypeConversionError) Error

func (e *TypeConversionError) Error() string

func (*TypeConversionError) Is

func (e *TypeConversionError) Is(target error) bool

Is method to comply with new errors functions

func (*TypeConversionError) Unwrap

func (e *TypeConversionError) Unwrap() error

type ValueNotAValidOptionError added in v0.10.0

type ValueNotAValidOptionError struct {
	Value   string
	Options map[string]bool
}

ValueNotAValidOptionError for when received valued is not listed as a valid option

func (*ValueNotAValidOptionError) Error added in v0.10.0

func (e *ValueNotAValidOptionError) Error() string

func (*ValueNotAValidOptionError) Is added in v0.10.0

func (e *ValueNotAValidOptionError) Is(target error) bool

Is method to comply with new errors functions

Jump to

Keyboard shortcuts

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