Skip to content

Commit 34a5ee7

Browse files
committed
Merge branch 'fix/version' of github.com:pubgo/funk into fix/version
2 parents 5f3032e + db141c1 commit 34a5ee7

File tree

8 files changed

+73
-55
lines changed

8 files changed

+73
-55
lines changed

config/aaa.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ type NamedConfig interface {
66
}
77

88
type Resources struct {
9-
// Resources resource config file must exist
9+
// Resources resource config file or dir must exist
1010
Resources []string `yaml:"resources"`
1111

12-
// PatchResources resource config not required to exist
12+
// PatchResources resource config file or dir not required to exist
1313
PatchResources []string `yaml:"patch_resources"`
1414

15-
// PatchEnvs config file or path, not required to exist
15+
// PatchEnvs env config file or dir not required to exist
1616
PatchEnvs []string `yaml:"patch_envs"`
1717
}

config/config.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ import (
1010
"strings"
1111

1212
"github.com/a8m/envsubst"
13+
"github.com/rs/zerolog"
1314
"github.com/samber/lo"
1415
"gopkg.in/yaml.v3"
1516

1617
"github.com/pubgo/funk/assert"
17-
"github.com/pubgo/funk/errors"
1818
"github.com/pubgo/funk/log"
19+
"github.com/pubgo/funk/log/logfields"
1920
"github.com/pubgo/funk/pathutil"
2021
"github.com/pubgo/funk/pretty"
2122
"github.com/pubgo/funk/recovery"
22-
"github.com/pubgo/funk/result"
2323
"github.com/pubgo/funk/typex"
24+
"github.com/pubgo/funk/v2/result"
2425
"github.com/pubgo/funk/vars"
2526
)
2627

