mirror of
https://github.com/ZSCNetSupportDept/scheduler.git
synced 2025-10-28 12:35:03 +08:00
44 lines
923 B
Go
44 lines
923 B
Go
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
|
||
"github.com/golang-module/carbon/v2"
|
||
"github.com/spf13/pflag"
|
||
"github.com/spf13/viper"
|
||
)
|
||
|
||
func Load() {
|
||
|
||
parseArgs()
|
||
readconfig()
|
||
fmt.Printf("%+v\n", Default)
|
||
|
||
carbon.SetDefault(carbon.Default{
|
||
Layout: carbon.DateTimeLayout,
|
||
Timezone: carbon.PRC,
|
||
WeekStartsAt: carbon.Monday,
|
||
Locale: "zh-CN", // 取值范围:lang 目录下翻译文件名,不包含文件后缀
|
||
})
|
||
|
||
}
|
||
|
||
func readconfig() {
|
||
if err := viper.ReadInConfig(); err != nil {
|
||
fmt.Printf("Error reading config file: %v\n", err)
|
||
os.Exit(1)
|
||
}
|
||
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)
|
||
}
|