feat: 更换配置文件实现包
This commit is contained in:
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module autoLogin
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/Mmx233/config v0.0.3
|
||||
github.com/Mmx233/tool v0.2.3
|
||||
github.com/PuerkitoBio/goquery v1.7.1 // indirect
|
||||
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -1,3 +1,5 @@
|
||||
github.com/Mmx233/config v0.0.3 h1:E744hob5fWD5P7m7q+91Z41R+Y1LDkxIhQXIeV1tel0=
|
||||
github.com/Mmx233/config v0.0.3/go.mod h1:3mt+LSHUvD/C6tq1ge/Dmp8xKivFmo7gxSihCPjIAi0=
|
||||
github.com/Mmx233/tool v0.2.3 h1:fnjkYrX6Zhc0vcyPF5ihnxkNjXQ8zeAoG1Xm6xo7aBE=
|
||||
github.com/Mmx233/tool v0.2.3/go.mod h1:un5MCoI21lH40fFI5S9++aja8nJb+2sjATak7PnGMZ0=
|
||||
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"autoLogin/models/util"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Timeout uint `json:"timeout"`
|
||||
DemoMode bool `json:"demo_mode"`
|
||||
Dns string `json:"dns"`
|
||||
Guardian uint `json:"guardian"`
|
||||
Daemon bool `json:"daemon"`
|
||||
}
|
||||
@@ -31,33 +26,3 @@ func (a *Config) Generate() *LoginInfo {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Config) FillDefault() *Config {
|
||||
var m = map[interface{}]map[string]interface{}{
|
||||
&a.From: {
|
||||
"Domain": "www.msftconnecttest.com",
|
||||
"UserType": "cmcc",
|
||||
},
|
||||
&a.Meta: {
|
||||
"N": "200",
|
||||
"Type": "1",
|
||||
"Acid": "5",
|
||||
"Enc": "srun_bx1",
|
||||
},
|
||||
&a.Settings: {
|
||||
"Dns": "1.2.4.8",
|
||||
},
|
||||
}
|
||||
|
||||
for q, w := range m {
|
||||
t := reflect.ValueOf(q).Elem()
|
||||
for k, v := range w {
|
||||
tt := t.FieldByName(k)
|
||||
if util.Reflect.IsEmpty(tt) {
|
||||
tt.Set(reflect.ValueOf(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
41
util/init.go
41
util/init.go
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"autoLogin/global"
|
||||
"autoLogin/models"
|
||||
"github.com/Mmx233/config"
|
||||
"github.com/Mmx233/tool"
|
||||
"log"
|
||||
"os"
|
||||
@@ -11,29 +12,35 @@ import (
|
||||
|
||||
func init() {
|
||||
//配置文件初始化
|
||||
Path := "Config.json"
|
||||
var c models.Config
|
||||
if !File.Exists(Path) {
|
||||
if err := File.WriteJson(
|
||||
Path,
|
||||
c.FillDefault(),
|
||||
); err != nil {
|
||||
log.Println("创建配置文件失败:\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
if e := config.Load(config.Options{
|
||||
Config: &global.Config,
|
||||
Default: &models.Config{
|
||||
From: models.LoginForm{
|
||||
Domain: "www.msftconnecttest.com",
|
||||
UserType: "cmcc",
|
||||
},
|
||||
Meta: models.LoginMeta{
|
||||
N: "200",
|
||||
Type: "1",
|
||||
Acid: "5",
|
||||
Enc: "srun_bx1",
|
||||
},
|
||||
Settings: models.Settings{
|
||||
Timeout: 1,
|
||||
},
|
||||
},
|
||||
Path: "Config.json",
|
||||
FillDefault: true,
|
||||
Overwrite: true,
|
||||
}); e != nil {
|
||||
if config.IsNew(e) {
|
||||
log.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err := File.ReadJson(Path, &c); err != nil {
|
||||
log.Println("读取配置文件失败:\n", err.Error())
|
||||
log.Println("读取配置文件失败:\n", e.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_ = File.WriteJson(Path, c.FillDefault())
|
||||
|
||||
global.Config = &c
|
||||
|
||||
//http工具设定
|
||||
tool.HTTP.Options.Timeout = 3 * time.Second
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user