feat: 更换file包

This commit is contained in:
Mmx
2021-09-19 23:14:03 +08:00
parent 052aba8422
commit 80b06250e6
8 changed files with 55 additions and 89 deletions

View File

@@ -1,79 +0,0 @@
package util
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
type file struct{}
var File file
func (a *file) Exists(path string) bool {
root, err := a.GetRootPath()
if err != nil {
return false
}
_, err = os.Stat(root + path)
return err == nil || os.IsExist(err)
}
func (a *file) Read(path string) ([]byte, error) {
root, err := a.GetRootPath()
if err != nil {
return nil, err
}
return ioutil.ReadFile(root + path)
}
func (a *file) ReadJson(path string, receiver interface{}) error {
data, err := a.Read(path)
if err != nil {
return err
}
return json.Unmarshal(data, receiver)
}
func (a *file) Write(path string, data []byte) error {
root, err := a.GetRootPath()
if err != nil {
return err
}
return ioutil.WriteFile(root+path, data, 700)
}
func (a *file) WriteJson(path string, receiver interface{}) error {
data, err := json.MarshalIndent(receiver, "", " ")
if err != nil {
return err
}
return a.Write(path, data)
}
func (*file) GetRootPath() (string, error) {
t, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Dir(t) + "/", nil
}
func (a *file) Add(path string, c string) error {
root, err := a.GetRootPath()
if err != nil {
return err
}
file, err := os.OpenFile(root+path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
defer file.Close()
if err != nil {
return err
}
w := bufio.NewWriter(file)
if _, err = w.WriteString(c + "\n"); err != nil {
return err
}
return w.Flush()
}

View File

@@ -1,11 +1,13 @@
package util
import (
"autoLogin/global"
"github.com/Mmx233/tool"
"time"
)
func init() {
//http工具设定
tool.HTTP.Options.Timeout = 3 * time.Second
//tool设定
tool.HTTP.Options.Timeout = time.Duration(global.Config.Settings.Timeout) * time.Second
tool.File.Options.ForceRoot = true
}

View File

@@ -3,6 +3,7 @@ package util
import (
"autoLogin/global"
"fmt"
"github.com/Mmx233/tool"
"log"
"os"
"reflect"
@@ -30,7 +31,7 @@ func (*loG) WriteLog(name string, a ...interface{}) {
default:
t = fmt.Sprint(v)
}
err := File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t)
err := tool.File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700)
if err != nil {
log.Println("Log error: ", err)
}