Skip to content

Commit

Permalink
优化set.ini生成
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanyanwow committed Nov 13, 2022
1 parent 4a07be0 commit e8e25d1
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion kernel/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ var DbName string
var BatPath string
var BatIdentify string

var Cfg, _ = ini.Load("set.ini") //初始化一个cfg
var Cfg *ini.File

func IsDev() bool {
return Cfg.Section("").Key("app_mode").String() == "dev"
}

func Init() {
iniFileExists, _ := PathExists("set.ini")
if !iniFileExists {
createIni()
}

Cfg, _ = ini.Load("set.ini") //初始化一个cfg

DbPath = Cfg.Section("").Key("db_path").String()
fmt.Println(DbPath)
if DbPath == "" {
Expand Down Expand Up @@ -49,3 +56,30 @@ func Init() {
//os.Exit(1)

}

func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func createIni() {
file6, err := os.Create("set.ini")
if err != nil {
fmt.Println(err)
return
}
data := "app_mode = dev\n\ndb_path = \ndb_name = \n\nbat_path = \n\nbat_identify = \n\n[gin]\naddress = 127.0.0.1\nport = 8899"

_, err = file6.WriteString(data)
if err != nil {
fmt.Println(err)
return
}

_ = file6.Close()
}

0 comments on commit e8e25d1

Please sign in to comment.