use local files for test

This commit is contained in:
Roy Olav Purser 2022-09-17 13:07:12 +02:00
parent 9badf9a54a
commit 4a84dcdabe
Signed by: roypur
GPG Key ID: 90142918D5E59AD3
2 changed files with 102 additions and 65 deletions

View File

@ -20,35 +20,47 @@ type cEntry struct {
hasdef bool // Default value is defined hasdef bool // Default value is defined
} }
type keyLookupStruct struct {
key string
end bool
next map[string]*keyLookupStruct
}
func (kl *keyLookupStruct) setKey(parts []string, key string) {
if len(kl.next) == 0 {
kl.next = make(map[string]*keyLookupStruct)
}
if len(parts) > 0 {
nextLookup := new(keyLookupStruct)
nextLookup.setKey(parts[1:], key)
kl.next[parts[0]] = nextLookup
} else {
kl.key = key
kl.end = true
}
}
type Config struct { type Config struct {
parsed bool parsed bool
env map[string]cEntry env map[string]cEntry
mapEnv map[string]map[string]string mapEnv map[int]*keyLookupStruct
mapMapEnv map[string]map[string]map[string]string
mapMapMapEnv map[string]map[string]map[string]map[string]string
mapMapMapMapEnv map[string]map[string]map[string]map[string]map[string]string
mapMapMapMapMapEnv map[string]map[string]map[string]map[string]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.
// The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called // The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called
// will not be taken into account. // will not be taken into account.
func NewConfig() *Config { func NewConfig(levels int) *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]string) config.mapEnv = make(map[int]*keyLookupStruct)
config.mapMapEnv = make(map[string]map[string]map[string]string)
config.mapMapMapEnv = make(map[string]map[string]map[string]map[string]string)
config.mapMapMapMapEnv = make(map[string]map[string]map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv = make(map[string]map[string]map[string]map[string]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])
val := splitted[1] val := splitted[1]
splitted = append(strings.Split(key, "_"), "", "", "", "", "", "") splitted = append(strings.Split(key, "_"), make([]string, levels, levels)...)
if unicode.IsLetter(getFirstRune(key)) { if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry var entry cEntry
entry.value = val entry.value = val
@ -58,65 +70,83 @@ func NewConfig() *Config {
config.env[key] = entry config.env[key] = entry
if len(splitted) > 1 { if len(splitted) > 1 {
for count, _ := range splitted { for count, _ := range splitted {
if count < len(splitted)-6 { if count >= len(splitted)-levels {
var p0, p1, p2, p3, p4, p5 string break
p0 = strings.Trim(strings.Join(splitted[:count], "_"), "_") }
p1 = strings.Trim(strings.Join(splitted[count:], "_"), "_") for level := 1; level <= levels; level++ {
if len(config.mapEnv[p0]) == 0 { parts := make([]string, level, level)
config.mapEnv[p0] = make(map[string]string) for partPos, part := range parts {
config.mapMapEnv[p0] = make(map[string]map[string]string) firstPartPos := 0
config.mapMapMapEnv[p0] = make(map[string]map[string]map[string]string) if partPos > 0 {
config.mapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]string) firstPartPos = count + partPos - 1
config.mapMapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]map[string]string) }
} lastPartPos := firstPartPos
if p0 != "" && p1 != "" { if partPos-1 == len(parts) {
config.mapEnv[p0][p1] = key lastPartPos = len(parts)
}
parts[partPos] = strings.Trim(strings.Join(splitted[:count], "_"), "_")
} }
config.mapEnv[level].setKey(parts, key)
}
p1 = splitted[count] var p0, p1, p2, p3, p4, p5 string
p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_") p0 = strings.Trim(strings.Join(splitted[:count], "_"), "_")
if len(config.mapMapEnv[p0][p1]) == 0 { p1 = strings.Trim(strings.Join(splitted[count:], "_"), "_")
config.mapMapEnv[p0][p1] = make(map[string]string) if len(config.mapEnv[p0]) == 0 {
config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string) config.mapEnv[p0] = make(map[string]string)
config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string) config.mapMapEnv[p0] = make(map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string) config.mapMapMapEnv[p0] = make(map[string]map[string]map[string]string)
} config.mapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]string)
if p0 != "" && p1 != "" && p2 != "" { config.mapMapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]map[string]string)
config.mapMapEnv[p0][p1][p2] = key }
} if p0 != "" && p1 != "" {
config.mapEnv[p0][p1] = key
}
p2 = splitted[count+1] p1 = splitted[count]
p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_") p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_")
if len(config.mapMapMapEnv[p0][p1][p2]) == 0 { if len(config.mapMapEnv[p0][p1]) == 0 {
config.mapMapMapEnv[p0][p1][p2] = make(map[string]string) config.mapMapEnv[p0][p1] = make(map[string]string)
config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string) config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string) config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string)
} config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string)
if p0 != "" && p1 != "" && p2 != "" && p3 != "" { }
config.mapMapMapEnv[p0][p1][p2][p3] = key if p0 != "" && p1 != "" && p2 != "" {
} config.mapMapEnv[p0][p1][p2] = key
}
p3 = splitted[count+2] p2 = splitted[count+1]
p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_") p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 { if len(config.mapMapMapEnv[p0][p1][p2]) == 0 {
config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string) config.mapMapMapEnv[p0][p1][p2] = make(map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string) config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string)
} config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string)
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" { }
config.mapMapMapMapEnv[p0][p1][p2][p3][p4] = key if p0 != "" && p1 != "" && p2 != "" && p3 != "" {
} config.mapMapMapEnv[p0][p1][p2][p3] = key
}
p4 = splitted[count+3] p3 = splitted[count+2]
p5 = strings.Trim(strings.Join(splitted[count+4:], "_"), "_") p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_")
if len(config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4]) == 0 { if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4] = make(map[string]string) config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string)
} config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string)
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" && p5 != "" { }
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4][p5] = key if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" {
} config.mapMapMapMapEnv[p0][p1][p2][p3][p4] = key
}
p4 = splitted[count+3]
p5 = strings.Trim(strings.Join(splitted[count+4:], "_"), "_")
if len(config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4]) == 0 {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4] = make(map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" && p5 != "" {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4][p5] = key
} }
} }
} }
} }
} }
} }

View File

@ -19,4 +19,11 @@ cd ${dir}/buildenv
go mod init src go mod init src
go mod tidy go mod tidy
package_name=$(ls ${dir}/buildenv/deps/pkg/mod/git.purser.it/roypur | tr -d "\n")
package_dir="${dir}/buildenv/deps/pkg/mod/git.purser.it/roypur/${package_name}"
chmod -R 777 ${package_dir}
rm ${package_dir}/*.go
cp $(dirname ${dir})/*.go ${package_dir}
go build -o "${dir}/bin/test" "src/main.go" go build -o "${dir}/bin/test" "src/main.go"