-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandFactory.cs
396 lines (320 loc) · 15.8 KB
/
CommandFactory.cs
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
namespace Hst.Imager.ConsoleApp
{
using System.CommandLine;
using System.IO;
using Core.Commands;
using Core.Models;
public static class CommandFactory
{
public static readonly Option<FileInfo> LogFileOption = new(
new[] { "--log-file" },
description: "Write log file.");
public static readonly Option<bool> VerboseOption = new(
new[] { "--verbose" },
description: "Verbose output.");
public static readonly Option<FormatEnum> FormatOption = new(
new[] { "--format", "-f" },
description: "Format of output.",
getDefaultValue: () => FormatEnum.Table);
public static Command CreateRootCommand()
{
var rootCommand = new RootCommand
{
Description = "Hst Imager reads, writes and initializes image files and physical drives."
};
rootCommand.AddGlobalOption(LogFileOption);
rootCommand.AddGlobalOption(VerboseOption);
rootCommand.AddCommand(CreateBlankCommand());
rootCommand.AddCommand(CreateConvertCommand());
rootCommand.AddCommand(CreateFormatCommand());
rootCommand.AddCommand(CreateInfoCommand());
rootCommand.AddCommand(CreateListCommand());
rootCommand.AddCommand(CreateOptimizeCommand());
rootCommand.AddCommand(CreateReadCommand());
rootCommand.AddCommand(CreateScriptCommand());
rootCommand.AddCommand(CreateBlockCommand());
rootCommand.AddCommand(CreateCompareCommand());
rootCommand.AddCommand(CreateWriteCommand());
rootCommand.AddCommand(GptCommandFactory.CreateGptCommand());
rootCommand.AddCommand(MbrCommandFactory.CreateMbrCommand());
rootCommand.AddCommand(RdbCommandFactory.CreateRdbCommand());
rootCommand.AddCommand(FsCommandFactory.CreateFsCommand());
rootCommand.AddCommand(AdfCommandFactory.CreateAdfCommand());
return rootCommand;
}
public static Command CreateScriptCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to script file.");
var scriptCommand = new Command("script", "Run script.");
scriptCommand.AddArgument(pathArgument);
scriptCommand.SetHandler(CommandHandler.Script, pathArgument);
return scriptCommand;
}
public static Command CreateInfoCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var showUnallocatedOption = new Option<bool>(
new[] { "--unallocated", "-u" },
description: "Show unallocated.",
getDefaultValue: () => true);
var command = new Command("info", "Display info about physical drive or image file.");
command.AddArgument(pathArgument);
command.AddOption(showUnallocatedOption);
command.SetHandler(CommandHandler.Info, pathArgument, showUnallocatedOption);
return command;
}
public static Command CreateListCommand()
{
var allOption = new Option<bool>(
new[] { "--all", "-a" },
description: "Show all physical drives.",
getDefaultValue: () => false);
var listCommand = new Command("list", "Display list of physical drives.");
listCommand.AddOption(allOption);
listCommand.SetHandler(CommandHandler.List, allOption);
return listCommand;
}
public static Command CreateWriteCommand()
{
var sourceArgument = new Argument<string>(
name: "Source",
description: "Path to source image file.");
var destinationArgument = new Argument<string>(
name: "Destination",
description: "Path to destination physical drive.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size of image file to write.");
var retriesOption = new Option<int>(
new[] { "--retries", "-r" },
description: "Number of retries to try read or write data.",
getDefaultValue: () => 5);
var verifyOption = new Option<bool>(
new[] { "--verify", "-v" },
description: "Verify data written.");
var forceOption = new Option<bool>(
new[] { "--force", "-f" },
description: "Force write to ignore write errors.",
getDefaultValue: () => false);
var writeCommand = new Command("write", "Write image file to physical drive.");
writeCommand.AddArgument(sourceArgument);
writeCommand.AddArgument(destinationArgument);
writeCommand.AddOption(sizeOption);
writeCommand.AddOption(retriesOption);
writeCommand.AddOption(verifyOption);
writeCommand.AddOption(forceOption);
writeCommand.SetHandler(CommandHandler.Write, sourceArgument, destinationArgument, sizeOption,
retriesOption, verifyOption, forceOption);
return writeCommand;
}
public static Command CreateReadCommand()
{
var sourceArgument = new Argument<string>(
name: "Source",
description: "Path to source physical drive.");
var destinationArgument = new Argument<string>(
name: "Destination",
description: "Path to destination image file.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size of physical drive to read.");
var retriesOption = new Option<int>(
new[] { "--retries", "-r" },
description: "Number of retries to try read or write data.",
getDefaultValue: () => 5);
var verifyOption = new Option<bool>(
new[] { "--verify", "-v" },
description: "Verify data read.");
var forceOption = new Option<bool>(
new[] { "--force", "-f" },
description: "Force read to ignore read errors.",
getDefaultValue: () => false);
var startOption = new Option<long?>(
new[] { "--start", "-st" },
description: "Start offset.");
var readCommand = new Command("read", "Read physical drive to image file.");
readCommand.AddArgument(sourceArgument);
readCommand.AddArgument(destinationArgument);
readCommand.AddOption(sizeOption);
readCommand.AddOption(retriesOption);
readCommand.AddOption(verifyOption);
readCommand.AddOption(forceOption);
readCommand.AddOption(startOption);
readCommand.SetHandler(CommandHandler.Read, sourceArgument, destinationArgument, sizeOption, retriesOption,
verifyOption, forceOption, startOption);
return readCommand;
}
public static Command CreateConvertCommand()
{
var sourceArgument = new Argument<string>(
name: "Source",
description: "Path to source image file.");
var destinationArgument = new Argument<string>(
name: "Destination",
description: "Path to destination image file.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size of image file convert.");
var verifyOption = new Option<bool>(
new[] { "--verify", "-v" },
description: "Verify data converted.");
var convertCommand = new Command("convert", "Convert image file.");
convertCommand.AddArgument(sourceArgument);
convertCommand.AddArgument(destinationArgument);
convertCommand.AddOption(sizeOption);
convertCommand.AddOption(verifyOption);
convertCommand.SetHandler(CommandHandler.Convert, sourceArgument, destinationArgument, sizeOption, verifyOption);
return convertCommand;
}
public static Command CreateFormatCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var partitionTableArgument = new Argument<PartitionTable>(
name: "PartitionTable",
description: "Partition table to create.");
var fileSystemArgument = new Argument<string>(
name: "FileSystem",
description: "File system to format partition created.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size of disk to format.");
var command = new Command("format", "Format physical drive or image file.");
command.AddArgument(pathArgument);
command.AddArgument(partitionTableArgument);
command.AddArgument(fileSystemArgument);
command.AddOption(sizeOption);
command.SetHandler(CommandHandler.Format, pathArgument, partitionTableArgument, fileSystemArgument, sizeOption);
return command;
}
public static Command CreateBlankCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path image file.");
var sizeArgument = new Argument<string>(
name: "Size",
description: "Size of image file.");
var compatibleSizeOption = new Option<bool>(
new[] { "--compatible", "-c" },
description: "Make size compatible by reducing it with 5%.",
getDefaultValue: () => false);
var blankCommand = new Command("blank", "Blank image file.");
blankCommand.AddArgument(pathArgument);
blankCommand.AddArgument(sizeArgument);
blankCommand.AddOption(compatibleSizeOption);
blankCommand.SetHandler(CommandHandler.Blank, pathArgument, sizeArgument, compatibleSizeOption);
return blankCommand;
}
public static Command CreateOptimizeCommand()
{
var pathArgument = new Argument<string>(
name: "Source",
description: "Path to image file.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size to optimize to.");
var partitionTableOption = new Option<PartitionTable>(
new[] { "--partition-table", "-pt" },
description: "Optimize to size of partition table.",
getDefaultValue: () => PartitionTable.None);
var convertCommand = new Command("optimize", "Optimize image file size.");
convertCommand.AddArgument(pathArgument);
convertCommand.AddOption(sizeOption);
convertCommand.AddOption(partitionTableOption);
convertCommand.SetHandler(CommandHandler.Optimize, pathArgument, sizeOption, partitionTableOption);
return convertCommand;
}
public static Command CreateCompareCommand()
{
var sourceArgument = new Argument<string>(
name: "Source",
description: "Path to source physical drive or image file.");
var destinationArgument = new Argument<string>(
name: "Destination",
description: "Path to destination physical drive or image file.");
var sizeOption = new Option<string>(
new[] { "--size", "-s" },
description: "Size to verify.");
var retriesOption = new Option<int>(
new[] { "--retries", "-r" },
description: "Number of retries to try read or write data.",
getDefaultValue: () => 5);
var forceOption = new Option<bool>(
new[] { "--force", "-f" },
description: "Force compare to ignore read errors.",
getDefaultValue: () => false);
var convertCommand = new Command("compare", "Compare source and destination.");
convertCommand.AddArgument(sourceArgument);
convertCommand.AddArgument(destinationArgument);
convertCommand.AddOption(sizeOption);
convertCommand.AddOption(retriesOption);
convertCommand.AddOption(forceOption);
convertCommand.SetHandler(CommandHandler.Compare, sourceArgument, destinationArgument, sizeOption, retriesOption, forceOption);
return convertCommand;
}
public static Command CreateBlockCommand()
{
var command = new Command("block", "Block.");
command.AddCommand(CreateBlockReadCommand());
command.AddCommand(CreateBlockViewCommand());
return command;
}
public static Command CreateBlockReadCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var outputPathArgument = new Argument<string>(
name: "OutputPath",
description: "Output path to write sectors.");
var blockSizeOption = new Option<int>(
new[] { "--block-size", "-bs" },
description: "Block size.",
getDefaultValue: () => 512);
var usedOption = new Option<bool>(
new[] { "--used", "-u" },
description: "Only used blocks.");
var startOption = new Option<long?>(
new[] { "--start", "-s" },
description: "Start offset.");
var endOption = new Option<long?>(
new[] { "--end", "-e" },
description: "End offset.");
var blankCommand = new Command("read", "Read blocks to file per block.");
blankCommand.SetHandler(CommandHandler.BlockRead, pathArgument, outputPathArgument, blockSizeOption,
usedOption, startOption, endOption);
blankCommand.AddArgument(pathArgument);
blankCommand.AddArgument(outputPathArgument);
blankCommand.AddOption(blockSizeOption);
blankCommand.AddOption(usedOption);
blankCommand.AddOption(startOption);
blankCommand.AddOption(endOption);
return blankCommand;
}
public static Command CreateBlockViewCommand()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var blockSizeOption = new Option<int>(
new[] { "--block-size", "-bs" },
description: "Block size.",
getDefaultValue: () => 512);
var startOption = new Option<long?>(
new[] { "--start", "-s" },
description: "Start offset.");
var blankCommand = new Command("view", "View block as hex.");
blankCommand.SetHandler(CommandHandler.BlockView, pathArgument, blockSizeOption, startOption);
blankCommand.AddArgument(pathArgument);
blankCommand.AddOption(blockSizeOption);
blankCommand.AddOption(startOption);
return blankCommand;
}
}
}