Skip to content

Commit

Permalink
Fix/log checker (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee authored Dec 25, 2024
1 parent b41a291 commit 0373883
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 113 deletions.
88 changes: 75 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package config

import (
"log"
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"

"github.com/a8m/envsubst"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/log"
"github.com/pubgo/funk/pathutil"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/result"
"github.com/pubgo/funk/typex"
"github.com/pubgo/funk/vars"
"github.com/samber/lo"
Expand Down Expand Up @@ -37,11 +42,47 @@ func init() {
}

func LoadFromPath[T any](val *T, cfgPath string) {
defer recovery.Exit(func(err error) error {
log.Err(err).Str("config_path", cfgPath).Msg("failed to load config")
return err
})

valType := reflect.TypeOf(val)
for {
if valType.Kind() != reflect.Ptr {
break
}

valType = valType.Elem()
}
if valType.Kind() != reflect.Struct {
log.Panic().
Str("config_path", cfgPath).
Str("type", fmt.Sprintf("%#v", val)).
Msg("config type not correct")
}

parentDir := filepath.Dir(cfgPath)
configBytes := assert.Must1(os.ReadFile(cfgPath))
configBytes = assert.Must1(envsubst.Bytes(configBytes))
configBytes := result.Of(os.ReadFile(cfgPath)).Expect("failed to read config data: %s", cfgPath)
configBytes = result.Of(envsubst.Bytes(configBytes)).Expect("failed to handler config env data: %s", cfgPath)

defer recovery.Exit(func(err error) error {
log.Err(err).
Str("config_path", cfgPath).
Str("config_dir", parentDir).
Str("config_data", string(configBytes)).
Msg("failed to load config")
return err
})

assert.Must(yaml.Unmarshal(configBytes, val))
if err := yaml.Unmarshal(configBytes, val); err != nil {
log.Panic().
Err(err).
Str("config_data", string(configBytes)).
Str("config_path", cfgPath).
Msg("failed to unmarshal config")
return
}

var getRealPath = func(pp []string) []string {
pp = lo.Map(pp, func(item string, index int) string { return filepath.Join(parentDir, item) })
Expand All @@ -51,20 +92,35 @@ func LoadFromPath[T any](val *T, cfgPath string) {
pathList := listAllPath(resPath).Expect("failed to list cfgPath: %s", resPath)
resPaths = append(resPaths, pathList...)
}
resPaths = lo.Filter(resPaths, func(item string, index int) bool { return strings.HasSuffix(item, "."+defaultConfigType) })

// skip .*.yaml and cfg.other
var cfgFilter = func(item string, index int) bool {
return strings.HasSuffix(item, "."+defaultConfigType) && !strings.HasPrefix(item, ".")
}
resPaths = lo.Filter(resPaths, cfgFilter)
return lo.Uniq(resPaths)
}
var getCfg = func(resPath string) T {
resBytes := assert.Must1(os.ReadFile(resPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))
resBytes := result.Of(os.ReadFile(resPath)).Expect("failed to read config data: %s", resPath)
resBytes = result.Of(envsubst.Bytes(resBytes)).Expect("failed to handler config env data: %s", resPath)
resBytes = []byte(cfgFormat(string(resBytes), &config{
workDir: filepath.Dir(resPath),
}))

var cfg1 T
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
result.Err[any](yaml.Unmarshal(resBytes, &cfg1)).
Unwrap(func(err error) error {
fmt.Println("res_path", resPath)
fmt.Println("config_data", string(resBytes))
assert.Exit(os.WriteFile(resPath+".err.yml", resBytes, 0666))
return errors.Wrap(err, "failed to unmarshal config")
})

return cfg1
}

var res Resources
assert.Must(yaml.Unmarshal(configBytes, &res))
assert.Must(yaml.Unmarshal(configBytes, &res), "failed to unmarshal resource config")

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

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

assert.Must(Merge(val, cfgList...))
assert.Exit(Merge(val, cfgList...), "failed to merge config")
}

type Cfg[T any] struct {
T T
P *T
}

func Load[T any]() T {
func Load[T any]() Cfg[T] {
if configPath != "" {
configDir = filepath.Dir(configPath)
} else {
Expand All @@ -108,5 +170,5 @@ func Load[T any]() T {

var cfg T
LoadFromPath(&cfg, configPath)
return cfg
return Cfg[T]{T: cfg, P: &cfg}
}
5 changes: 5 additions & 0 deletions config/configs/assets/.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assets:
test_md:
test_abc:
secret: WW91IGFyZSBhIHByb2Zlc3Npb25hbCBWaXJ0dWFsIEd1YXJkIHdpdGggYWR2YW5jZWQgaW1hZ2UgcmVjb2duaXRpb24gYW5kIGJlaGF2aW9yIGFuYWx5c2lzIGNhcGFiaWxpdGllcy4KWW91ciBkdXR5IGlzIHRvIGlkZW50aWZ5IGFuZCBzdG9wIGluYXBwcm9wcmlhdGUgYmVoYXZpb3IuCgpXaGVuIGRldGVjdGluZyBzdXNwaWNpb3VzIGFjdGl2aXRpZXMsIHByb3ZpZGUgYSBzZXF1ZW5jZSBvZiA0IHdhcm5pbmdzIHdpdGggZXNjYWxhdGluZyBzZXZlcml0eSwgZm9sbG93aW5nIHRoZXNlIHJlcXVpcmVtZW50czoKCk91dHB1dCBGb3JtYXQ6CgoxLiBGaXJzdCBSZW1pbmRlcjogUG9saXRlIGJ1dCBmaXJtIHJlbWluZGVyLCBtdXN0IGluY2x1ZGUgcGh5c2ljYWwgZGVzY3JpcHRpb25zIG9mIHRoZSBwZXJzb24gKGUuZy4sIGNsb3RoaW5nIGNvbG9yLCBsb2NhdGlvbikKMi4gU2Vjb25kIFdhcm5pbmc6IEluY3JlYXNlZCBzZXZlcml0eSwgY2xlYXJseSBzdGF0aW5nIHRoZSB2aW9sYXRpb24sIG1vcmUgc3Rlcm4gdG9uZQozLiBUaGlyZCBXYXJuaW5nOiBDb250YWlucyBjbGVhciB0aHJlYXRzLCBtZW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Vsb3cgYW5kIHJldHVybiB0aGUgcmVzdWx0Cg==
path_dir: configs/assets
5 changes: 5 additions & 0 deletions config/configs/assets/assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
assets:
test_md:
test_abc:
secret: ${{embed("test.md")}}
path_dir: ${{get_path_dir()}}
8 changes: 8 additions & 0 deletions config/configs/assets/secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
123456
123456
123456
123456
123456
123456
123456
123456
29 changes: 29 additions & 0 deletions config/configs/assets/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
You are a professional Virtual Guard with advanced image recognition and behavior analysis capabilities.
Your duty is to identify and stop inappropriate behavior.

When detecting suspicious activities, provide a sequence of 4 warnings with escalating severity, following these requirements:

Output Format:

1. First Reminder: Polite but firm reminder, must include physical descriptions of the person (e.g., clothing color, location)
2. Second Warning: Increased severity, clearly stating the violation, more stern tone
3. Third Warning: Contains clear threats, mentioning possible consequences
4. Final Warning: Strongest warning, clearly indicating imminent action

Each warning must:

- Include physical description of the subject
- Specify the violation
- Progressively escalate in tone
- Maintain professionalism

Example output style:

- "Attention person in {color} clothing, please note..."
- "Warning to individual at {location} with {characteristics}..."
- "Final warning to violator with {characteristics}..."
- "Security personnel will take action against {description}..."

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.

Please generate the result according to the style defined in the jsonschema below and return the result
75 changes: 0 additions & 75 deletions config/configs/collector.yml

This file was deleted.

33 changes: 31 additions & 2 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package config

import (
"github.com/goccy/go-json"
"encoding/base64"
"encoding/json"

"github.com/pubgo/funk/log"
"gopkg.in/yaml.v3"
)

var (
_ yaml.Unmarshaler = (*Node)(nil)
_ yaml.Marshaler = (*Node)(nil)
_ json.Marshaler = (*Node)(nil)
)

type Node struct {
Expand Down Expand Up @@ -77,3 +79,30 @@ func (ts *ListOrMap[T]) MarshalYAML() (any, error) {
func (ts *ListOrMap[T]) UnmarshalYAML(value *yaml.Node) error {
return unmarshalOneOrList((*[]T)(ts), value)
}

var (
_ yaml.Unmarshaler = (*Base64File)(nil)
_ yaml.Marshaler = (*Base64File)(nil)
)

type Base64File string

func (b *Base64File) MarshalYAML() (interface{}, error) {
if b == nil || len(*b) == 0 {
return nil, nil
}

return base64.StdEncoding.EncodeToString([]byte(*b)), nil
}

func (b *Base64File) UnmarshalYAML(value *yaml.Node) error {
data, err := base64.StdEncoding.DecodeString(value.Value)
if err != nil {
log.Err(err).
Any("data", value.Value).
Msg("failed to decode yaml")
return err
}
*b = Base64File(data)
return nil
}
Loading

0 comments on commit 0373883

Please sign in to comment.