ecp

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: Apache-2.0 Imports: 6 Imported by: 10

README

ecp

Environment config parser

If you run your application in a container and deploy it via a docker-compose file, then you may need this tool for parsing configuration easily instead of mounting an external config file. You can simply set some environments and then ecp will help you fill the configs. Or, you can COPY a "default" config file to the image, and change some variables by overwriting the keys via environments.

The environment config keys can be auto generated or set by the yaml or env tag.

The only thing you should do is importing this package, and Parse your config.

Go Report Card Build Status GoDoc license

Usage Example

package main

import (
    "fmt"
    "os"

    "github.com/wrfly/ecp"
)

type Conf struct {
    LogLevel string `default:"debug"`
    Port     int    `env:"PORT"`
}

func main() {
    config := &Conf{}
    if err := ecp.Parse(config); err != nil {
        panic(err)
    }
    fmt.Printf("default log level: [ %s ]\n", config.LogLevel)
    fmt.Println()

    // set some env
    envs := map[string]string{
        "LOGLEVEL": "info",
        "PORT":         "1234",
    }
    for k, v := range envs {
        fmt.Printf("export %s=%s\n", k, v)
        os.Setenv(k, v)
    }
    fmt.Println()

    // then parse configuration from environments
    if err := ecp.Parse(config, "ECP"); err != nil {
        panic(err)
    }
    fmt.Printf("new log level: [ %s ], port: [ %d ]\n",
        config.LogLevel, config.Port)
    fmt.Println()

    // and list all the env keys
    envLists := ecp.List(config, "ecp")
    for _, k := range envLists {
        fmt.Println(k)
    }
}

Outputs:

default log level: [ debug ]

export LOGLEVEL=info
export PORT=1234

new log level: [ info ], port: [ 1234 ]

LOGLEVEL=debug
PORT=

Documentation

Overview

Package ecp can help you convert environments into configurations it's an environment config parser

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(config interface{}, keyName string) (interface{}, error)

Get the value of the keyName in that struct

func GetBool

func GetBool(config interface{}, keyName string) (bool, error)

GetBool returns bool

func GetFloat64

func GetFloat64(config interface{}, keyName string) (float64, error)

GetFloat64 returns float64

func GetInt64

func GetInt64(config interface{}, keyName string) (int64, error)

GetInt64 returns int64

func GetString

func GetString(config interface{}, keyName string) (string, error)

GetString returns string

func List

func List(config interface{}, prefix ...string) []string

List all the config environments

Example
type config struct {
	Age  int
	Name string
}
for _, key := range List(config{}) {
	fmt.Printf("env %s", key)
}

// env AGE=
// env NAME=
Output:

func New added in v0.1.3

func New() *ecp

New ecp object

func Parse

func Parse(config interface{}, prefix ...string) error

Parse the configuration through environments starting with the prefix (or not) ecp.Parse(&config) or ecp.Parse(&config, "PREFIX")

Parse will overwrite the existing value if there is an environment configuration matched with the struct name or the "env" tag name.

Also, Parse will set the default value to the config, if it's not set values. For basic types, if the value is zero value, then it will be set to the default value. You can change the basic type to a pointer type, thus Parse will only set the default value when the field is nil, not the zero value. for example:

type config struct {
    One   string   `default:"1"`
    Two   int      `default:"2"`
    Three []string `default:"1,2,3"`
}
c := &config{}
Example
type config struct {
	Age      int           `default:"18"`
	Name     string        `default:"wrfly"`
	Duration time.Duration `default:"10d"`
}
c := &config{}
if err := Parse(&c); err != nil {
	panic(err)
}

os.Setenv("AGE", "10")
if err := Parse(&c); err != nil {
	panic(err)
}

// now you'll get a config with
// `Age=10` and `Name=wrfly`
if c.Age != 10 || c.Name != "wrfly" || c.Duration != time.Hour*24*10 {
	panic("???")
}
Output:

Types

type BuildKeyFunc added in v0.2.0

type BuildKeyFunc func(structure, field string, tag reflect.StructTag) string

BuildKeyFunc how to get the key name

type LookupValueFunc added in v0.1.3

type LookupValueFunc func(key string) (value string, exist bool)

LookupValueFunc returns the value and whether exist

type SetValueFunc added in v0.2.0

type SetValueFunc func(tag reflect.StructTag, field reflect.Value, val string) bool

SetValueFunc set the field value and returns whether this filed is set by this function

Jump to

Keyboard shortcuts

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