Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test4 #39

Merged
merged 61 commits into from
Nov 3, 2023
Merged
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
fad0d50
Compiled main.go and pushed changes
Hoshinonyaruko Oct 23, 2023
fda2572
test
Hoshinonyaruko Oct 23, 2023
34cfac1
适配了频道私聊,用bolt数据库取代ini
Hoshinonyaruko Oct 23, 2023
7fc1f10
适配了nonebot2
Hoshinonyaruko Oct 24, 2023
28f9a73
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo
Hoshinonyaruko Oct 24, 2023
9fadda4
适配私域
Hoshinonyaruko Oct 24, 2023
1336c6a
add license
Hoshinonyaruko Oct 24, 2023
b094949
add a lot
Hoshinonyaruko Oct 25, 2023
f4787f5
Resolved merge conflicts
Hoshinonyaruko Oct 25, 2023
86698c6
trss support
Hoshinonyaruko Oct 26, 2023
045f3e9
fix
Hoshinonyaruko Oct 26, 2023
618561c
add action
Hoshinonyaruko Oct 26, 2023
7e803aa
add action
Hoshinonyaruko Oct 26, 2023
ece4cef
add action
Hoshinonyaruko Oct 26, 2023
f864a50
fixbug
Hoshinonyaruko Oct 26, 2023
4564731
add wss
Hoshinonyaruko Oct 26, 2023
070e4df
merge
Hoshinonyaruko Oct 26, 2023
84e8fe9
bugfix
Hoshinonyaruko Oct 26, 2023
0e4fd50
fix action
Hoshinonyaruko Oct 26, 2023
fe2234a
fix action again
Hoshinonyaruko Oct 26, 2023
cf67a11
fix action againnn
Hoshinonyaruko Oct 26, 2023
0875837
fa
Hoshinonyaruko Oct 26, 2023
3a3c33f
fix
Hoshinonyaruko Oct 26, 2023
8789fa8
add a lot
Hoshinonyaruko Oct 27, 2023
7943466
add ws server token
Hoshinonyaruko Oct 28, 2023
4660983
merge
Hoshinonyaruko Oct 28, 2023
2a1026a
bugifx
Hoshinonyaruko Oct 28, 2023
4186970
fix
Hoshinonyaruko Oct 28, 2023
2f26828
fixat
Hoshinonyaruko Oct 28, 2023
007e1af
bugfix
Hoshinonyaruko Oct 28, 2023
c41fd77
bugfix
Hoshinonyaruko Oct 28, 2023
d8354b7
test
Hoshinonyaruko Oct 28, 2023
6e3c63c
test
Hoshinonyaruko Oct 28, 2023
f08d9d7
test2
Hoshinonyaruko Oct 28, 2023
4dd6e15
add url service
Hoshinonyaruko Oct 31, 2023
1f8c174
add url service
Hoshinonyaruko Oct 31, 2023
240f569
add url service
Hoshinonyaruko Oct 31, 2023
b191cb7
bugfix
Hoshinonyaruko Oct 31, 2023
42daab3
merge
Hoshinonyaruko Oct 31, 2023
8fec03f
fix
Hoshinonyaruko Oct 31, 2023
cca528b
fix
Hoshinonyaruko Oct 31, 2023
463a182
fix
Hoshinonyaruko Oct 31, 2023
e9187a1
bug fix
Hoshinonyaruko Nov 1, 2023
5ba2815
fix
Hoshinonyaruko Nov 1, 2023
c16e1af
fix
Hoshinonyaruko Nov 1, 2023
ec7863d
test
Hoshinonyaruko Nov 2, 2023
e8a7e73
ok
Hoshinonyaruko Nov 2, 2023
122d17d
add webui
Hoshinonyaruko Nov 2, 2023
4e9c209
merge
Hoshinonyaruko Nov 2, 2023
b502d61
add image_compress
Hoshinonyaruko Nov 3, 2023
3519e3e
merge
Hoshinonyaruko Nov 3, 2023
530ff96
bug fix
Hoshinonyaruko Nov 3, 2023
155a701
fix cq code
Hoshinonyaruko Nov 3, 2023
2f5ee4a
fixbug
Hoshinonyaruko Nov 3, 2023
afa03e3
bugfix
Hoshinonyaruko Nov 3, 2023
fdd1a73
merge
Hoshinonyaruko Nov 3, 2023
fe39cd2
bugfix
Hoshinonyaruko Nov 3, 2023
be08cbe
bugfix
Hoshinonyaruko Nov 3, 2023
931dfef
merge
Hoshinonyaruko Nov 3, 2023
73819c8
bugfix
Hoshinonyaruko Nov 3, 2023
b27f062
bugfix
Hoshinonyaruko Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 69 additions & 32 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package config

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -74,6 +74,7 @@ func LoadConfig(path string) (*Config, error) {
return conf, nil
}

