Compare commits
6 Commits
179666c8f9
...
v0.1.57
Author | SHA1 | Date | |
---|---|---|---|
2efc1a155e
|
|||
45f8b0f918
|
|||
2cdcbec364
|
|||
f815bff582
|
|||
73278439b0
|
|||
0a589a2565
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
tests/buildenv/*
|
||||||
|
tests/bin/*
|
@ -1,3 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
go fmt *.go
|
go fmt *.go
|
||||||
git add *.go
|
go fmt tests/*.go
|
||||||
|
|
||||||
|
git add *.go tests/*.go
|
||||||
|
git add tests/*.go
|
||||||
|
53
envconf.go
53
envconf.go
@ -23,7 +23,7 @@ type cEntry struct {
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
parsed bool
|
parsed bool
|
||||||
env map[string]cEntry
|
env map[string]cEntry
|
||||||
mapEnv map[string]map[string]cEntry
|
mapEnv map[string]map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
|
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
|
||||||
@ -33,27 +33,31 @@ func NewConfig() *Config {
|
|||||||
config := new(Config)
|
config := new(Config)
|
||||||
config.parsed = false
|
config.parsed = false
|
||||||
config.env = make(map[string]cEntry)
|
config.env = make(map[string]cEntry)
|
||||||
config.mapEnv = make(map[string]map[string]cEntry)
|
config.mapEnv = make(map[string]map[string]string)
|
||||||
|
|
||||||
for _, v := range os.Environ() {
|
for _, v := range os.Environ() {
|
||||||
splitted := strings.SplitN(v, "=", 2)
|
splitted := strings.SplitN(v, "=", 2)
|
||||||
if len(splitted) == 2 {
|
if len(splitted) == 2 {
|
||||||
key := cleanKey(splitted[0])
|
key := cleanKey(splitted[0])
|
||||||
left, right, mappable := keySplit(key)
|
val := splitted[1]
|
||||||
|
splitted = strings.Split(key, "_")
|
||||||
if unicode.IsLetter(getFirstRune(key)) {
|
if unicode.IsLetter(getFirstRune(key)) {
|
||||||
var entry cEntry
|
var entry cEntry
|
||||||
entry.value = splitted[1]
|
entry.value = val
|
||||||
entry.dtype = TypeNone
|
entry.dtype = TypeNone
|
||||||
entry.unset = false
|
entry.unset = false
|
||||||
entry.empty = false
|
entry.empty = false
|
||||||
config.env[key] = entry
|
config.env[key] = entry
|
||||||
if mappable {
|
if len(splitted) > 1 {
|
||||||
|
for count, _ := range splitted {
|
||||||
|
left := strings.Join(splitted[:count], "_")
|
||||||
|
right := strings.Join(splitted[count:], "_")
|
||||||
if len(config.mapEnv[left]) == 0 {
|
if len(config.mapEnv[left]) == 0 {
|
||||||
config.mapEnv[left] = make(map[string]cEntry)
|
config.mapEnv[left] = make(map[string]string)
|
||||||
|
}
|
||||||
|
fmt.Printf("left=(%s), right=(%s), key=(%s)\n", left, right, key)
|
||||||
|
config.mapEnv[left][right] = key
|
||||||
}
|
}
|
||||||
|
|
||||||
config.mapEnv[left][right] = entry
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,11 +86,14 @@ func (c *Config) Define(key string, dtype DataType) {
|
|||||||
// Variables without a defined type will be ignored by Parse.
|
// Variables without a defined type will be ignored by Parse.
|
||||||
func (c *Config) DefineMap(key string, dtype DataType) {
|
func (c *Config) DefineMap(key string, dtype DataType) {
|
||||||
key = cleanKey(key)
|
key = cleanKey(key)
|
||||||
|
fmt.Println(c.mapEnv)
|
||||||
entries, ok := c.mapEnv[key]
|
entries, ok := c.mapEnv[key]
|
||||||
if ok {
|
if ok {
|
||||||
for mapKey, entry := range entries {
|
for _, key = range entries {
|
||||||
|
fmt.Printf("key-from-define-map (%s)\n", key)
|
||||||
|
entry := c.env[key]
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
c.mapEnv[key][mapKey] = entry
|
c.env[key] = entry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,13 +143,6 @@ func (c *Config) Parse() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range c.mapEnv {
|
|
||||||
for mk, mv := range v {
|
|
||||||
mv.parsed = mv.dtype.parse(k+"_"+mk, mv.value)
|
|
||||||
c.mapEnv[k][mk] = mv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if failed {
|
if failed {
|
||||||
for k, v := range c.env {
|
for k, v := range c.env {
|
||||||
if (v.parsed.err == nil) && v.unset {
|
if (v.parsed.err == nil) && v.unset {
|
||||||
@ -234,17 +234,7 @@ func (c *Config) Status() (ok bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, v := range c.mapEnv {
|
|
||||||
for _, mv := range v {
|
|
||||||
err := mv.parsed.err
|
|
||||||
if err != nil {
|
|
||||||
ok = false
|
|
||||||
if !mv.empty {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Fprintln(os.Stderr, "")
|
fmt.Fprintln(os.Stderr, "")
|
||||||
for _, v := range c.env {
|
for _, v := range c.env {
|
||||||
@ -303,8 +293,9 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
|
|||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
for k, v := range entries {
|
for k, v := range entries {
|
||||||
if (v.dtype.baseType() == dtype.baseType()) && (v.parsed.err == nil) {
|
entry := c.env[v]
|
||||||
retval[k] = v.parsed
|
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
|
||||||
|
retval[k] = entry.parsed
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
22
tests/build.sh
Executable file
22
tests/build.sh
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
dir=$(dirname $(realpath $0))
|
||||||
|
export GO111MODULE=on
|
||||||
|
export GOPROXY=direct
|
||||||
|
export GOPATH=${dir}/buildenv/deps
|
||||||
|
|
||||||
|
mkdir -p ${dir}/buildenv
|
||||||
|
chmod -R 755 ${dir}/buildenv
|
||||||
|
rm -rf ${dir}/buildenv
|
||||||
|
|
||||||
|
mkdir -p "${dir}/buildenv/src"
|
||||||
|
mkdir -p ${GOPATH}/src
|
||||||
|
mkdir -p ${dir}/bin
|
||||||
|
|
||||||
|
cp -r "${dir}/main.go" "${dir}/buildenv/src/main.go"
|
||||||
|
|
||||||
|
cd ${dir}/buildenv
|
||||||
|
|
||||||
|
go mod init src
|
||||||
|
go mod tidy
|
||||||
|
|
||||||
|
go build -o "${dir}/bin/test" "src/main.go"
|
17
tests/main.go
Normal file
17
tests/main.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.purser.it/roypur/envconf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conf := envconf.NewConfig()
|
||||||
|
conf.Define("this_is_a_map", envconf.TypeInt)
|
||||||
|
conf.Define("test_map", envconf.TypeInt)
|
||||||
|
conf.Parse()
|
||||||
|
conf.Status()
|
||||||
|
|
||||||
|
fmt.Println(conf.GetMapInt("test_map"))
|
||||||
|
fmt.Println(conf.GetInt("this_is_a_map"))
|
||||||
|
}
|
Reference in New Issue
Block a user