Skip to content

Commit 51ec515

Browse files
committed
chore: quick update fix/version at 2025-09-24 11:45:48
1 parent 34a5ee7 commit 51ec515

File tree

11 files changed

+87
-60
lines changed

11 files changed

+87
-60
lines changed

config/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,22 @@ func loadEnvConfigMap(cfgPath string) EnvSpecMap {
8383

8484
pathList := listAllPath(envPath).Expect("failed to list env config path: %s", envPath)
8585
for _, p := range pathList {
86-
envConfigBytes := result.Wrap(os.ReadFile(p)).Expect("failed to handler env config data, path=%s", p)
87-
envConfigBytes = bytes.TrimSpace(envConfigBytes)
86+
envConfigBytes := result.Wrap(os.ReadFile(p)).
87+
Map(bytes.TrimSpace).
88+
Must(func(e *zerolog.Event) {
89+
e.Str(logfields.Msg, fmt.Sprintf("failed to handler env config data, path=%s", p))
90+
})
8891
if len(envConfigBytes) == 0 {
8992
continue
9093
}
9194

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)
95+
envConfigBytes = cfgFormat(envConfigBytes, &config{workDir: filepath.Dir(cfgPath)})
96+
envConfigBytes = result.Wrap(envsubst.Bytes(envConfigBytes)).Must(func(e *zerolog.Event) {
97+
e.Str(logfields.Msg, fmt.Sprintf("failed to handler config env data: %s", envConfigBytes))
98+
})
99+
result.Must(yaml.Unmarshal(envConfigBytes, &envSpecMap), func(e *zerolog.Event) {
100+
e.Str(logfields.Msg, fmt.Sprintf("failed to unmarshal env config, data=%s path=%s", envConfigBytes, p))
101+
})
94102
}
95103
}
96104
initEnv(envSpecMap)

config/envs.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55

66
"github.com/pubgo/funk/env"
7-
"github.com/samber/lo"
7+
"github.com/pubgo/funk/strutil"
88
)
99

1010
type EnvSpecMap map[string]*EnvSpec
@@ -13,16 +13,14 @@ type EnvSpec struct {
1313
Name string `yaml:"name"`
1414
Description string `yaml:"description"`
1515
Default string `yaml:"default"`
16+
Value string `yaml:"value"`
1617
Required bool `yaml:"required"`
1718
Example string `yaml:"example"`
18-
Versions string `yaml:"versions"`
19-
Tags string `yaml:"tags"`
2019
}
2120

