改善配置模块代码~

This commit is contained in:
2025-05-20 09:01:07 +08:00
parent a66f1c69b1
commit 67573e6ac2
9 changed files with 61 additions and 102 deletions

View File

@@ -5,24 +5,15 @@ import (
"os"
"github.com/golang-module/carbon/v2"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func Load() {
// where to read config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
parseArgs()
readconfig()
check()
debugprint()
if len(os.Args) != 1 {
handleArguments()
}
fmt.Println(Default)
carbon.SetDefault(carbon.Default{
Layout: carbon.DateTimeLayout,
@@ -33,66 +24,20 @@ func Load() {
}
func check() {
// 暂时只支持SQLite
if DB.Type != "SQLite" {
fmt.Println("sorry,we support SQLite only so far(At config/config.go : check())")
os.Exit(1)
}
if startTime := carbon.Parse(StartTime); startTime.IsMonday() != true {
fmt.Println("the start time must be a monday")
os.Exit(1)
}
}
func debugprint() {
fmt.Printf("ListenPort=%v\n", ListenPort)
fmt.Printf("database type:%s\n", DB.Type)
fmt.Printf("database path : %s\n", DB.Path)
fmt.Printf("database port:%d\n", DB.Port)
fmt.Printf("database user:%s\n", DB.User)
fmt.Printf("database passowrd:%s\n", DB.Password)
fmt.Printf("database name:%s\n", DB.Name)
fmt.Printf("session:%s\n", Session)
fmt.Printf("semester:%d\n", Semester)
fmt.Printf("start time:%s\n", StartTime)
fmt.Printf("week:%d\n", Week)
fmt.Printf("File=%v\n", File)
}
func handleArguments() {
if len(os.Args) > 2 {
fmt.Println("Please enter only 1 argument")
os.Exit(1)
}
switch os.Args[1] {
case "newsemester":
if DB.Type == "SQLite" {
sqliteNewSemester()
}
default:
panic("invalid argument")
}
}
func readconfig() {
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file: %v\n", err)
os.Exit(1)
}
ListenPort = viper.GetInt("ListenPort")
DB.Type = viper.GetString("DB.Type")
DB.Path = viper.GetString("DB.Path")
DB.Port = viper.GetInt("DB.Port")
DB.User = viper.GetString("DB.User")
DB.Password = viper.GetString("DB.Password")
DB.Name = viper.GetString("DB.Name")
Session = viper.GetString("Session")
Semester = viper.GetInt("Semester")
StartTime = viper.GetString("StartTime")
Week = viper.GetInt("Week")
File = viper.GetString("File")
if err := viper.Unmarshal(&Default); err != nil {
panic(fmt.Errorf("映射配置到结构体失败: %s", err))
}
}
func parseArgs() {
pflag.String("config", "config.yaml", "the path to config file.")
viper.BindPFlags(pflag.CommandLine)
pflag.Parse()
pathToConfigure = viper.GetString("config")
viper.SetConfigFile(pathToConfigure)
}