commit
70fe1ab91e
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
type ConfigContainerType string
|
||||
|
||||
const (
|
||||
BesidesExe ConfigContainerType = "besides-exe"
|
||||
UserConfig ConfigContainerType = "user-config"
|
||||
SystemConfig ConfigContainerType = "system-config"
|
||||
None ConfigContainerType = ""
|
||||
)
|
@ -0,0 +1,101 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
type AppConfig struct {
|
||||
AppName string `json:"app_name"`
|
||||
AppVersion string `json:"app_version"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
configure := AppConfig{
|
||||
AppName: "aaaaa",
|
||||
}
|
||||
cc := ConfigConfigure{
|
||||
AppName: "json-configure",
|
||||
ContainerType: BesidesExe,
|
||||
ConfigureFile: "config.json",
|
||||
}
|
||||
cc.Check()
|
||||
cc.MakeupContainerPath()
|
||||
log.Println("Configure Container Path:", cc.ContainerPath)
|
||||
cc.EnsureContainer()
|
||||
configure = cc.EnsureConfigure(configure).(AppConfig)
|
||||
log.Println("AppName:", configure.AppName)
|
||||
log.Println("AppVersion:", configure.AppVersion)
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// should return the dir path of the exe file
|
||||
func getConfigContainerBesidesExe() string {
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to Get Work Dir", err)
|
||||
}
|
||||
return path
|
||||
} else {
|
||||
executablePath, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to Get Executable Path", err)
|
||||
}
|
||||
executableDir := filepath.Dir(executablePath)
|
||||
return executableDir
|
||||
}
|
||||
}
|
||||
|
||||
func getConfigContainerUserConfig(appName string) string {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to Get User Config Dir", err)
|
||||
}
|
||||
dir = filepath.Join(dir, appName)
|
||||
return dir
|
||||
}
|
||||
|
||||
func getConfigContainerSystemConfig(appName string) string {
|
||||
log.Fatal("Error, not implemented yet")
|
||||
return ""
|
||||
}
|
Loading…
Reference in new issue