You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.3 KiB
102 lines
3.3 KiB
1 month ago
|
package config
|
||
1 month ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
type ConfigConfigure struct {
|
||
|
AppName string
|
||
|
ContainerType ConfigContainerType
|
||
|
ContainerPath string
|
||
|
ConfigureFile string
|
||
|
}
|
||
|
|
||
|
func (receiver *ConfigConfigure) Check() {
|
||
|
if receiver.ContainerType == None {
|
||
1 month ago
|
log.Fatal("Check, ContainerType should not be None or Empty ")
|
||
1 month ago
|
} else if receiver.ContainerType == BesidesExe {
|
||
|
// pass
|
||
|
} else if receiver.ContainerType == UserConfig {
|
||
|
if receiver.AppName == "" {
|
||
|
log.Fatal("Check, AppName should not be None while ContainerType == UserConfig")
|
||
|
}
|
||
|
} else if receiver.ContainerType == SystemConfig {
|
||
|
if receiver.AppName == "" {
|
||
|
log.Fatal("Check, AppName should not be None while ContainerType == SystemConfig\nBesides SystemConfig is not supported for now")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (receiver *ConfigConfigure) MakeupContainerPath() {
|
||
|
if receiver.ContainerType == None {
|
||
1 month ago
|
log.Fatal("Error Making Container Path, ContainerType must be Specified")
|
||
1 month ago
|
} else if receiver.ContainerType == BesidesExe {
|
||
|
receiver.ContainerPath = getConfigContainerBesidesExe()
|
||
|
} else if receiver.ContainerType == UserConfig {
|
||
|
receiver.ContainerPath = getConfigContainerUserConfig(receiver.AppName)
|
||
|
} else if receiver.ContainerType == SystemConfig {
|
||
1 month ago
|
log.Fatal("Error Making Container Path, SystemConfig is not Supported for now")
|
||
1 month ago
|
}
|
||
|
}
|
||
|
|
||
|
func (receiver *ConfigConfigure) EnsureContainer() {
|
||
|
if receiver.ContainerPath == "" {
|
||
1 month ago
|
log.Fatal("Error Ensuring Container, ContainerPath should not be Empty")
|
||
1 month ago
|
}
|
||
|
|
||
|
// 检查目录是否存在
|
||
|
if _, err := os.Stat(receiver.ContainerPath); os.IsNotExist(err) {
|
||
|
// 如果目录不存在,则创建
|
||
|
err := os.MkdirAll(receiver.ContainerPath, 0755)
|
||
|
if err != nil {
|
||
1 month ago
|
log.Fatal("Failed to Create Container Directory: ", err)
|
||
1 month ago
|
}
|
||
1 month ago
|
log.Printf("Container Directory Created: %s", receiver.ContainerPath)
|
||
1 month ago
|
} else if err != nil {
|
||
1 month ago
|
log.Fatal("Failed to Check Container Directory: ", err)
|
||
1 month ago
|
} else {
|
||
1 month ago
|
log.Printf("Container Directory Already Exists: %s", receiver.ContainerPath)
|
||
1 month ago
|
}
|
||
|
}
|
||
|
|
||
|
func (receiver *ConfigConfigure) EnsureConfigure(defaultConfig interface{}) interface{} {
|
||
|
// makeup config file full path
|
||
|
configFile := filepath.Join(receiver.ContainerPath, receiver.ConfigureFile)
|
||
|
|
||
|
// 检查配置文件是否存在
|
||
|
if _, err := os.Stat(configFile); err == nil {
|
||
|
// 配置文件存在,读取并反序列化到目标结构体
|
||
|
data, err := os.ReadFile(configFile)
|
||
|
if err != nil {
|
||
1 month ago
|
log.Fatalf("Failed to Read Config File: %v", err)
|
||
1 month ago
|
}
|
||
|
|
||
|
// 使用反射获取 defaultConfig 的类型
|
||
|
val := reflect.New(reflect.TypeOf(defaultConfig)).Interface()
|
||
|
if err := json.Unmarshal(data, val); err != nil {
|
||
1 month ago
|
log.Fatalf("Failed to Unmarshal Config File: %v", err)
|
||
1 month ago
|
}
|
||
1 month ago
|
log.Printf("Loaded Config from File: %s", configFile)
|
||
1 month ago
|
return reflect.ValueOf(val).Elem().Interface()
|
||
|
} else if os.IsNotExist(err) {
|
||
|
// 配置文件不存在,保存默认配置到文件
|
||
|
jsonData, err := json.MarshalIndent(defaultConfig, "", " ")
|
||
|
if err != nil {
|
||
1 month ago
|
log.Fatalf("Error Encoding Default Config: %v", err)
|
||
1 month ago
|
}
|
||
|
if err := os.WriteFile(configFile, jsonData, 0644); err != nil {
|
||
1 month ago
|
log.Fatalf("Failed to Write Default Config to File: %v", err)
|
||
1 month ago
|
}
|
||
1 month ago
|
log.Printf("Saved Default Config to File: %s", configFile)
|
||
1 month ago
|
return defaultConfig
|
||
|
} else {
|
||
|
// 其他错误
|
||
1 month ago
|
log.Fatalf("Error Checking Config File: %v", err)
|
||
1 month ago
|
}
|
||
|
return defaultConfig
|
||
|
}
|