-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
86 lines (78 loc) · 1.81 KB
/
utils.go
File metadata and controls
86 lines (78 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package box
import (
"github.com/tudyzhb/yaml"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
)
// 读取配置文件
func PubConfigRead(configPath string, targetConfig interface{}, defaultConfig interface{}) (err error) {
var b []byte
if b, err = ioutil.ReadFile(configPath); err != nil {
if os.IsNotExist(err) == false {
return
}
// 自动创建配置文件
if defaultConfig == nil {
err = fmt.Errorf(`[config] defaultConfig undefined, "%s" error: %v`, configPath, err)
return
}
if d, _err2 := yaml.Marshal(defaultConfig); _err2 != nil {
err = _err2
return
} else if err = ioutil.WriteFile(configPath, d, 0666); err != nil {
return
}
err = fmt.Errorf(`[config] please modify "%s" and run again`, configPath)
return
}
// 读取配置
if err = yaml.Unmarshal(b, targetConfig); err != nil {
return
}
// 设置保存地址对对象指针
type configInterface interface {
SetSavePath(savePath string) (err error)
SetSavePoint(saveTarget interface{}) (err error)
}
if _c, _ok := targetConfig.(configInterface); _ok == true {
_c.SetSavePath(configPath)
_c.SetSavePoint(targetConfig)
}
return
}
// 取运行文件的哈希前32位
func PubGetRunFileHash() (ret string) {
if len(os.Args) > 0 {
if p, err := filepath.Abs(os.Args[0]); err == nil {
if f, err := os.Open(p); err == nil {
defer f.Close()
h := sha1.New()
if _, err = io.Copy(h, f); err == nil {
ret = fmt.Sprintf("%x", h.Sum(nil))
}
}
}
}
if len(ret) > 8 {
ret = ret[0:8]
}
return
}
// 取运行文件的修改时间
func PubGetRunFileTime() (ret *time.Time) {
if len(os.Args) > 0 {
if p, err := filepath.Abs(os.Args[0]); err == nil {
if info, err := os.Stat(p); err == nil {
if t := info.ModTime(); t.Unix() > 0 {
ret = &t
}
}
}
}
return
}