env_strings

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2015 License: Apache-2.0 Imports: 17 Imported by: 3

README

ENV STRINGS

Read Env from config and compile the values into string.

About ENV JSON

Sometimes we need use some config string as following:

db.conf

configStr:="127.0.0.1|123456|1000"

but, when we management more and more server and serivce, and if we need change the password or ip, it was a disaster.

So, we just want use config string like this.

db.conf

configStr:="{{.host}}|{{.password}}|{{.timeout}}"

We use golang's template to replace values into the config while we execute the string.

first, we set the env config at ~/.bash_profile or ~/.zshrc, and the default key is ENV_STRINGS and the default file extention is .env, the value of ENV_STRINGS could be a file or folder,it joined by;, it will automatic load all *.env files.

Env

export ENV_STRINGS ='~/playgo/test.env;~/playgo/test2.env'

or

export ENV_STRINGS ='~/playgo'
example program
package main

import (
	"fmt"

	"github.com/gogap/env_strings"
)

func main() {
	if ret, err := env_strings.Execute("{{.host}}|{{.password}}|{{.timeout}}"); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(ret)
	}

	envStrings := env_strings.NewEnvStrings("ENV_KEY", ".env")

	if ret, err := envStrings.Execute("{{.host}}|{{.password}}|{{.timeout}}"); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(ret)
	}
}

env1.json

{
	"host":"127.0.0.1",
	"password":"123456"
}

env2.json

{
	"timeout":1000
}

result:

{127.0.0.1 123456 1000}
Advance
use redis to storage conf

we should set the config file of /etc/env_strings.conf (default path) before use, or set config file path by system ENV with your '~/.bash_profile' or ~/.zshrc, the key is ENV_STRINGS_CONF, just edit like this: export ENV_STRINGS_CONF=/etc/env_strings.conf

/etc/env_strings.conf

{
    "storages": [{
        "engine": "redis",
        "options": {
            "db": 1,
            "password": "",
            "pool_size": 10,
            "address": "localhost:6379"
        }
    }]
}
set data to redis
> redis-cli -n 1 set name gogap
OK
> redis-cli -n 1 get name
gogap
> redis-cli -n 1 set key field value
OK
> redis-cli -n 1 hget key field
value
redis command: redis_get

config template:

{
	"name":"{{redis_get "name"}}"
}

result:

{
	"name":"gogap"
}

if key not exist, and we want get a default value

{
	"name":"{{redis_get "noexistkey" "gogap"}}"
}

result:

{
	"name":"gogap"
}
redis command: redis_hget

config template:

{
	"name":"{{redis_hget "key" "field"}}"
}

result:

{
	"name":"value"
}

if key or filed not exist, and we want get a default value

{
	"name":"{{redis_hget "key2" "filed" "gogap"}}"
}

result:

{
	"name":"gogap"
}

Documentation

Index

Constants

View Source
const (
	ENV_STRINGS_KEY = "ENV_STRINGS"
	ENV_STRINGS_EXT = ".env"

	ENV_STRINGS_CONF = "/etc/env_strings.conf"

	ENV_STRINGS_CONFIG_KEY = "ENV_STRINGS_CONF"
)
View Source
const (
	STORAGE_REDIS = "redis"
)

Variables

This section is empty.

Functions

func EnvStringsConfig

func EnvStringsConfig(fileName string) option

func Execute

func Execute(str string) (ret string, err error)

func ExecuteWith

func ExecuteWith(str string, envValues map[string]interface{}) (ret string, err error)

func FuncMap

func FuncMap(name string, function interface{}) option

func UnmarshalJsonArray

func UnmarshalJsonArray(data string) ([]interface{}, error)

func UnmarshalJsonObject

func UnmarshalJsonObject(data string) (map[string]interface{}, error)

Types

type EnvStringConfig

type EnvStringConfig struct {
	Storages []StorageConfig `json:"storages"`
}

type EnvStrings

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

func NewEnvStrings

func NewEnvStrings(envName string, envExt string, opts ...option) *EnvStrings

func (*EnvStrings) Execute

func (p *EnvStrings) Execute(str string) (ret string, err error)

func (*EnvStrings) ExecuteWith

func (p *EnvStrings) ExecuteWith(str string, envValues map[string]interface{}) (ret string, err error)

func (*EnvStrings) FuncUsageStatic

func (p *EnvStrings) FuncUsageStatic() map[string][]FuncStaticItem

func (*EnvStrings) RegisterFunc

func (p *EnvStrings) RegisterFunc(name string, function interface{}) (err error)

type ExtFunc

type ExtFunc func(args ...interface{}) (ret interface{}, err error)

type ExtFuncs

type ExtFuncs interface {
	GetFuncs() template.FuncMap
}

func NewExtFuncsRedis

func NewExtFuncsRedis(options map[string]interface{}) ExtFuncs

type ExtFuncsRedis

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

func (*ExtFuncsRedis) Get

func (p *ExtFuncsRedis) Get(args ...interface{}) (ret interface{}, err error)

func (*ExtFuncsRedis) GetFuncs

func (p *ExtFuncsRedis) GetFuncs() template.FuncMap

func (*ExtFuncsRedis) HGet

func (p *ExtFuncsRedis) HGet(args ...interface{}) (ret interface{}, err error)

type FuncStaticItem

type FuncStaticItem struct {
	EnvName  string
	FuncName string
	Input    []interface{}
	Output   []interface{}
}

type StorageConfig

type StorageConfig struct {
	Engine  string                 `json:"engine"`
	Options map[string]interface{} `json:"options"`
}

type TemplateFuncs

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

func NewTemplateFuncs

func NewTemplateFuncs() *TemplateFuncs

func (*TemplateFuncs) GetFuncMaps

func (p *TemplateFuncs) GetFuncMaps(envName string) template.FuncMap

func (*TemplateFuncs) Register

func (p *TemplateFuncs) Register(name string, function interface{}) (err error)

Jump to

Keyboard shortcuts

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