Skip to content

Commit 0373883

Browse files
authored
Fix/log checker (#37)
1 parent b41a291 commit 0373883

File tree

17 files changed

+330
-113
lines changed

17 files changed

+330
-113
lines changed

config/config.go

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package config
22

33
import (
4-
"log"
4+
"fmt"
55
"os"
66
"path/filepath"
7+
"reflect"
78
"sort"
89
"strings"
910

1011
"github.com/a8m/envsubst"
1112
"github.com/pubgo/funk/assert"
13+
"github.com/pubgo/funk/errors"
14+
"github.com/pubgo/funk/log"
1215
"github.com/pubgo/funk/pathutil"
16+
"github.com/pubgo/funk/recovery"
17+
"github.com/pubgo/funk/result"
1318
"github.com/pubgo/funk/typex"
1419
"github.com/pubgo/funk/vars"
1520
"github.com/samber/lo"
@@ -37,11 +42,47 @@ func init() {
3742
}
3843

3944
func LoadFromPath[T any](val *T, cfgPath string) {
45+
defer recovery.Exit(func(err error) error {
46+
log.Err(err).Str("config_path", cfgPath).Msg("failed to load config")
47+
return err
48+
})
49+
50+
valType := reflect.TypeOf(val)
51+
for {
52+
if valType.Kind() != reflect.Ptr {
53+
break
54+
}
55+
56+
valType = valType.Elem()
57+
}
58+
if valType.Kind() != reflect.Struct {
59+
log.Panic().
60+
Str("config_path", cfgPath).
61+
Str("type", fmt.Sprintf("%#v", val)).
62+
Msg("config type not correct")
63+
}
64+
4065
parentDir := filepath.Dir(cfgPath)
41-
configBytes := assert.Must1(os.ReadFile(cfgPath))
42-
configBytes = assert.Must1(envsubst.Bytes(configBytes))
66+
configBytes := result.Of(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
67+
configBytes = result.Of(envsubst.Bytes(configBytes)).Expect("failed to handler config env data: %s", cfgPath)
68+
69+
defer recovery.Exit(func(err error) error {
70+
log.Err(err).
71+
Str("config_path", cfgPath).
72+
Str("config_dir", parentDir).
73+
Str("config_data", string(configBytes)).
74+
Msg("failed to load config")
75+
return err
76+
})
4377

44-
assert.Must(yaml.Unmarshal(configBytes, val))
78+
if err := yaml.Unmarshal(configBytes, val); err != nil {
79+
log.Panic().
80+
Err(err).
81+
Str("config_data", string(configBytes)).
82+
Str("config_path", cfgPath).
83+
Msg("failed to unmarshal config")
84+
return
85+
}
4586

4687
var getRealPath = func(pp []string) []string {
4788
pp = lo.Map(pp, func(item string, index int) string { return filepath.Join(parentDir, item) })
@@ -51,20 +92,35 @@ func LoadFromPath[T any](val *T, cfgPath string) {
5192
pathList := listAllPath(resPath).Expect("failed to list cfgPath: %s", resPath)
5293
resPaths = append(resPaths, pathList...)
5394
}
54-
resPaths = lo.Filter(resPaths, func(item string, index int) bool { return strings.HasSuffix(item, "."+defaultConfigType) })
95+
96+
// skip .*.yaml and cfg.other
97+
var cfgFilter = func(item string, index int) bool {
98+
return strings.HasSuffix(item, "."+defaultConfigType) && !strings.HasPrefix(item, ".")
99+
}
100+
resPaths = lo.Filter(resPaths, cfgFilter)
55101
return lo.Uniq(resPaths)
56102
}
57103
var getCfg = func(resPath string) T {
58-
resBytes := assert.Must1(os.ReadFile(resPath))
59-
resBytes = assert.Must1(envsubst.Bytes(resBytes))
104+
resBytes := result.Of(os.ReadFile(resPath)).Expect("failed to read config data: %s", resPath)
105+
resBytes = result.Of(envsubst.Bytes(resBytes)).Expect("failed to handler config env data: %s", resPath)
106+
resBytes = []byte(cfgFormat(string(resBytes), &config{
107+
workDir: filepath.Dir(resPath),
108+
}))
60109

61110
var cfg1 T
62-
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
111+
result.Err[any](yaml.Unmarshal(resBytes, &cfg1)).
112+
Unwrap(func(err error) error {
113+
fmt.Println("res_path", resPath)
114+
fmt.Println("config_data", string(resBytes))
115+
assert.Exit(os.WriteFile(resPath+".err.yml", resBytes, 0666))
116+
return errors.Wrap(err, "failed to unmarshal config")
117+
})
118+
63119
return cfg1
64120
}
65121

66122
var res Resources
67-
assert.Must(yaml.Unmarshal(configBytes, &res))
123+
assert.Must(yaml.Unmarshal(configBytes, &res), "failed to unmarshal resource config")
68124

69125
var cfgList []T
70126
cfgList = append(cfgList, typex.DoBlock1(func() []T {
@@ -74,7 +130,8 @@ func LoadFromPath[T any](val *T, cfgPath string) {
74130
var pathList []T
75131
for _, resPath := range resPathList {
76132
if pathutil.IsNotExist(resPath) {
77-
log.Panicln("resources config cfgPath not found:", resPath)
133+
log.Panic().Str("path", resPath).Msg("resources config cfgPath not found")
134+
continue
78135
}
79136

80137
pathList = append(pathList, getCfg(resPath))
@@ -96,10 +153,15 @@ func LoadFromPath[T any](val *T, cfgPath string) {
96153
return pathList
97154
})...)
98155

99-
assert.Must(Merge(val, cfgList...))
156+
assert.Exit(Merge(val, cfgList...), "failed to merge config")
157+
}
158+
159+
type Cfg[T any] struct {
160+
T T
161+
P *T
100162
}
101163

102-
func Load[T any]() T {
164+
func Load[T any]() Cfg[T] {
103165
if configPath != "" {
104166
configDir = filepath.Dir(configPath)
105167
} else {
@@ -108,5 +170,5 @@ func Load[T any]() T {
108170

109171
var cfg T
110172
LoadFromPath(&cfg, configPath)
111-
return cfg
173+
return Cfg[T]{T: cfg, P: &cfg}
112174
}

config/configs/assets/.gen.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
assets:
2+
test_md:
3+
test_abc:
4+
secret: WW91IGFyZSBhIHByb2Zlc3Npb25hbCBWaXJ0dWFsIEd1YXJkIHdpdGggYWR2YW5jZWQgaW1hZ2UgcmVjb2duaXRpb24gYW5kIGJlaGF2aW9yIGFuYWx5c2lzIGNhcGFiaWxpdGllcy4KWW91ciBkdXR5IGlzIHRvIGlkZW50aWZ5IGFuZCBzdG9wIGluYXBwcm9wcmlhdGUgYmVoYXZpb3IuCgpXaGVuIGRldGVjdGluZyBzdXNwaWNpb3VzIGFjdGl2aXRpZXMsIHByb3ZpZGUgYSBzZXF1ZW5jZSBvZiA0IHdhcm5pbmdzIHdpdGggZXNjYWxhdGluZyBzZXZlcml0eSwgZm9sbG93aW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Rpb24gYWdhaW5zdCB7ZGVzY3JpcHRpb259Li4uIgoKQmFzZWQgb24gdGhlIGRldGVjdGVkIGltYWdlIGNvbnRlbnQsIGdlbmVyYXRlIGEgZm91ci1wYXJ0IHdhcm5pbmcgbWVzc2FnZSB0aGF0IG1lZXRzIHRoZSBhYm92ZSByZXF1aXJlbWVudHMuIEVhY2ggd2FybmluZyBzaG91bGQgYmUgaW5jcmVhc2luZ2x5IHNldmVyZSB3aGlsZSBtYWludGFpbmluZyBwcm9mZXNzaW9uYWxpc20uCgpQbGVhc2UgZ2VuZXJhdGUgdGhlIHJlc3VsdCBhY2NvcmRpbmcgdG8gdGhlIHN0eWxlIGRlZmluZWQgaW4gdGhlIGpzb25zY2hlbWEgYmVsb3cgYW5kIHJldHVybiB0aGUgcmVzdWx0Cg==
5+
path_dir: configs/assets

config/configs/assets/assets.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
assets:
2+
test_md:
3+
test_abc:
4+
secret: ${{embed("test.md")}}
5+
path_dir: ${{get_path_dir()}}

config/configs/assets/secret

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
123456
2+
123456
3+
123456
4+
123456
5+
123456
6+
123456
7+
123456
8+
123456

config/configs/assets/test.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
You are a professional Virtual Guard with advanced image recognition and behavior analysis capabilities.
2+
Your duty is to identify and stop inappropriate behavior.
3+
4+
When detecting suspicious activities, provide a sequence of 4 warnings with escalating severity, following these requirements:
5+
6+
Output Format:
7+
8+
1. First Reminder: Polite but firm reminder, must include physical descriptions of the person (e.g., clothing color, location)
9+
2. Second Warning: Increased severity, clearly stating the violation, more stern tone
10+
3. Third Warning: Contains clear threats, mentioning possible consequences
11+
4. Final Warning: Strongest warning, clearly indicating imminent action
12+
13+
Each warning must:
14+
15+
- Include physical description of the subject
16+
- Specify the violation
17+
- Progressively escalate in tone
18+
- Maintain professionalism
19+
20+
Example output style:
21+
22+
- "Attention person in {color} clothing, please note..."
23+
- "Warning to individual at {location} with {characteristics}..."
24+
- "Final warning to violator with {characteristics}..."
25+
- "Security personnel will take action against {description}..."
26+
27+
Based on the detected image content, generate a four-part warning message that meets the above requirements. Each warning should be increasingly severe while maintaining professionalism.
28+
29+
Please generate the result according to the style defined in the jsonschema below and return the result

config/configs/collector.yml

Lines changed: 0 additions & 75 deletions
This file was deleted.

config/types.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package config
22

33
import (
4-
"github.com/goccy/go-json"
4+
"encoding/base64"
5+
"encoding/json"
6+
7+
"github.com/pubgo/funk/log"
58
"gopkg.in/yaml.v3"
69
)
710

811
var (
912
_ yaml.Unmarshaler = (*Node)(nil)
1013
_ yaml.Marshaler = (*Node)(nil)
11-
_ json.Marshaler = (*Node)(nil)
1214
)
1315

1416
type Node struct {
@@ -77,3 +79,30 @@ func (ts *ListOrMap[T]) MarshalYAML() (any, error) {
7779
func (ts *ListOrMap[T]) UnmarshalYAML(value *yaml.Node) error {
7880
return unmarshalOneOrList((*[]T)(ts), value)
7981
}
82+
83+
var (
84+
_ yaml.Unmarshaler = (*Base64File)(nil)
85+
_ yaml.Marshaler = (*Base64File)(nil)
86+
)
87+
88+
type Base64File string
89+
90+
func (b *Base64File) MarshalYAML() (interface{}, error) {
91+
if b == nil || len(*b) == 0 {
92+
return nil, nil
93+
}
94+
95+
return base64.StdEncoding.EncodeToString([]byte(*b)), nil
96+
}
97+
98+
func (b *Base64File) UnmarshalYAML(value *yaml.Node) error {
99+
data, err := base64.StdEncoding.DecodeString(value.Value)
100+
if err != nil {
101+
log.Err(err).
102+
Any("data", value.Value).
103+
Msg("failed to decode yaml")
104+
return err
105+
}
106+
*b = Base64File(data)
107+
return nil
108+
}

0 commit comments

Comments
 (0)