From 9c274d4364a41e5430f35253ffed873e16db497f Mon Sep 17 00:00:00 2001 From: Mmx <1624045573@qq.com> Date: Wed, 5 May 2021 10:11:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E4=BC=98=E5=8C=96file=E5=8C=85?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/file.go | 14 +++++++++++--- Util/init.go | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Util/file.go b/Util/file.go index 1eb1ea5..342555b 100644 --- a/Util/file.go +++ b/Util/file.go @@ -23,7 +23,11 @@ func (*file) Exists(path string) bool { return true } -func (*file) Read(path string, receiver interface{}) error { +func (a *file) Read(path string) ([]byte, error) { + return ioutil.ReadFile(a.GetRootPath() + "/" + path) +} + +func (*file) ReadJson(path string, receiver interface{}) error { file, err := ioutil.ReadFile(path) if err != nil { return err @@ -31,12 +35,16 @@ func (*file) Read(path string, receiver interface{}) error { return json.Unmarshal(file, receiver) } -func (*file) Write(path string, receiver interface{}) error { +func (a *file) Write(path string, data []byte) error { + return ioutil.WriteFile(a.GetRootPath()+"/"+path, data, 700) +} + +func (a *file) WriteJson(path string, receiver interface{}) error { data, err := json.MarshalIndent(receiver, "", " ") if err != nil { return err } - return ioutil.WriteFile(path, data, 700) + return a.Write(path, data) } func (*file) GetRootPath() string { diff --git a/Util/init.go b/Util/init.go index 895cc0f..21f67fa 100644 --- a/Util/init.go +++ b/Util/init.go @@ -9,9 +9,9 @@ import ( func init() { //配置文件初始化 - Path := File.GetRootPath() + "/Config.json" + Path := "Config.json" if !File.Exists(Path) { - if err := File.Write(Path, &Modles.Config{ //默认值 + if err := File.WriteJson(Path, &Modles.Config{ //默认值 From: Modles.LoginForm{ Domain: "www.msftconnecttest.com", UserName: "", @@ -32,7 +32,7 @@ func init() { } var c Modles.Config - if err := File.Read(Path, &c); err != nil { + if err := File.ReadJson(Path, &c); err != nil { log.Println("读取配置文件失败:\n", err.Error()) os.Exit(1) }