All Projects → antham → Envh

antham / Envh

Licence: mit
Go helpers to manage environment variables

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Envh

dotfiles
My personal app/env configs and dotfiles.
Stars: ✭ 27 (-71.58%)
Mutual labels:  environment, configuration-management
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+49.47%)
Mutual labels:  configuration-management, environment
awesome-hacktoberfest-plant-a-tree
Will you choose the ✨ Hacktoberfest t-shirt ✨ but don't want to stop contributing to the environment and a sustainable future? Find an organization here so you can plant a tree! 🌱
Stars: ✭ 30 (-68.42%)
Mutual labels:  environment, tree
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (-24.21%)
Mutual labels:  environment, configuration-management
carbon footprint
An open-source about a Carbon Footprint Calculator made with Reactjs. The objective is to have a nice simple web about the environment and how to preserve our planet.
Stars: ✭ 14 (-85.26%)
Mutual labels:  environment, tree
Hands On Algorithmic Problem Solving
A middle-to-high level algorithm book designed with coding interview at heart!
Stars: ✭ 1,227 (+1191.58%)
Mutual labels:  tree
Astq
Abstract Syntax Tree (AST) Query Engine
Stars: ✭ 89 (-6.32%)
Mutual labels:  tree
Waffles
Bash Configuration Management
Stars: ✭ 79 (-16.84%)
Mutual labels:  configuration-management
Rlenv.directory
Explore and find reinforcement learning environments in a list of 150+ open source environments.
Stars: ✭ 79 (-16.84%)
Mutual labels:  environment
Beetbox
Pre-provisioned L*MP stack
Stars: ✭ 94 (-1.05%)
Mutual labels:  environment
X
Desktop environment in the browser.
Stars: ✭ 1,316 (+1285.26%)
Mutual labels:  environment
Homies
linux package management
Stars: ✭ 86 (-9.47%)
Mutual labels:  environment
Openbsd Cookbooks
Setup environment in OpenBSD using Ansible playbook
Stars: ✭ 80 (-15.79%)
Mutual labels:  configuration-management
Autorandr
Auto-detect the connected display hardware and load the appropriate X11 setup using xrandr
Stars: ✭ 1,286 (+1253.68%)
Mutual labels:  configuration-management
Splay Tree
Fast splay-tree data structure
Stars: ✭ 80 (-15.79%)
Mutual labels:  tree
Night Config
Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations
Stars: ✭ 93 (-2.11%)
Mutual labels:  configuration-management
Redmine issues tree
Provides a tree view of the Redmine issues list
Stars: ✭ 79 (-16.84%)
Mutual labels:  tree
Behavior Tree
🌲 Manage React state with Behavior Trees
Stars: ✭ 85 (-10.53%)
Mutual labels:  tree
Smart Array To Tree
Convert large amounts of data array to tree fastly
Stars: ✭ 91 (-4.21%)
Mutual labels:  tree
Envkey App
Secure, human-friendly, cross-platform secrets and config.
Stars: ✭ 83 (-12.63%)
Mutual labels:  configuration-management

Envh CircleCI codecov codebeat badge Go Report Card GolangCI GoDoc GitHub tag

This library is made up of two parts :

  • Env object : it wraps your environments variables in an object and provides convenient helpers.
  • Env tree object : it manages environment variables through a tree structure to store a config the same way as in a yaml file or whatever format allows to store a config hierarchically

Install

go get github.com/antham/envh

How it works

Check the godoc, there are many examples provided.

Example with a tree dumped in a config struct

package envh

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

type CONFIG2 struct {
	DB struct {
		USERNAME   string
		PASSWORD   string
		HOST       string
		NAME       string
		PORT       int
		URL        string
		USAGELIMIT float32
	}
	MAILER struct {
		HOST     string
		USERNAME string
		PASSWORD string
		ENABLED  bool
	}
	MAP map[string]string
}

func (c *CONFIG2) Walk(tree *EnvTree, keyChain []string) (bool, error) {
	if setter, ok := map[string]func(*EnvTree, []string) error{
		"CONFIG2_DB_URL": c.setURL,
		"CONFIG2_MAP":    c.setMap,
	}[strings.Join(keyChain, "_")]; ok {
		return true, setter(tree, keyChain)
	}

	return false, nil
}

func (c *CONFIG2) setMap(tree *EnvTree, keyChain []string) error {
	datas := map[string]string{}

	keys, err := tree.FindChildrenKeys(keyChain...)

	if err != nil {
		return err
	}

	for _, key := range keys {
		value, err := tree.FindString(append(keyChain, key)...)

		if err != nil {
			return err
		}

		datas[key] = value
	}

	c.MAP = datas

	return nil
}

func (c *CONFIG2) setURL(tree *EnvTree, keyChain []string) error {
	datas := map[string]string{}

	for _, key := range []string{"USERNAME", "PASSWORD", "HOST", "NAME"} {
		value, err := tree.FindString("CONFIG2", "DB", key)

		if err != nil {
			return err
		}

		datas[key] = value
	}

	port, err := tree.FindInt("CONFIG2", "DB", "PORT")

	if err != nil {
		return err
	}

	c.DB.URL = fmt.Sprintf("jdbc:mysql://%s:%d/%s?user=%s&password=%s", datas["HOST"], port, datas["NAME"], datas["USERNAME"], datas["PASSWORD"])

	return nil
}

func ExampleStructWalker_customFieldSet() {
	os.Clearenv()
	setEnv("CONFIG2_DB_USERNAME", "foo")
	setEnv("CONFIG2_DB_PASSWORD", "bar")
	setEnv("CONFIG2_DB_HOST", "localhost")
	setEnv("CONFIG2_DB_NAME", "my-db")
	setEnv("CONFIG2_DB_PORT", "3306")
	setEnv("CONFIG2_DB_USAGELIMIT", "95.6")
	setEnv("CONFIG2_MAILER_HOST", "127.0.0.1")
	setEnv("CONFIG2_MAILER_USERNAME", "foo")
	setEnv("CONFIG2_MAILER_PASSWORD", "bar")
	setEnv("CONFIG2_MAILER_ENABLED", "true")
	setEnv("CONFIG2_MAP_KEY1", "value1")
	setEnv("CONFIG2_MAP_KEY2", "value2")
	setEnv("CONFIG2_MAP_KEY3", "value3")

	env, err := NewEnvTree("^CONFIG2", "_")

	if err != nil {
		return
	}

	s := CONFIG2{}

	err = env.PopulateStruct(&s)

	if err != nil {
		return
	}

	b, err := json.Marshal(s)

	if err != nil {
		return
	}

	fmt.Println(string(b))
	// Output:
	// {"DB":{"USERNAME":"foo","PASSWORD":"bar","HOST":"localhost","NAME":"my-db","PORT":3306,"URL":"jdbc:mysql://localhost:3306/my-db?user=foo\u0026password=bar","USAGELIMIT":95.6},"MAILER":{"HOST":"127.0.0.1","USERNAME":"foo","PASSWORD":"bar","ENABLED":true},"MAP":{"KEY1":"value1","KEY2":"value2","KEY3":"value3"}}
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].