-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder.go
More file actions
331 lines (272 loc) · 9.72 KB
/
builder.go
File metadata and controls
331 lines (272 loc) · 9.72 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package launchctlutil
import (
"bytes"
"fmt"
"strconv"
)
const (
oneIndent = " "
twoIndents = oneIndent + oneIndent
threeIndents = twoIndents + oneIndent
openPlist = "<plist version=\"1.0\">\n"
closePlist = "</plist>\n"
openKey = "<key>"
closeKey = "</key>\n"
openDict = "<dict>\n"
closeDict = "</dict>\n"
openArray = "<array>\n"
closeArray = "</array>\n"
openString = "<string>"
closeString = "</string>\n"
openInt = "<integer>"
closeInt = "</integer>\n"
newLine = "\n"
)
// ConfigurationBuilder is used to build a new launchd service Configuration.
//
// Example:
// config, err := launchctlutil.NewConfigurationBuilder().
// SetKind(launchctlutil.UserAgent).
// SetLabel("com.testing").
// SetRunAtLoad(true).
// SetCommand("echo").
// AddArgument("Hello world!").
// SetLogParentPath("/tmp").
// Build()
// if err != nil {
// log.Fatal(err.Error())
// }
type ConfigurationBuilder interface {
// SetLabel sets the label.
SetLabel(label string) ConfigurationBuilder
// SetCommand sets the command to execute.
SetCommand(command string) ConfigurationBuilder
// AddEnvironmentVariable adds an environment variable with
// a given value.
AddEnvironmentVariable(name string, value string) ConfigurationBuilder
// AddArgument adds an argument for a command.
AddArgument(value string) ConfigurationBuilder
// SetLogParentPath sets the directory path where a log
// file will be saved to. One combined log file is saved
// containing the output of both stderr and stdout. The
// file name is formatted as "(launchd-label).log".
//
// This setting overrides the settings of SetStandardErrorPath()
// and SetStandardOutPath().
SetLogParentPath(logParentPath string) ConfigurationBuilder
// SetStandardErrorPath sets the file path where stderr
// output will be saved to.
//
// This setting is ignored if SetLogParentPath() is used.
SetStandardErrorPath(filePath string) ConfigurationBuilder
// SetStandardOutPath sets the file path where stdout
// output will be saved to.
//
// This setting is ignored if SetLogParentPath() is used.
SetStandardOutPath(filePath string) ConfigurationBuilder
// SetKind sets the type.
SetKind(kind Kind) ConfigurationBuilder
// SetStartInterval sets the start interval in seconds.
SetStartInterval(seconds int) ConfigurationBuilder
// SetStartCalendarIntervalMinute sets the minute of each hour
// that the command will be executed. For example, setting the
// minute to 10 will run the command at the 10th minute of each
// hour: 01:10, 02:10, 03:10, and so on.
SetStartCalendarIntervalMinute(minuteOfEachHour int) ConfigurationBuilder
// SetRunAtLoad sets whether or not the service will start
// when it is loaded.
SetRunAtLoad(enabled bool) ConfigurationBuilder
// SetUserName sets whether the service should run as a specific
// user (by username).
SetUserName(userName string) ConfigurationBuilder
// SetGroupName sets whether the service should run as a specific
// group (by group name).
SetGroupName(groupName string) ConfigurationBuilder
// SetInitGroups sets whether launchd should call the function
// initgroups(3) before starting the servie.
SetInitGroups(enabled bool) ConfigurationBuilder
// SetUmask sets the umask for the service.
SetUmask(umask int) ConfigurationBuilder
// Build returns the resulting service Configuration.
Build() (Configuration, error)
}
type configurationBuilder struct {
label string
command string
environmentVariables string
arguments string
lines string
logParentPath string
stderrLogFilePath string
stdoutLogFilePath string
configurationFilePath string
kind Kind
startIntervalSeconds int
startCalendarIntervalMinuteOfHour int
isStartCalendarIntervalMinuteSet bool
runAtLoad bool
isRunAtLoadSet bool
userName string
groupName string
initGroups bool
isInitGroupsSet bool
umask int
isUmaskSet bool
}
// NewConfigurationBuilder creates a new instance of a ConfigurationBuilder.
func NewConfigurationBuilder() ConfigurationBuilder {
return &configurationBuilder{}
}
func (o *configurationBuilder) SetLabel(label string) ConfigurationBuilder {
o.label = label
return o
}
func (o *configurationBuilder) SetCommand(command string) ConfigurationBuilder {
o.command = command
return o
}
func (o *configurationBuilder) AddEnvironmentVariable(name string, value string) ConfigurationBuilder {
o.environmentVariables = concat(o.environmentVariables, " ", openKey, name, closeKey,
threeIndents, openString, value, closeString)
return o
}
func (o *configurationBuilder) AddArgument(value string) ConfigurationBuilder {
o.arguments = concat(o.arguments,
threeIndents, openString, value, closeString)
return o
}
func (o *configurationBuilder) SetLogParentPath(logParentPath string) ConfigurationBuilder {
o.logParentPath = logParentPath
return o
}
func (o *configurationBuilder) SetStandardErrorPath(filePath string) ConfigurationBuilder {
o.stderrLogFilePath = filePath
return o
}
func (o *configurationBuilder) SetStandardOutPath(filePath string) ConfigurationBuilder {
o.stdoutLogFilePath = filePath
return o
}
func (o *configurationBuilder) SetKind(kind Kind) ConfigurationBuilder {
o.kind = kind
return o
}
func (o *configurationBuilder) SetStartInterval(seconds int) ConfigurationBuilder {
o.startIntervalSeconds = seconds
return o
}
func (o *configurationBuilder) SetStartCalendarIntervalMinute(minuteOfEachHour int) ConfigurationBuilder {
o.startCalendarIntervalMinuteOfHour = minuteOfEachHour
o.isStartCalendarIntervalMinuteSet = true
return o
}
func (o *configurationBuilder) SetRunAtLoad(enabled bool) ConfigurationBuilder {
o.runAtLoad = enabled
o.isRunAtLoadSet = true
return o
}
func (o *configurationBuilder) SetUserName(userName string) ConfigurationBuilder {
o.userName = userName
return o
}
func (o *configurationBuilder) SetGroupName(groupName string) ConfigurationBuilder {
o.groupName = groupName
return o
}
func (o *configurationBuilder) SetInitGroups(enabled bool) ConfigurationBuilder {
o.initGroups = enabled
o.isInitGroupsSet = true
return o
}
func (o *configurationBuilder) SetUmask(umask int) ConfigurationBuilder {
o.umask = umask
o.isUmaskSet = true
return o
}
func (o *configurationBuilder) Build() (Configuration, error) {
lines := concat("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" ",
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n",
openPlist,
oneIndent, openDict)
lines = concat(lines, twoIndents, openKey, "Label", closeKey,
twoIndents, openString, o.label, closeString)
if len(o.environmentVariables) > 0 {
lines = concat(lines, twoIndents, openKey, "EnvironmentVariables", closeKey,
twoIndents, openDict,
o.environmentVariables,
twoIndents, closeDict)
}
if len(o.userName) > 0 {
lines = concat(lines, twoIndents, openKey, "UserName", closeKey,
twoIndents, openString, o.userName, closeString)
}
if len(o.groupName) > 0 {
lines = concat(lines, twoIndents, openKey, "GroupName", closeKey,
twoIndents, openString, o.groupName, closeString)
}
if o.isInitGroupsSet {
lines = concat(lines, twoIndents, openKey, "InitGroups", closeKey,
twoIndents, boolToXml(o.initGroups), newLine)
}
if o.isUmaskSet {
lines = concat(lines, twoIndents, openKey, "Umask", closeKey,
twoIndents, openInt, strconv.Itoa(o.umask), closeInt)
}
if len(o.command) > 0 {
lines = concat(lines, twoIndents, openKey, "ProgramArguments", closeKey,
twoIndents, openArray,
threeIndents, openString, o.command, closeString)
if len(o.arguments) > 0 {
lines = concat(lines, o.arguments)
}
lines = concat(lines, twoIndents, closeArray)
}
if len(o.logParentPath) > 0 {
lines = concat(lines, twoIndents, openKey, "StandardOutPath", closeKey,
twoIndents, openString, o.logParentPath, "/", o.label, ".log", closeString)
lines = concat(lines, twoIndents, openKey, "StandardErrorPath", closeKey,
twoIndents, openString, o.logParentPath, "/", o.label, ".log", closeString)
} else {
if len(o.stderrLogFilePath) > 0 {
lines = concat(lines, twoIndents, openKey, "StandardErrorPath", closeKey,
twoIndents, openString, o.stderrLogFilePath, closeString)
}
if len(o.stdoutLogFilePath) > 0 {
lines = concat(lines, twoIndents, openKey, "StandardOutPath", closeKey,
twoIndents, openString, o.stdoutLogFilePath, closeString)
}
}
if o.startIntervalSeconds > 0 {
lines = concat(lines, twoIndents, openKey, "StartInterval", closeKey,
twoIndents, openInt, strconv.Itoa(o.startIntervalSeconds), closeInt)
}
if o.isStartCalendarIntervalMinuteSet {
lines = concat(lines, twoIndents, openKey, "StartCalendarInterval", closeKey,
twoIndents, openDict,
threeIndents, openKey, "Minute", closeKey,
threeIndents, openInt, strconv.Itoa(o.startCalendarIntervalMinuteOfHour), closeInt,
twoIndents, closeDict)
}
if o.isRunAtLoadSet {
lines = concat(lines, twoIndents, openKey, "RunAtLoad", closeKey,
twoIndents, boolToXml(o.runAtLoad), newLine)
}
lines = concat(lines, oneIndent, closeDict, closePlist)
return &configuration{
label: o.label,
contents: lines,
kind: o.kind,
}, nil
}
func boolToXml(b bool) string {
return fmt.Sprintf("<%t/>", b)
}
func concat(current string, additions ...string) (new string) {
var buffer bytes.Buffer
buffer.WriteString(current)
for _, addition := range additions {
buffer.WriteString(addition)
}
return buffer.String()
}