goappenv

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 4 Imported by: 3

README

GO APP ENV

ci codecov Go Report Card GoDoc

Library to read JSON file and transform into flatten configuration map with ability to overwrite config by using specific environment file or environment variable.

Active environment

Active environment can configure from environment variable name GO_APP_ACTIVE_ENV or command line argument --env.

In case of found both environment variable and command line argument, AppEnv will use value from environment variable

If both not provide, default active environment as default

Config Directory

Config directory can configure from environment variable name GO_APP_CONFIG_DIR or command line argument --configDir.

In case of found both environment variable and command line argument, AppEnv will use value from environment variable

If both not provide, default active environment as ./resources/


Configuration Mapping

By default, App env will read JSON file name config.json under config directory. Will return empty map of configuration in case of file not found.

Flatten Configuration

App env will read JSON and transform into 1 level map by joining key with dot (.). For example, following JSON

{
    "app":{
        "version": 1,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
}

will transform to key-value map

"app.version" = 1
"app.name" = "demo"
"app.meta.something" = "value"
"active" = true
active environment overwrite

In case of active environment is not default, App env will trying to read config-{activeEnv}.json under config directory. In case of file not found, will fallback to use configuration from config.json.

If config-{activeEnv}.json found, will merge configuration with config.json by take value from active env configuration as priority.

Ex.
// config.json
{
    "app":{
        "version": 1,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
}

// config-{activeEnv}.json
{
    "app":{
        "version": 2
    },
    "active": true
    "provider": "internal"
}

// merge result
{
    "app":{
        "version": 2,
        "name": "demo",
        "meta": {
            "something": "value"
        }
    },
    "active": true
    "provider": "internal"
}
Environment variable overwrite

App env will try to read flatten configuration key, by replace dash (-) and dot (.) with underscore (_) and change all character to upper case. If no variable found will use active env config or default.

Ex.
app.version >> APP_VERSION
app.api.get-user >> APP_API_GET_USER

Usage

appEnv := NewAppEnv(os.DirFS("{desired directory}"))

Example


// in file main.go
...
app := appenv.NewAppEnv(os.DirFS("."))
fmt.Printf("env: %s, configDir:%s \n", app.ActiveEnv(), app.ConfigDir())
...

/*
run:
    go run main.go
result:
    env: default, configDir: ./resources/

run:
    go run main.go --env=test
result:
    env: test, configDir: ./resources/
    
run:
    go run main.go --env=test --configDir=./cfg
result:
    env: test, configDir: ./cfg
    
run:
    GO_APP_ACTIVE_ENV=local GO_APP_CONFIG_DIR=./tmp go run main.go --env=test --configDir=./cfg
result:
    env: local, configDir: ./tmp
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigOrDefault

func ConfigOrDefault(appenv IAppEnv, key string, defaultValue interface{}) interface{}

Types

type AppEnv

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

func NewAppEnv

func NewAppEnv(fsys fs.FS, ctx context.Context) *AppEnv

func (*AppEnv) ActiveEnv

func (a *AppEnv) ActiveEnv() string

func (*AppEnv) Config

func (a *AppEnv) Config() map[string]interface{}

func (*AppEnv) ConfigDir

func (a *AppEnv) ConfigDir() string

func (*AppEnv) Context

func (a *AppEnv) Context() context.Context

type IAppEnv

type IAppEnv interface {
	ActiveEnv() string
	ConfigDir() string
	Config() map[string]interface{}
	Context() context.Context
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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