// 确保配置完整性
func ensureConfigComplete(conf *Config, path string) error {
// 读取配置文件到缓冲区
configData, err := os.ReadFile(path)
Expand All @@ -95,16 +96,25 @@ func ensureConfigComplete(conf *Config, path string) error {
return err
}

// 使用反射比较现有配置和默认配置
missingSettings, err := getMissingSettings(&currentConfig.Settings, &defaultConfig.Settings)
// 使用反射找出结构体中缺失的设置
missingSettingsByReflection, err := getMissingSettingsByReflection(currentConfig, defaultConfig)
if err != nil {
return err
}

if len(missingSettings) > 0 {
// 如果存在缺失的设置,从默认配置模板中提取缺失的字段及其注释
fmt.Println("缺失的设置:", missingSettings)
missingConfigLines, err := extractMissingConfigLines(missingSettings, template.ConfigTemplate)
// 使用文本比对找出缺失的设置
missingSettingsByText, err := getMissingSettingsByText(template.ConfigTemplate, string(configData))
if err != nil {
return err
}

// 合并缺失的设置
allMissingSettings := mergeMissingSettings(missingSettingsByReflection, missingSettingsByText)

// 如果存在缺失的设置,处理缺失的配置行
if len(allMissingSettings) > 0 {
fmt.Println("缺失的设置:", allMissingSettings)
missingConfigLines, err := extractMissingConfigLines(allMissingSettings, template.ConfigTemplate)
if err != nil {
return err
}
Expand All @@ -122,51 +132,78 @@ func ensureConfigComplete(conf *Config, path string) error {
return nil
}

func getMissingSettings(current, defaultConf interface{}) (map[string]string, error) {
missingSettings := make(map[string]string)

// Ensure we are dealing with pointers and get the underlying element.
currentVal := reflect.ValueOf(current)
if currentVal.Kind() != reflect.Ptr {
return nil, errors.New("current config is not a pointer")
// mergeMissingSettings 合并由反射和文本比对找到的缺失设置
func mergeMissingSettings(reflectionSettings, textSettings map[string]string) map[string]string {
for k, v := range textSettings {
reflectionSettings[k] = v
}
currentVal = currentVal.Elem()
return reflectionSettings
}

defaultVal := reflect.ValueOf(defaultConf)
if defaultVal.Kind() != reflect.Ptr {
return nil, errors.New("default config is not a pointer")
}
defaultVal = defaultVal.Elem()
// getMissingSettingsByReflection 使用反射来对比结构体并找出缺失的设置
func getMissingSettingsByReflection(currentConfig, defaultConfig *Config) (map[string]string, error) {
missingSettings := make(map[string]string)
currentVal := reflect.ValueOf(currentConfig).Elem()
defaultVal := reflect.ValueOf(defaultConfig).Elem()

for i := 0; i < currentVal.NumField(); i++ {
fieldInfo := currentVal.Type().Field(i) // 获取字段信息
yamlTag := fieldInfo.Tag.Get("yaml") // 从字段信息中获取 yaml 标签
field := currentVal.Type().Field(i)
yamlTag := field.Tag.Get("yaml")
if yamlTag == "" {
continue // 如果没有 yaml 标签,则跳过
continue
}
yamlKeyName := strings.SplitN(yamlTag, ",", 2)[0]
if isZeroOfUnderlyingType(currentVal.Field(i).Interface()) && !isZeroOfUnderlyingType(defaultVal.Field(i).Interface()) {
missingSettings[yamlKeyName] = "missing"
}
}

// 使用strings来处理可能的逗号分隔的选项,例如 "fieldname,omitempty"
yamlKeyName := strings.Split(yamlTag, ",")[0] // 获取标签前的实际 YAML 键名
return missingSettings, nil
}

// getMissingSettingsByText compares settings in two strings line by line, looking for missing keys.
func getMissingSettingsByText(templateContent, currentConfigContent string) (map[string]string, error) {
templateKeys := extractKeysFromString(templateContent)
currentKeys := extractKeysFromString(currentConfigContent)

if isZeroOfUnderlyingType(currentVal.Field(i).Interface()) &&
!isZeroOfUnderlyingType(defaultVal.Field(i).Interface()) {
missingSettings[fieldInfo.Name] = yamlKeyName
missingSettings := make(map[string]string)
for key := range templateKeys {
if _, found := currentKeys[key]; !found {
missingSettings[key] = "missing"
}
}

return missingSettings, nil
}

// extractKeysFromString reads a string and extracts the keys (text before the colon).
func extractKeysFromString(content string) map[string]bool {
keys := make(map[string]bool)
lines := strings.Split(content, "\n")
for _, line := range lines {
if strings.Contains(line, ":") {
key := strings.TrimSpace(strings.Split(line, ":")[0])
keys[key] = true
}
}
return keys
}

func extractMissingConfigLines(missingSettings map[string]string, configTemplate string) ([]string, error) {
var missingConfigLines []string

// Split the configuration template into lines.
lines := strings.Split(configTemplate, "\n")
for _, yamlKey := range missingSettings {
for yamlKey := range missingSettings {
found := false
// Create a regex to match the line with optional spaces around the colon
regexPattern := fmt.Sprintf(`^\s*%s\s*:\s*`, regexp.QuoteMeta(yamlKey))
regex, err := regexp.Compile(regexPattern)
if err != nil {
return nil, fmt.Errorf("invalid regex pattern: %s", err)
}

for _, line := range lines {
// Check if the line contains the YAML key.
if strings.Contains(line, yamlKey) {
if regex.MatchString(line) {
missingConfigLines = append(missingConfigLines, line)
found = true
break
Expand Down
Loading