Skip to content

Commit

Permalink
modify to make EnvKV reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Dec 5, 2024
1 parent d423cc1 commit 9f38e1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions realy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,16 @@ func (kv KVSlice) Composit(kv2 KVSlice) (out KVSlice) {
return
}

func EnvKV(cfg *C) (m KVSlice) {
t := reflect.TypeOf(*cfg)
// EnvKV turns a struct with `env` keys (used with go-simpler/env) into a standard formatted
// environment variable key/value pair list, one per line. Note you must dereference a pointer
// type to use this. This allows the composition of the config in this file with an extended
// form with a customized variant of realy to produce correct environment variables both read
// and write.
func EnvKV(cfg any) (m KVSlice) {
t := reflect.TypeOf(cfg)
for i := 0; i < t.NumField(); i++ {
k := t.Field(i).Tag.Get("env")
v := reflect.ValueOf(*cfg).Field(i).Interface()
v := reflect.ValueOf(cfg).Field(i).Interface()
var val st
switch v.(type) {
case string:
Expand All @@ -129,13 +134,17 @@ func EnvKV(cfg *C) (m KVSlice) {
val = strings.Join(arr, ",")
}
}
// this can happen with embedded structs
if k == "" {
continue
}
m = append(m, KV{k, val})
}
return
}

func PrintEnv(cfg *C, printer io.Writer) {
for _, v := range EnvKV(cfg) {
for _, v := range EnvKV(*cfg) {
_, _ = fmt.Fprintf(printer, "%s=%s\n", v.Key, v.Value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion realy/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.34
v1.2.35

0 comments on commit 9f38e1c

Please sign in to comment.