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.
json-configure-golang/config-configure.go

102 lines
3.3 KiB

1 month ago
package main
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 {
log.Fatal("Check, ContainerType should not be None or empty ")
} 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 {
log.Fatal("Error making container path, ContainerType must be specified")
} else if receiver.ContainerType == BesidesExe {
receiver.ContainerPath = getConfigContainerBesidesExe()
} else if receiver.ContainerType == UserConfig {
receiver.ContainerPath = getConfigContainerUserConfig(receiver.AppName)
} else if receiver.ContainerType == SystemConfig {
log.Fatal("Error making container path, SystemConfig is not supported for now")
}
}
func (receiver *ConfigConfigure) EnsureContainer() {
if receiver.ContainerPath == "" {
log.Fatal("Error ensuring container, ContainerPath should not be empty")
}
// 检查目录是否存在
if _, err := os.Stat(receiver.ContainerPath); os.IsNotExist(err) {
// 如果目录不存在,则创建
err := os.MkdirAll(receiver.ContainerPath, 0755)
if err != nil {
log.Fatal("Failed to create container directory: ", err)
}
log.Printf("Container directory created: %s", receiver.ContainerPath)
} else if err != nil {
log.Fatal("Failed to check container directory: ", err)
} else {
log.Printf("Container directory already exists: %s", receiver.ContainerPath)
}
}
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 {
log.Fatalf("Failed to read config file: %v", err)
}
// 使用反射获取 defaultConfig 的类型
val := reflect.New(reflect.TypeOf(defaultConfig)).Interface()
if err := json.Unmarshal(data, val); err != nil {
log.Fatalf("Failed to unmarshal config file: %v", err)
}
log.Printf("Loaded config from file: %s", configFile)
return reflect.ValueOf(val).Elem().Interface()
} else if os.IsNotExist(err) {
// 配置文件不存在,保存默认配置到文件
jsonData, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
log.Fatalf("Error encoding default config: %v", err)
}
if err := os.WriteFile(configFile, jsonData, 0644); err != nil {
log.Fatalf("Failed to write default config to file: %v", err)
}
log.Printf("Saved default config to file: %s", configFile)
return defaultConfig
} else {
// 其他错误
log.Fatalf("Error checking config file: %v", err)
}
return defaultConfig
}