From e8e25d1720cf5da167419c1d07618b2f2449c280 Mon Sep 17 00:00:00 2001 From: siam <59419979@qq.com> Date: Sun, 13 Nov 2022 12:37:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96set.ini=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/config/config.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/kernel/config/config.go b/kernel/config/config.go index 3d25eaf..6f62d7a 100644 --- a/kernel/config/config.go +++ b/kernel/config/config.go @@ -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 == "" { @@ -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() +}