-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
103 lines (86 loc) · 1.69 KB
/
util.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
)
var (
settings = map[string]string{
"root": ".",
"tmp_path": "./tmp/.go_watch",
"valid_ext": ".go, .tpl, .tmpl, .html, .toml, .yml",
"ignored": "assets, tmp, vendor",
}
)
func handleSig() {
ch := make(chan os.Signal, 1)
signal.Notify(ch,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
for {
s := <-ch
switch s {
case syscall.SIGINT:
closex()
case syscall.SIGHUP:
closex()
case syscall.SIGTERM:
closex()
case syscall.SIGQUIT:
closex()
}
}
}()
}
func isTmpDir(path string) bool {
absolutePath, _ := filepath.Abs(path)
absoluteTmpPath, _ := filepath.Abs(tmpPath())
return absolutePath == absoluteTmpPath
}
func isIgnoredFolder(path string) bool {
paths := strings.Split(path, "/")
if len(paths) <= 0 {
return false
}
for _, e := range strings.Split(settings["ignored"], ",") {
if strings.TrimSpace(e) == paths[0] {
return true
}
}
return false
}
func isWatchedFile(path string) bool {
absolutePath, _ := filepath.Abs(path)
absoluteTmpPath, _ := filepath.Abs(tmpPath())
if strings.HasPrefix(absolutePath, absoluteTmpPath) {
return false
}
ext := filepath.Ext(path)
for _, e := range strings.Split(settings["valid_ext"], ",") {
if strings.TrimSpace(e) == ext {
return true
}
}
return false
}
func root() string {
return settings["root"]
}
func tmpPath() string {
return settings["tmp_path"]
}
func fileClose(file *os.File) {
if err := file.Close(); err != nil {
log.Printf("failed to close file: %s => %v\n", file.Name(), err)
}
}
func closex() {
_ = os.RemoveAll(tmpPath())
os.Exit(1)
}