-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockercontrol.pas
335 lines (298 loc) · 7.1 KB
/
dockercontrol.pas
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
332
333
334
program DockerControl;
{$MODE DELPHI}
uses
{$IFDEF UNIX}
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}
{$ENDIF}
{$IFDEF DARWIN}
MacConfiguration,
MacController,
{$ENDIF}
{$IFDEF MSWINDOWS}
Tray,
TrayButton,
WindowsConfiguration,
WindowsController,
WindowsShares,
WinPEImageReader,
{$ENDIF}
ConfigurationInterface,
ControllerInterface,
CustApp,
DockerConfiguration,
DockerController,
FileInfo,
StringFunctions,
SysUtils;
type
{ TDockerControl }
TDockerControl = class(TCustomApplication)
private const
COMMAND_CONFIG = 'config';
COMMAND_RESET = 'reset';
COMMAND_RESTART = 'restart';
COMMAND_START = 'start';
COMMAND_STOP = 'stop';
COMMAND_VERSION = 'version';
EXIT_CODE_ERROR = 1;
OPTION_VERSION_LONG = '--version';
OPTION_VERSION_SHORT = '-v';
protected
FController: IControllerInterface;
procedure DoRun; override;
procedure ExecuteConfig;
procedure ExecuteReset;
procedure ExecuteRestart;
procedure ExecuteStart;
procedure ExecuteStop;
procedure ExecuteVersion;
procedure WriteErrorString(const Message: String);
procedure WriteUsageString(const Args: array of String);
end;
{ TDockerControl }
procedure TDockerControl.DoRun;
var
Command: string;
begin
// Print the usage string, if more or less than one command line argument has
// been specified.
if ParamCount < 1 then
begin
WriteUsageString([]);
Terminate(EXIT_CODE_ERROR);
Exit;
end;
// Create a new controller class instance which supports the current operating
// system.
{$IFDEF MSWINDOWS}
FController := TWindowsController.Create;
{$ELSE}
{$IFDEF DARWIN}
FController := TMacController.Create;
{$ELSE}
WriteErrorString('Unsupported operating system');
Terminate(EXIT_CODE_ERROR);
Exit;
{$ENDIF}
{$ENDIF}
// Perform the specified command, if it is valid.
Command := ParamStr(1);
if Command = COMMAND_CONFIG then
ExecuteConfig
else if Command = COMMAND_RESET then
ExecuteReset
else if Command = COMMAND_RESTART then
ExecuteRestart
else if Command = COMMAND_START then
ExecuteStart
else if Command = COMMAND_STOP then
ExecuteStop
else if (Command = COMMAND_VERSION) or
(Command = OPTION_VERSION_LONG) or
(Command = OPTION_VERSION_SHORT) then
ExecuteVersion
else
begin
WriteErrorString(Format('Unknown command ''%s''', [Command]));
WriteUsageString([]);
Terminate(EXIT_CODE_ERROR);
end;
// Ensure that the program is always terminated at this point.
if not Terminated then
Terminate;
end;
procedure TDockerControl.ExecuteConfig;
const
PARAMETER_GET = 'get';
PARAMETER_SET = 'set';
var
I: Integer;
begin
if ParamCount < 3 then
begin
WriteUsageString([
Format('<%s|%s>', [PARAMETER_GET, PARAMETER_SET]),
'<name>'
]);
Terminate(EXIT_CODE_ERROR);
end
else if ParamStr(2) = PARAMETER_GET then
begin
try
for I := 3 to ParamCount do
WriteLn(FController.GetOption(ParamStr(I)));
except
on E: exception do
begin
WriteErrorString(E.Message);
Terminate(EXIT_CODE_ERROR);
end;
end;
end
else if ParamStr(2) = PARAMETER_SET then
begin
if ParamCount < 4 then
begin
WriteUsageString([ParamStr(2), '<name>', '<value>']);
Terminate(EXIT_CODE_ERROR);
end
else
begin
I := 3;
while (I < ParamCount) do
begin
try
FController.SetOption(ParamStr(I), ParamStr(I + 1));
WriteLn(Format('Changed %s to ''%s''', [
ParamStr(I),
FController.GetOption(ParamStr(I))
]));
except
on E: exception do
begin
WriteErrorString(E.Message);
Terminate(EXIT_CODE_ERROR);
Exit;
end;
end;
Inc(I, 2);
end;
end;
end
else
begin
WriteUsageString([
Format('<%s|%s>', [PARAMETER_GET, PARAMETER_SET]),
'<name>'
]);
Terminate(EXIT_CODE_ERROR);
end;
end;
procedure TDockerControl.ExecuteReset;
begin
WriteLn('Resetting the Docker service');
try
if not FController.Reset then
begin
WriteErrorString(FController.GetErrorMessage);
Terminate(EXIT_CODE_ERROR);
end;
except
on E: exception do
WriteErrorString(E.Message);
end;
end;
procedure TDockerControl.ExecuteRestart;
begin
WriteLn('Restarting the Docker service');
try
if not FController.Restart then
begin
WriteErrorString(FController.GetErrorMessage);
Terminate(EXIT_CODE_ERROR);
end;
except
on E: exception do
WriteErrorString(E.Message);
end;
end;
procedure TDockerControl.ExecuteStart;
begin
WriteLn('Starting the Docker service');
try
if not FController.Start then
begin
WriteErrorString(FController.GetErrorMessage);
Terminate(EXIT_CODE_ERROR);
end;
except
on E: exception do
WriteErrorString(E.Message);
end;
end;
procedure TDockerControl.ExecuteStop;
begin
WriteLn('Stopping the Docker service');
try
if not FController.Stop then
begin
WriteErrorString(FController.GetErrorMessage);
Terminate(EXIT_CODE_ERROR);
end;
except
on E: exception do
WriteErrorString(E.Message);
end;
end;
procedure TDockerControl.ExecuteVersion;
var
FileVerInfo: TFileVersionInfo;
begin
FileVerInfo := TFileVersionInfo.Create(nil);
try
// Read the version information from the binary instead of using hardcoded
// values such as constants.
FileVerInfo.ReadFileInfo;
// Write the version information string to the standard output stream.
WriteLn(Format('%s version %s (%s)', [
FileVerInfo.VersionStrings.Values['ProductName'],
Copy(
FileVerInfo.VersionStrings.Values['FileVersion'],
1,
LastDelimiter('.', FileVerInfo.VersionStrings.Values['FileVersion']) - 1
),
{$I %FPCTARGETOS%}
]));
except
on E: exception do
WriteErrorString(E.Message);
end;
FileVerInfo.Free;
end;
procedure TDockerControl.WriteErrorString(const Message: String);
begin
WriteLn(Format('ERROR: %s', [Message]));
end;
procedure TDockerControl.WriteUsageString(const Args: array of String);
var
Command: String;
Index: Integer;
begin
// Remove the path to the executable as well as the extension and use that as
// the command.
Command := ExtractFileName(ParamStr(0));
Index := LastDelimiter('.', Command);
if Index > 0 then
Command := Copy(Command, 1, Index - 1);
// Write the usage string to the standard output stream.
if ParamCount > 0 then
begin
Write(Format('Usage: %s %s', [Command, ParamStr(1)]));
for Index := 0 to High(Args) do
Write(' ' + Args[Index]);
WriteLn;
end
else
begin
WriteLn(Format('Usage: %s <%s|%s|%s|%s|%s|%s>', [
Command,
COMMAND_CONFIG,
COMMAND_RESET,
COMMAND_RESTART,
COMMAND_START,
COMMAND_STOP,
COMMAND_VERSION
]));
end;
end;
var
Application: TDockerControl;
{$R *.res}
begin
Application:=TDockerControl.Create(nil);
Application.Title:='Docker Control';
Application.Run;
Application.Free;
end.