-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.go
88 lines (79 loc) · 1.81 KB
/
utils.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
package daemon
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"strings"
"github.com/sohaha/zlsgo/zshell"
"github.com/sohaha/zlsgo/ztype"
)
func (c *Config) execPath() (path string) {
if len(c.Executable) != 0 {
path, _ = filepath.Abs(c.Executable)
return path
}
path, _ = os.Executable()
return
}
func runGrep(grep, command string, args ...string) (res string, err error) {
var grepout bytes.Buffer
var out bytes.Buffer
var outErr bytes.Buffer
commands := []string{command}
commands = append(commands, args...)
err = runcmd(commands, bytes.NewReader([]byte("")), &out, &outErr)
if err != nil {
return
}
commands = []string{"grep", grep}
err = runcmd(commands, bytes.NewReader(out.Bytes()), &grepout, &outErr)
if err != nil {
return
}
res = grepout.String()
return
}
func isSudo() error {
_, id, _, err := zshell.Run("id -u")
if err != nil {
return err
}
id = strings.Replace(id, "\n", "", -1)
if id != "0" {
return ErrNotAnRootUser
}
return nil
}
func IsPermissionError(err error) bool {
return err == ErrNotAnAdministrator || err == ErrNotAnRootUser
}
func run(command string, args ...string) error {
var out bytes.Buffer
var outErr bytes.Buffer
commands := []string{command}
commands = append(commands, args...)
return runcmd(commands, bytes.NewReader([]byte("")), &out, &outErr)
}
func runcmd(commands []string, in *bytes.Reader, out, outErr *bytes.Buffer) error {
code, _, _, err := zshell.ExecCommand(context.Background(), commands, in, out, outErr)
if err != nil {
return err
}
if code != 0 {
errMsg := outErr.String()
if errMsg == "" {
errMsg = out.String()
}
err = errors.New(errMsg)
}
return err
}
func isServiceRestart(c *Config) bool {
load := optionRunAtLoadDefault
if l, ok := c.Options[optionRunAtLoad]; ok {
load = ztype.ToBool(l)
}
return load
}