cafe

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 5 Imported by: 1

README

CAFE

Configuation Management for Environment Variables for Applications

What is CAFE?

CAFE is a simple configuration management tool for environment variables for applications. It is designed to offer similar functionalities to joi and yup but for environment variables in Golang Projects.

Why CAFE?

CAFE is designed to be used in Golang projects that require environment variables to be validated and parsed. Cafe can Ensure that the Enviroment Variables are set and are of the correct type.

How to use CAFE?

Set-up
// Setting Up The schema
config := cafe.New(
	cafe.Schema{
			"PORT":        cafe.Int("SERVER_PORT"),              // PORT is an integer that is set by the SERVER_PORT environment variable and is not required
			"DB_HOST":     cafe.String("DB_HOST").Require(),     // DB_HOST is a string that is required
			"DB_PORT":     cafe.Int("DB_PORT").Require(),        // DB_PORT is an integer that is required
			"DB_USER":     cafe.String("DB_USER").Require(),     // DB_USER is a string that is required
			"DB_PASSWORD": cafe.String("DB_PASSWORD").Require(), // DB_PASSWORD is a string that is required
			"DB_NAME":     cafe.String("DB_NAME").Require(),     // DB_NAME is a string that is required
		})
Intializing and Validating
 // err will be nil if all the environment variables are set and are of the correct type
 // if err is not nil, it will contain the error message check errs.go for more information
 err := s.Initialize()
ALT: Set-up, Intialize and Validate
config, err := New( // Creates a new schema and initializes it
		Schema{
			"PORT":        cafe.Int("SERVER_PORT"),              // PORT is an integer that is set by the SERVER_PORT environment variable and is not required
			"DB_HOST":     cafe.String("DB_HOST").Require(),     // DB_HOST is a string that is required
			"DB_PORT":     cafe.Int("DB_PORT").Require(),        // DB_PORT is an integer that is required
			"DB_USER":     cafe.String("DB_USER").Require(),     // DB_USER is a string that is required
			"DB_PASSWORD": cafe.String("DB_PASSWORD").Require(), // DB_PASSWORD is a string that is required
			"DB_NAME":     cafe.String("DB_NAME").Require(),     // DB_NAME is a string that is required
		},
	)
	if err != nil {
		t.Error(err)
	}
Accessing the variables
serverPort,err := config.Getcafe.Int("PORT") 
if err != nil {
	// handle error
}

Roadmap

  • Support for Integers
  • Support for Strings
  • Support for Floats
  • Support for Booleans
  • Support for Arrays
  • Support for Maps
  • Support for Structs and nested objects
  • Muliple Configurations Sources (JSON, YAML, TOML, etc)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Err_REQUIRED_KEY_MISSING = fmt.Errorf("required key missing")
	Err_KEY_IS_REQUIRED      = fmt.Errorf("key is required")
)

Functions

This section is empty.

Types

type Cafe

type Cafe struct {
	// contains filtered or unexported fields
}

func New

func New(s Schema) (*Cafe, error)

func NewCafeSchema

func NewCafeSchema(s Schema) *Cafe

func (*Cafe) Env added in v0.4.2

func (c *Cafe) Env() string

func (*Cafe) GetBool

func (s *Cafe) GetBool(k string) (bool, error)

func (*Cafe) GetInt

func (s *Cafe) GetInt(k string) (int, error)

func (*Cafe) GetString

func (s *Cafe) GetString(k string) (string, error)

func (*Cafe) GetSubSchema added in v0.4.1

func (s *Cafe) GetSubSchema(k string) (*Cafe, error)

func (*Cafe) Initialize

func (s *Cafe) Initialize() error

func (*Cafe) JSON added in v0.4.2

func (c *Cafe) JSON() string

type Schema

type Schema map[string]*Type

type Type

type Type struct {
	Value interface{}
	// contains filtered or unexported fields
}

func Bool

func Bool(k string) *Type

func Int

func Int(k string) *Type

func String

func String(k string) *Type

func SubSchema added in v0.4.1

func SubSchema(k string, s Schema) *Type

func (*Type) Before

func (t *Type) Before() error

func (*Type) Default

func (t *Type) Default(v interface{}) *Type

func (*Type) Key

func (t *Type) Key(k string) *Type

func (*Type) Require

func (t *Type) Require() *Type

func (*Type) Validate

func (t *Type) Validate() error

Jump to

Keyboard shortcuts

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