Giter Club home page Giter Club logo

environment's Introduction

environment

Automatically retrieves values from environment variables into a configuration structure.

Usage Example

package main

import (
	"github.com/lukejoshuapark/environment"
)

type Config struct {
	Hostname string `environment:"APP_HOSTNAME,example.com"`
	Username string `environment:"APP_USERNAME"`
	Password string `environment:"APP_PASSWORD"`
}

func main() {
	cfg := &Config{}
	if err := environment.Populate(cfg); err != nil {
		// Failed to populate the provided structure.
		return
	}

	// cfg has now been populated from environment variables.
}

Parsers

It is often useful to be able to represent richer types than string in our configuration, even though we are forced to provide these values in string form through environment variables.

Consider the scenario where we might want to source a port number from our environment variables:

type Config struct {
	Hostname string `environment:"APP_HOSTNAME,example.com"`
	Port     uint16 `environment:"APP_PORT,8080"` // Non-string type.
	Username string `environment:"APP_USERNAME"`
	Password string `environment:"APP_PASSWORD"`
}

By default, environment.Populate won't know how to handle the uint16 in this struct and will fail. To show environment.Populate how to handle this type, we can use a parser:

func ParseUInt16(val string) (uint16, error) {
	n, err := strconv.ParseUint(val, 10, 16)
	if err != nil {
		return 0, err
	}

	return uint16(n), nil
}

environment.UseParser(ParseUInt16)

You can define a parser function for any type you like. An example of a more complex parser than can read base64 encoded Ed25519 private keys is provided below:

func ParseEd25519PrivateKey(val string) (ed25519.PrivateKey, error) {
	rawKey, err := base64.StdEncoding.DecodeString(val)
	if err != nil {
		return nil, fmt.Errorf("could not base64 decode private key: %w", err)
	}

	if len(rawKey) != ed25519.SeedSize {
		return nil, fmt.Errorf("expected a key length of %v bytes, but was %v bytes", ed25519.SeedSize, len(rawKey))
	}

	return ed25519.NewKeyFromSeed(rawKey), nil
}

environment.UseParser(ParseEd25519PrivateKey)

Optional Environment Variables

By default, fields tagged with environment are required - if the corresponding environment variable is not set, Populate will return a non-nil error when processing.

If instead you'd like to have a field be optional, you can provide a default value by suffixing a comma and the default string value for that field.

This can be seen above for the Hostname field. If APP_HOSTNAME is set in the environment, its value will be assigned to the Hostname field. Otherwise, the value "example.com" will be assigned.

Note that default values are assigned prior to parsing. Because of this, for non-string types, you must provide a default value in string form that can successfully be parsed by the parser for that type.


Icons made by Freepik from www.flaticon.com.

environment's People

Contributors

lukejoshuapark avatar

Stargazers

donuts-are-good avatar Evan Davies avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.