improve: 移除 yaml config 之外类型的支持

This commit is contained in:
Mmx233
2023-08-28 20:49:36 +08:00
parent 266590f154
commit 2a098e00dd
7 changed files with 80 additions and 533 deletions

View File

@@ -5,7 +5,7 @@ import (
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
"github.com/Mmx233/tool"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
"os"
"time"
)
@@ -14,54 +14,63 @@ var Config models.Config
var Timeout time.Duration
func readConfig() {
//配置文件默认值
viper.SetDefault("form", srun.LoginForm{
Domain: "www.msftconnecttest.com",
UserType: "cmcc",
})
viper.SetDefault("meta", srun.LoginMeta{
N: "200",
Type: "1",
Acid: "5",
Enc: "srun_bx1",
})
viper.SetDefault("settings", models.Settings{
Basic: models.Basic{
Timeout: 5,
},
Guardian: models.Guardian{
Duration: 300,
},
Log: models.Log{
FilePath: "./",
},
DDNS: models.DDNS{
Enable: false,
TTL: 600,
Domain: "www.example.com",
},
})
func createDefaultConfig() error {
configFile, err := os.OpenFile(Flags.Path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer configFile.Close()
return yaml.NewEncoder(configFile).Encode(&models.Config{
Form: srun.LoginForm{
Domain: "www.msftconnecttest.com",
UserType: "cmcc",
},
Meta: srun.LoginMeta{
N: "200",
Type: "1",
Acid: "5",
Enc: "srun_bx1",
},
Settings: models.Settings{
Basic: models.Basic{
Timeout: 5,
},
Guardian: models.Guardian{
Duration: 300,
},
Log: models.Log{
FilePath: "./",
},
DDNS: models.DDNS{
Enable: false,
TTL: 600,
Domain: "www.example.com",
},
},
})
}
func initConfig() {
// 生成配置文件
if exist, e := tool.File.Exists(Flags.Path); e != nil {
log.Fatalln("[init] 读取配置文件失败:", e)
if exist, err := tool.File.Exists(Flags.Path); err != nil {
log.Fatalln("[init] 读取配置文件失败:", err)
} else if !exist {
e = viper.WriteConfigAs(Flags.Path)
if e != nil {
log.Fatalln("[init] 生成配置文件失败:", e)
err = createDefaultConfig()
if err != nil {
log.Fatalln("[init] 生成配置文件失败:", err)
}
log.Infoln("[init] 已生成配置文件,请编辑 '" + Flags.Path + "' 然后重试")
os.Exit(0)
}
// 读取配置文件
viper.SetConfigFile(Flags.Path)
if e := viper.ReadInConfig(); e != nil {
log.Fatalln("[init] 读取配置失败:", e)
configBytes, err := os.ReadFile(Flags.Path)
if err != nil {
log.Fatalln("[init] 读取配置失败:", err)
}
if e := viper.Unmarshal(&Config); e != nil {
log.Fatalln("[init] 解析配置失败:", e)
if err = yaml.Unmarshal(configBytes, &Config); err != nil {
log.Fatalln("[init] 解析配置失败:", err)
}
// flag 配置覆写

View File

@@ -8,7 +8,7 @@ func init() {
initFlags()
//配置文件初始化
readConfig()
initConfig()
//初始化常变量
Timeout = time.Duration(Config.Settings.Basic.Timeout) * time.Second

View File

@@ -5,41 +5,41 @@ import (
)
type Guardian struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
Duration uint `json:"duration" yaml:"duration" mapstructure:"duration"`
Enable bool `yaml:"enable"`
Duration uint `yaml:"duration"`
}
type Basic struct {
Https bool `json:"https" yaml:"https" mapstructure:"https"`
SkipCertVerify bool `json:"skip_cert_verify" yaml:"skip_cert_verify" mapstructure:"skip_cert_verify"`
Timeout uint `json:"timeout" yaml:"timeout" mapstructure:"timeout"`
Interfaces string `json:"interfaces" yaml:"interfaces" mapstructure:"interfaces"`
Https bool `yaml:"https"`
SkipCertVerify bool `yaml:"skip_cert_verify"`
Timeout uint `yaml:"timeout"`
Interfaces string `yaml:"interfaces"`
}
type Log struct {
DebugLevel bool `json:"debug_level" yaml:"debug_level" mapstructure:"debug_level"`
WriteFile bool `json:"write_file" yaml:"write_file" mapstructure:"write_file"`
FilePath string `json:"log_path" yaml:"log_path" mapstructure:"log_path"`
FileName string `json:"log_name" yaml:"log_name" mapstructure:"log_name"`
DebugLevel bool `yaml:"debug_level"`
WriteFile bool `yaml:"write_file"`
FilePath string `yaml:"log_path"`
FileName string `yaml:"log_name"`
}
type DDNS struct {
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
TTL uint `json:"ttl" yaml:"ttl" mapstructure:"ttl"`
Domain string `json:"domain" yaml:"domain" mapstructure:"domain"`
Provider string `json:"provider" yaml:"provider" mapstructure:"provider"`
Config map[string]interface{} `mapstructure:",remain"`
Enable bool `yaml:"enable"`
TTL uint `yaml:"ttl"`
Domain string `yaml:"domain"`
Provider string `yaml:"provider"`
Config map[string]interface{} `yaml:",inline"`
}
type Settings struct {
Basic Basic `json:"basic" yaml:"basic" mapstructure:"basic"`
Guardian Guardian `json:"guardian" yaml:"guardian" mapstructure:"guardian"`
Log Log `json:"log" yaml:"log" mapstructure:"log"`
DDNS DDNS `json:"ddns" yaml:"ddns" mapstructure:"ddns"`
Basic Basic `yaml:"basic"`
Guardian Guardian `yaml:"guardian"`
Log Log `yaml:"log"`
DDNS DDNS `yaml:"ddns"`
}
type Config struct {
Form srun.LoginForm `json:"form" yaml:"form" mapstructure:"form"`
Meta srun.LoginMeta `json:"meta" yaml:"meta" mapstructure:"meta"`
Settings Settings `json:"settings" yaml:"settings" mapstructure:"settings"`
Form srun.LoginForm `yaml:"form"`
Meta srun.LoginMeta `yaml:"meta"`
Settings Settings `yaml:"settings"`
}