@@ -48,49 +49,55 @@ func init() {
4849

4950
func GetConfigData(cfgPath string) (_ []byte, gErr error) {
5051
var configBytes []byte
51-
defer recovery.Err(&gErr, func(err error) error {
52+
defer result.RecoveryErr(&gErr, func(err error) error {
5253
log.Err(err).Str("config_path", cfgPath).Msgf("config: %s", configBytes)
5354
return err
5455
})
5556

56-
configBytes = result.Of(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
57+
configBytes = result.Wrap(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
5758
configBytes = cfgFormat(configBytes, &config{workDir: filepath.Dir(cfgPath)})
58-
configBytes = result.Of(envsubst.Bytes(configBytes)).Expect("failed to handler config env data: %s", cfgPath)
59+
configBytes = result.Wrap(envsubst.Bytes(configBytes)).Expect("failed to handler config env data: %s", cfgPath)
5960
return configBytes, nil
6061
}
6162

62-
func LoadEnvConfigMap(cfgPath string) EnvConfigMap { return loadEnvConfigMap(cfgPath) }
63+
func LoadEnvConfigMap(cfgPath string) EnvSpecMap { return loadEnvConfigMap(cfgPath) }
64+
65+
func loadEnvConfigMap(cfgPath string) EnvSpecMap {
66+
defer recovery.Exit(func(err error) error {
67+
log.Err(err).Str("path", cfgPath).Msg("load env config map error")
68+
return err
69+
})
6370

64-
func loadEnvConfigMap(cfgPath string) EnvConfigMap {
6571
var res Resources
66-
configBytes := result.Of(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
72+
configBytes := result.Wrap(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
6773
assert.Must(yaml.Unmarshal(configBytes, &res), "failed to unmarshal resource config")
6874

6975
parentDir := filepath.Dir(cfgPath)
70-
var envCfgMap EnvConfigMap
76+
var envSpecMap EnvSpecMap
7177
for _, envPath := range res.PatchEnvs {
7278
envPath = filepath.Join(parentDir, envPath)
7379
if pathutil.IsNotExist(envPath) {
74-
log.Warn().Str("env_path", envPath).Msg("env config cfgPath not found")
80+
log.Warn().Str("env_path", envPath).Msg("env config path not found")
7581
continue
7682
}
7783

78-
pathList := listAllPath(envPath).Expect("failed to list envPath: %s", envPath)
84+
pathList := listAllPath(envPath).Expect("failed to list env config path: %s", envPath)
7985
for _, p := range pathList {
80-
envConfigBytes := result.Of(os.ReadFile(p)).Expect("failed to handler env config data, path=%s", p)
86+
envConfigBytes := result.Wrap(os.ReadFile(p)).Expect("failed to handler env config data, path=%s", p)
8187
envConfigBytes = bytes.TrimSpace(envConfigBytes)
8288
if len(envConfigBytes) == 0 {
8389
continue
8490
}
8591

86-
assert.MustF(yaml.Unmarshal(envConfigBytes, &envCfgMap), "failed to unmarshal env config, data=%s path=%s", envConfigBytes, p)
92+
envConfigBytes = result.Wrap(envsubst.Bytes(envConfigBytes)).Expect("failed to handler config env data: %s", envConfigBytes)
93+
assert.MustF(yaml.Unmarshal(envConfigBytes, &envSpecMap), "failed to unmarshal env config, data=%s path=%s", envConfigBytes, p)
8794
}
8895
}
89-
initEnv(envCfgMap)
90-
return envCfgMap
96+
initEnv(envSpecMap)
97+
return envSpecMap
9198
}
9299

93-
func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
100+
func LoadFromPath[T any](val *T, cfgPath string) EnvSpecMap {
94101
defer recovery.Exit(func(err error) error {
95102
log.Err(err).Str("config_path", cfgPath).Msg("failed to load config")
96103
return err
@@ -113,7 +120,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
113120

114121
var envCfgMap = loadEnvConfigMap(cfgPath)
115122

116-
configBytes := result.Of(GetConfigData(cfgPath)).Expect("failed to handler config data")
123+
configBytes := result.Wrap(GetConfigData(cfgPath)).Expect("failed to handler config data")
117124
defer recovery.Exit(func(err error) error {
118125
log.Err(err).
119126
Str("config_path", cfgPath).
@@ -149,16 +156,15 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
149156
return lo.Uniq(resPaths)
150157
}
151158
getCfg := func(resPath string) T {
152-
resBytes := result.Of(GetConfigData(resPath)).Expect("failed to handler config data")
159+
resBytes := result.Wrap(GetConfigData(resPath)).Expect("failed to handler config data")
153160

154161
var cfg1 T
155-
result.Err[any](yaml.Unmarshal(resBytes, &cfg1)).
156-
Unwrap(func(err error) error {
157-
fmt.Println("res_path", resPath)
158-
fmt.Println("config_data", string(resBytes))
159-
assert.Exit(os.WriteFile(resPath+".err.yml", resBytes, 0666))
160-
return errors.Wrap(err, "failed to unmarshal config")
161-
})
162+
result.ErrOf(yaml.Unmarshal(resBytes, &cfg1)).Must(func(e *zerolog.Event) {
163+
fmt.Println("res_path", resPath)
164+
fmt.Println("config_data", string(resBytes))
165+
assert.Exit(os.WriteFile(resPath+".err.yml", resBytes, 0666))
166+
e.Str(logfields.Msg, "failed to unmarshal config")
167+
})
162168

163169
return cfg1
164170
}
@@ -210,7 +216,7 @@ func LoadFromPath[T any](val *T, cfgPath string) EnvConfigMap {
210216
type Cfg[T any] struct {
211217
T T
212218
P *T
213-
EnvCfg *EnvConfigMap
219+
EnvCfg *EnvSpecMap
214220
}
215221

216222
func Load[T any]() Cfg[T] {

config/configs/assets/assets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ assets:
22
test_md:
33
test_abc:
44
secret: ${{embed("test.md")}}
5-
path_dir: ${{path_dir()}}
5+
path_dir: ${{config_dir()}}

config/envs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/samber/lo"
88
)
99

10-
type EnvConfigMap map[string]*EnvConf
10+
type EnvSpecMap map[string]*EnvSpec
1111

12-
type EnvConf struct {
12+
type EnvSpec struct {
1313
Name string `yaml:"name"`
1414
Description string `yaml:"description"`
1515
Default string `yaml:"default"`
@@ -19,7 +19,7 @@ type EnvConf struct {
1919
Tags string `yaml:"tags"`
2020
}
2121

22-
func initEnv(envMap EnvConfigMap) {
22+
func initEnv(envMap EnvSpecMap) {
2323
for name, cfg := range envMap {
2424
envData := env.Get(name)
2525
envData = strings.TrimSpace(lo.Ternary(envData != "", envData, cfg.Default))

config/util.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/pubgo/funk/errors"
2323
"github.com/pubgo/funk/log"
2424
"github.com/pubgo/funk/pathutil"
25-
"github.com/pubgo/funk/result"
25+
"github.com/pubgo/funk/v2/result"
2626
)
2727

2828
func GetConfigDir() string {
@@ -81,16 +81,16 @@ func SetConfigPath(confPath string) {
8181
configPath = confPath
8282
}
8383

84-
func MergeR[A any, B any | *any](dst *A, src ...B) (ret result.Result[*A]) {
84+
func MergeR[A any, B any | *any](dst *A, src ...B) (r result.Result[*A]) {
8585
if len(src) == 0 {
86-
return ret.WithVal(dst)
86+
return r.WithValue(dst)
8787
}
8888

8989
err := Merge(dst, src...)
9090
if err != nil {
91-
return ret.WithErr(err)
91+
return r.WithErr(err)
9292
}
93-
return ret.WithVal(dst)
93+
return r.WithValue(dst)
9494
}
9595

9696
func Merge[A any, B any | *any](dst *A, src ...B) error {
@@ -182,7 +182,7 @@ func unmarshalOneOrList[T any](list *[]T, value *yaml.Node) error {
182182

183183
func listAllPath(dirOrPath string) (ret result.Result[[]string]) {
184184
if !pathutil.IsDir(dirOrPath) {
185-
return ret.WithVal([]string{dirOrPath})
185+
return ret.WithValue([]string{dirOrPath})
186186
}
187187

188188
var paths []string
@@ -202,7 +202,7 @@ func listAllPath(dirOrPath string) (ret result.Result[[]string]) {
202202
if err != nil {
203203
return ret.WithErr(err)
204204
}
205-
return ret.WithVal(paths)
205+
return ret.WithValue(paths)
206206
}
207207

208208
func makeList(typ reflect.Type, data []reflect.Value) reflect.Value {
@@ -229,7 +229,7 @@ func getEnvData(cfg *config) map[string]any {
229229
"get_path_dir": func() string {
230230
return cfg.workDir
231231
},
232-
"path_dir": func() string {
232+
"config_dir": func() string {
233233
return cfg.workDir
234234
},
235235
"embed": func(name string) string {

config/util_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ func TestEnv(t *testing.T) {
5353
data, err = envsubst.String("${hello:-abc}")
5454
assert.Nil(t, err)
5555
assert.Equal(t, data, "abc")
56-
57-
data, err = envsubst.String("${{hello:-abc}}")
58-
assert.Nil(t, err)
59-
assert.Equal(t, data, "${{hello:-abc}}")
6056
}
6157

6258
func TestConfigPath(t *testing.T) {

env/reload.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/rs/zerolog/log"
99
)
1010

11+
func init() {
12+
loadEnv()
13+
}
14+
1115
func Reload() {
1216
loadEnv()
1317
}
@@ -35,21 +39,26 @@ func loadEnv() {
3539
continue
3640
}
3741

38-
envKey := trim(kvs[0])
39-
_ = os.Unsetenv(envKey)
40-
41-
if envKey == "" ||
42-
strings.HasPrefix(envKey, "_") ||
43-
strings.HasPrefix(envKey, "=") ||
44-
!hasEnvPrefix(envKey, envPrefix) {
45-
logRecord(logger.Warn()).Msgf("unset env, key=%s", envKey)
42+
rawEnvKey := trim(kvs[0])
43+
if rawEnvKey == "" ||
44+
strings.HasPrefix(rawEnvKey, "_") ||
45+
strings.HasPrefix(rawEnvKey, "=") ||
46+
!hasEnvPrefix(rawEnvKey, envPrefix) {
47+
logRecord(logger.Warn()).Msgf("unset env, key=%s", rawEnvKey)
48+
_ = os.Unsetenv(rawEnvKey)
4649
continue
4750
}
4851

49-
key, ok := Normalize(envKey)
52+
key, ok := Normalize(rawEnvKey)
5053
if ok {
54+
if key == rawEnvKey {
55+
continue
56+
}
57+
5158
setOk := os.Setenv(key, kvs[1]) == nil
52-
logRecord(logger.Info()).Msgf("reset env, old_key=%s new_key=%s set_ok=%v", envKey, key, setOk)
59+
logRecord(logger.Info()).Msgf("reset env, old_key=%s new_key=%s set_ok=%v", rawEnvKey, key, setOk)
60+
} else {
61+
_ = os.Unsetenv(rawEnvKey)
5362
}
5463
}
5564
}

env/util_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
)
1515

16+
func TestResetEnv(t *testing.T) {
17+
os.Setenv("abc", "1")
18+
t.Log(os.Getenv("abc"))
19+
os.Setenv("abc", "2")
20+
t.Log(os.Getenv("abc"))
21+
}
22+
1623
func TestNormalize(t *testing.T) {
1724
k, ok := env.Normalize("aA-bS3_AK/c.d")
1825
assert.True(t, ok)

0 commit comments

Comments
 (0)