envloader

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 6 Imported by: 0

README

Go Reference Travis (.com) Coverage Status Go Report Card

go-envloader

Easily load your go configuration structs from your environment variables

Import Guide:

Run the following command from your terminal

go get "github.com/anthonyme00/go-envloader"

After which you can import this package with the following line

import (
    envloader "github.com/anthonyme00/go-envloader"
)

Usage

You will need to define environment variable keys to your existing struct fields by using the env tag.

Currently, there are 2 supported attributes:

  1. key - REQUIRED : The key for getting your environment variable
  2. default - OPTIONAL : Default value for this field in case your environment variable doesn't contain an entry with key = key

Attributes uses the colon (:) for definition, and are separated with semicolon (;)

Example:

type ExampleStruct struct {
    AppName string  `env:"key:APP_NAME;default:UNIPROJECT"`
    AppPort int     `env:"key:APP_PORT"`
}

This library also supports nested structs whether it's inline or not

Example:

package main

import (
	"github.com/anthonyme00/go-envloader"
)

type AppConfig struct {
	AppName string `env:"key:APP_NAME;default:UNIPROJECT"`
	AppPort int    `env:"key:APP_PORT;default:8081"`
}

type DBConfig struct {
	// you can use colons after the identifying atrribute key
	// colons after the first one are ignored and treated as
	// part of the string
	DBUri string `env:"key:DB_URI;default:192.168.0.1:8080"`
}

type Config struct {
	App AppConfig
	DB  DBConfig
}

func main() {
	config := Config{}

	loader := envloader.New(nil)

	errs := loader.Load(&config)
	if len(errs) > 0 {
		// Error handling here
	}

	// config is ready to be used :)
}

Slices are also supported. Slice elements are separated with comma (,) Example:

type ExampleStruct struct {
    StringSlice []string  `env:"key:STRING;default:STRING 1,STRING 2"`
    Int64Slice  []int64   `env:"key:INT64;default:3,5,7"`
}

You are also able to create default configuration files using the Stringify function

Example:

package main

import (
	"fmt"

	"github.com/anthonyme00/go-envloader"
)

type AppConfig struct {
	AppName string `env:"key:APP_NAME;default:UNIPROJECT"`
	AppPort int    `env:"key:APP_PORT;default:8081"`
}

type DBConfig struct {
	DBUri string `env:"key:DB_URI;default:192.168.0.1:8080"`
}

type Config struct {
	App AppConfig
	DB  DBConfig
}

func main() {
	config := Config{}

	loader := envloader.New(nil)

	env, _ := loader.Stringify(&config)
	fmt.Println(env)
}

Output:


APP_NAME = UNIPROJECT
APP_PORT = 8081

DB_URI = 192.168.0.1:8080

Supported types

  • int64 : single, slice
  • int32 : single, slice
  • int16 : single, slice
  • int8 : single, slice
  • int : single, slice
  • float64 : single, slice
  • float32 : single, slice
  • string : single, slice
  • custom struct : single

Special Types

These types are casted from string to its respective type. Follow this guideline for these types

  • bool : single, slice
    values :
    • "true"= true
    • "false" = false

Limitations

  • Does not support pointer types

Documentation

Index

Constants

View Source
const (
	TAG_KEY = "env"

	SOURCE_ENV      = "ENV"
	SOURCE_DEFAULT  = "DEFAULT VALUE"
	SOURCE_OVERRIDE = "OVERRIDE"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// This string is used to separate attribute key
	// and values, by default it is colon (:)
	// Example:
	// 1. 	Definition = ":"
	// 		Tag = `env:"key:KEY_1"`
	// 2. 	Definition = "="
	// 		Tag = `env:"key=KEY_1"`
	Definition string
	// This string is used to separate
	// slices elements, by default it is comma (,)
	SliceSeparator string
	// This string is used to separate between
	// attributes, by default it is semicolon (;)
	AttributeSeparator string
}

Customize special characters

type EnvLoader

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

func New

func New(c *Config) EnvLoader

Create new instance of EnvLoader

c - Configuration struct for configuring behaviour

func (*EnvLoader) Load

func (e *EnvLoader) Load(i interface{}) (errs []error)

Used to load environment data to struct

**Must use pointer**

func (*EnvLoader) Stringify

func (e *EnvLoader) Stringify(i interface{}) (s string, err error)

Can be used to create a default .ENV configuration file

**Must use pointer**

Jump to

Keyboard shortcuts

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