2221
func initEnv(envMap EnvSpecMap) {
2322
for name, cfg := range envMap {
24-
envData := env.Get(name)
25-
envData = strings.TrimSpace(lo.Ternary(envData != "", envData, cfg.Default))
23+
envData := strings.TrimSpace(strutil.FirstNotEmpty(env.Get(name), cfg.Value, cfg.Default))
2624
if cfg.Required && envData == "" {
2725
panic("env " + cfg.Name + " is required")
2826
}

env/env.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99

1010
"github.com/a8m/envsubst"
1111
"github.com/joho/godotenv"
12+
"github.com/samber/lo"
13+
1214
"github.com/pubgo/funk/assert"
1315
"github.com/pubgo/funk/pathutil"
1416
"github.com/pubgo/funk/v2/result"
15-
"github.com/samber/lo"
1617
)
1718

1819
func Set(key, value string) result.Error {
@@ -120,7 +121,7 @@ func Key(key string) string {
120121

121122
func LoadFiles(files ...string) (r result.Error) {
122123
files = lo.Filter(files, func(item string, index int) bool { return pathutil.IsExist(item) })
123-
if result.Catch(&r, godotenv.Load(files...)) {
124+
if result.Catch(&r, godotenv.Overload(files...)) {
124125
return
125126
}
126127

env/reload.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package env
22

33
import (
4+
"fmt"
5+
"log/slog"
46
"os"
57
"strings"
68

7-
"github.com/rs/zerolog"
8-
"github.com/rs/zerolog/log"
9+
"github.com/pubgo/funk/log/logfields"
910
)
1011

11-
func init() {
12-
loadEnv()
13-
}
14-
1512
func Reload() {
1613
loadEnv()
1714
}
@@ -26,16 +23,17 @@ func Init() {
2623
// a-b=>a_b, a.b=>a_b, a/b=>a_b
2724
func loadEnv() {
2825
envPrefix := getEnvPrefix()
29-
logger := log.With().Str("operation", "reload_env").Logger()
30-
envPrefixEventFn := func(e *zerolog.Event) {
31-
e.Dict("env_prefix", zerolog.Dict().Str("key", PrefixKey).Str("value", envPrefix))
32-
}
33-
logRecord(logger.Info(), envPrefixEventFn).Msg("reload env")
26+
27+
logger := slog.With(
28+
slog.String(logfields.Module, "env"),
29+
slog.String(logfields.Operation, "reload_env"),
30+
)
31+
logger.Info("reload env", slog.Any("env_prefix", map[string]any{"key": PrefixKey, "value": envPrefix}))
3432

3533
for _, env := range os.Environ() {
3634
kvs := strings.SplitN(env, "=", 2)
3735
if len(kvs) != 2 {
38-
logRecord(logger.Error()).Msg("split env error")
36+
logger.Error("split env error")
3937
continue
4038
}
4139

@@ -44,7 +42,7 @@ func loadEnv() {
4442
strings.HasPrefix(rawEnvKey, "_") ||
4543
strings.HasPrefix(rawEnvKey, "=") ||
4644
!hasEnvPrefix(rawEnvKey, envPrefix) {
47-
logRecord(logger.Warn()).Msgf("unset env, key=%s", rawEnvKey)
45+
logger.Warn(fmt.Sprintf("unset env, key=%s", rawEnvKey))
4846
_ = os.Unsetenv(rawEnvKey)
4947
continue
5048
}
@@ -56,7 +54,7 @@ func loadEnv() {
5654
}
5755

5856
setOk := os.Setenv(key, kvs[1]) == nil
59-
logRecord(logger.Info()).Msgf("reset env, old_key=%s new_key=%s set_ok=%v", rawEnvKey, key, setOk)
57+
logger.Info(fmt.Sprintf("reset env, old_key=%s new_key=%s set_ok=%v", rawEnvKey, key, setOk))
6058
} else {
6159
_ = os.Unsetenv(rawEnvKey)
6260
}

env/util.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@ import (
55
"strings"
66

77
"github.com/ettle/strcase"
8-
"github.com/rs/zerolog"
9-
10-
"github.com/pubgo/funk/log/logfields"
11-
"github.com/pubgo/funk/log/logutil"
128
)
139

14-
var logFn = func(e *zerolog.Event) {
15-
e.Str(logfields.Module, "env")
16-
}
17-
1810
const PrefixKey = "ENV_PREFIX"
1911

2012
func hasEnvPrefix(key string, prefix string) bool {
@@ -58,7 +50,3 @@ func Normalize(key string) (string, bool) {
5850

5951
return KeyHandler(key), true
6052
}
61-
62-
func logRecord(evt *zerolog.Event, funcs ...func(e *zerolog.Event)) *zerolog.Event {
63-
return logutil.Record(evt, append(funcs, logFn)...)
64-
}

env/util_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package env_test
22

33
import (
4+
"log/slog"
45
"os"
5-
"strings"
66
"testing"
77

88
"github.com/pubgo/funk/env"
9+
"github.com/pubgo/funk/log"
910
"github.com/pubgo/funk/pretty"
10-
"github.com/rs/zerolog"
11-
"github.com/rs/zerolog/log"
1211
"github.com/samber/lo"
1312
"github.com/stretchr/testify/assert"
1413
)
1514

1615
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"))
16+
assert.NoError(t, os.Setenv("abc", "1"))
17+
assert.Equal(t, os.Getenv("abc"), "1")
18+
assert.NoError(t, os.Setenv("abc", "2"))
19+
assert.Equal(t, os.Getenv("abc"), "2")
2120
}
2221

2322
func TestNormalize(t *testing.T) {
@@ -27,11 +26,7 @@ func TestNormalize(t *testing.T) {
2726
}
2827

2928
func TestEnvPrefix(t *testing.T) {
30-
log.Logger = log.Hook(zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, message string) {
31-
if strings.HasPrefix(message, "unset not match env") {
32-
e.Discard()
33-
}
34-
}))
29+
slog.SetDefault(slog.New(log.NewSlog(log.GetLogger(""))))
3530

3631
env.Reload()
3732
pretty.Println("env_keys", lo.Keys(env.Map()))

log/impl.slog.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log/slog"
66

7+
"github.com/pubgo/funk/log/slogutil"
78
"github.com/rs/zerolog"
89
slogcommon "github.com/samber/slog-common"
910
)
@@ -56,7 +57,11 @@ func (s slogImpl) Handle(ctx context.Context, r slog.Record) error {
5657
}
5758

5859
r.Attrs(func(attr slog.Attr) bool {
59-
evt.Any(attr.Key, attr.Value.Any())
60+
if fn, ok := attr.Value.Any().(slogutil.LogFunc); ok {
61+
evt.Func(fn)
62+
} else {
63+
evt.Any(attr.Key, attr.Value.Any())
64+
}
6065
return true
6166
})
6267

log/logfields/fields.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package logfields
22

33
const (
4-
Module = "module"
5-
Msg = "log_msg"
6-
Logger = "logger"
4+
Module = "module"
5+
Operation = "operation"
6+
Action = "action"
7+
Msg = "log_msg"
8+
Logger = "logger"
9+
710
Error = "error"
811
ErrorDetail = "error_detail"
912
ErrorStack = "error_stack"

log/slogutil/util.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package slogutil
2+
3+
import (
4+
"log/slog"
5+
6+
"github.com/rs/zerolog"
7+
)
8+
9+
type LogFunc func(evt *zerolog.Event)
10+
11+
func Func(fn LogFunc) slog.Attr {
12+
return slog.Any("func", fn)
13+
}

log/z_slog_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package log_test
2+
3+
import (
4+
"log/slog"
5+
"testing"
6+
7+
"github.com/pubgo/funk/log"
8+
"github.com/pubgo/funk/log/slogutil"
9+
"github.com/rs/zerolog"
10+
)
11+
12+
func TestSlog(t *testing.T) {
13+
slog.SetDefault(slog.New(log.NewSlog(log.GetLogger(""))))
14+
slog.Info("ok")
15+
slog.Info("ok", slogutil.Func(func(evt *zerolog.Event) {
16+
evt.Str("record", "ok")
17+
}))
18+
}

0 commit comments

Comments
 (0)