-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
61 lines (55 loc) · 1.01 KB
/
system.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
package sidecar
import (
"os"
"path/filepath"
"syscall"
)
func DetectFile(name string) (path string) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return ""
}
path, err = filepath.Abs(name)
if err != nil {
panic(err)
}
return
}
func CreateDirIfNotExist(path string) (abspath string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.Mkdir(path, 0744)
if err != nil {
panic(err)
}
}
abspath, err = filepath.Abs(path)
if err != nil {
panic(err)
}
return
}
func CreateFileIfNotExist(file string) (fd *os.File) {
fd, _ = os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_EXCL|os.O_APPEND, 0644)
return
}
func OpenExistFile(file string) (fd *os.File) {
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
panic(err)
}
return
}
func DetectProcess(pid int) (alive bool) {
process, err := os.FindProcess(pid)
if err != nil {
panic(err)
} else {
err := process.Signal(syscall.Signal(0))
if err != nil {
return false
} else {
return true
}
}
}