forked from takama/daemon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_nix.go
57 lines (48 loc) · 1.06 KB
/
helper_nix.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
// +build linux darwin
package daemon
import (
"os"
"os/exec"
"os/user"
)
// Service constants
const (
rootPrivileges = "You must have root user privileges. Possibly using 'sudo' command should help"
success = "\t\t\t\t\t[ \033[32mOK\033[0m ]" // Show colored "OK"
failed = "\t\t\t\t\t[\033[31mFAILED\033[0m]" // Show colored "FAILED"
)
func IsExecutable(path string) (bool, error) {
in, err := os.Open(path)
if err != nil {
return false, err
}
defer in.Close()
stat, err := in.Stat()
if err != nil {
return false, err
}
if stat.Mode()&0111 != 0 {
// executable
return true, nil
} else {
return false, nil
}
}
// Lookup path for executable file
func executablePath(name string) (string, error) {
if path, err := exec.LookPath(name); err == nil {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return execPath()
}
return path, nil
}
return execPath()
}
// Check root rights to use system service
func checkPrivileges() bool {
if user, err := user.Current(); err == nil && user.Gid == "0" {
return true
}
return false
}