This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_service.go
146 lines (119 loc) · 2.64 KB
/
system_service.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package systemservice
import (
"os"
"os/exec"
"os/user"
"strings"
)
/*
New creates a new system service manager instance.
*/
func New(cmd ServiceCommand) SystemService {
serv := SystemService{Command: cmd}
return serv
}
/*
SystemService represents a generic system service configuration
*/
type SystemService struct {
Command ServiceCommand
}
/*
ServiceCommand represents the command the system service should run
*/
type ServiceCommand struct {
// The human-friendly name of your service. Note: best to not include
// spaces in the name.
Name string
// The label to use to identify the service. This must be unique
// and should not include spaces.
Label string
// The name of the program to run
Program string
// The arguments to pass to the command. Optional.
Args []string
// The description of your service. Optional.
Description string
// The URL to your service documentation. Optional.
Documentation string
// Whether or not to turn on debug behavior
Debug bool
}
func (c *ServiceCommand) String() string {
s := c.Program
if len(c.Args) > 0 {
s = s + " " + strings.Join(c.Args, " ")
}
return s
}
/*
ServiceStatus is a generic representation of the service running on the system
*/
type ServiceStatus struct {
Running bool
PID int
}
/*
Running indicates if the service is active and running
*/
func (s *SystemService) Running() (bool, error) {
status, err := s.Status()
if err != nil {
return false, err
}
return status.Running, nil
}
/*
runCommand is a lightweight wrapper around exec.Command
*/
func runCommand(name string, args ...string) (out string, err error) {
stdout, err := exec.Command(name, args...).Output()
return string(stdout), err
}
/*
isRoot returns whether or not the program was run as root
Always returns false on Windows because there is no
good way to detect root on Windows.
*/
func isRoot() bool {
u, err := user.Current()
if err != nil {
return false
}
// On unix systems, root user either has the UID 0,
// the GID 0 or both.
return u.Uid == "0" || u.Gid == "0"
}
/*
homeDir returns the home directory of the user or "/" if
we cannot determine it.
*/
func homeDir() string {
u, err := user.Current()
if err != nil {
logger.Log("User does not have a home directory!")
return "/"
}
return u.HomeDir
}
/*
username returns the username of the current user
*/
func username() string {
user, err := user.Current()
if err != nil {
panic(err)
}
return user.Username
}
/*
fileExists is a helper to return whether or not a give
file exists
*/
func fileExists(filename string) bool {
info, err := appFS.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}