-
Notifications
You must be signed in to change notification settings - Fork 2
/
MbrCommandFactory.cs
223 lines (176 loc) · 8.68 KB
/
MbrCommandFactory.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
using Hst.Imager.Core.Models;
namespace Hst.Imager.ConsoleApp
{
using System.CommandLine;
public static class MbrCommandFactory
{
public static Command CreateMbrCommand()
{
var command = new Command("mbr", "Master Boot Record.");
command.AddCommand(CreateMbrInfo());
command.AddCommand(CreateMbrInit());
command.AddCommand(CreateMbrPart());
return command;
}
private static Command CreateMbrInfo()
{
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 Master Boot Record.");
command.SetHandler(CommandHandler.MbrInfo, pathArgument, showUnallocatedOption);
command.AddArgument(pathArgument);
command.AddOption(showUnallocatedOption);
return command;
}
private static Command CreateMbrInit()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var mbrInitCommand = new Command("initialize", "Initialize disk with empty Master Boot Record.");
mbrInitCommand.AddAlias("init");
mbrInitCommand.SetHandler(CommandHandler.MbrInit, pathArgument);
mbrInitCommand.AddArgument(pathArgument);
return mbrInitCommand;
}
private static Command CreateMbrPart()
{
var partCommand = new Command("part", "Partition.");
partCommand.AddCommand(CreateMbrPartAdd());
partCommand.AddCommand(CreateMbrPartDel());
partCommand.AddCommand(CreateMbrPartFormat());
partCommand.AddCommand(CreateMbrPartExport());
partCommand.AddCommand(CreateMbrPartImport());
partCommand.AddCommand(CreateMbrPartClone());
return partCommand;
}
private static Command CreateMbrPartAdd()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var typeArgument = new Argument<string>(
name: "Type",
description: "Type of the partition as name or number (e.g. name FAT32 or value 0xb for FAT32).");
var sizeArgument = new Argument<string>(
name: "Size",
description: "Size of the partition.");
var startSectorOption = new Option<long?>(
new[] { "--start-sector", "-s" },
description: "Start sector.");
var activeOption = new Option<bool>(
new[] { "--active", "-a" },
description: "Set partition active (bootable).",
getDefaultValue: () => false);
var mbrPartAddCommand = new Command("add", "Add partition.");
mbrPartAddCommand.SetHandler(CommandHandler.MbrPartAdd, pathArgument, typeArgument, sizeArgument,
startSectorOption, activeOption);
mbrPartAddCommand.AddArgument(pathArgument);
mbrPartAddCommand.AddArgument(typeArgument);
mbrPartAddCommand.AddArgument(sizeArgument);
mbrPartAddCommand.AddOption(startSectorOption);
mbrPartAddCommand.AddOption(activeOption);
return mbrPartAddCommand;
}
private static Command CreateMbrPartDel()
{
var path = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var partitionNumber = new Argument<int>(
name: "PartitionNumber",
description: "Partition number to delete.");
var command = new Command("delete", "Delete partition.");
command.AddAlias("del");
command.SetHandler(CommandHandler.MbrPartDel, path, partitionNumber);
command.AddArgument(path);
command.AddArgument(partitionNumber);
return command;
}
private static Command CreateMbrPartFormat()
{
var pathArgument = new Argument<string>(
name: "Path",
description: "Path to physical drive or image file.");
var partitionNumberArgument = new Argument<int>(
name: "PartitionNumber",
description: "Partition number to delete.");
var nameArgument = new Argument<string>(
name: "Name",
description: "Name of the partition.");
var fileSystemOption = new Option<string>(
new[] { "--file-system", "-fs" },
description: "File system format partition with.");
var formatCommand = new Command("format", "Format partition.");
formatCommand.SetHandler(CommandHandler.MbrPartFormat, pathArgument, partitionNumberArgument, nameArgument, fileSystemOption);
formatCommand.AddArgument(pathArgument);
formatCommand.AddArgument(partitionNumberArgument);
formatCommand.AddArgument(nameArgument);
return formatCommand;
}
private static Command CreateMbrPartExport()
{
var sourcePathArgument = new Argument<string>(
name: "SourcePath",
description: "Path to source physical drive or image file.");
var partition = new Argument<string>(
name: "Partition",
description: "Partition to export from (\"2\" for partition number 2 or \"fat32\" for first fat32 partition).");
var destinationPathArgument = new Argument<string>(
name: "DestinationPath",
description: "Path to destination physical drive or image file.");
var command = new Command("export", "Export partition to a file.");
command.SetHandler(CommandHandler.MbrPartExport, sourcePathArgument, partition,
destinationPathArgument);
command.AddArgument(sourcePathArgument);
command.AddArgument(partition);
command.AddArgument(destinationPathArgument);
return command;
}
private static Command CreateMbrPartImport()
{
var sourcePathArgument = new Argument<string>(
name: "SourcePath",
description: "Path to source file.");
var destinationPathArgument = new Argument<string>(
name: "DestinationPath",
description: "Path to destination physical drive or image file.");
var partition = new Argument<string>(
name: "Partition",
description: "Partition to import to (\"2\" for partition number 2 or \"fat32\" for first fat32 partition).");
var command = new Command("import", "Import partition from a file.");
command.SetHandler(CommandHandler.MbrPartImport, sourcePathArgument, destinationPathArgument, partition);
command.AddArgument(sourcePathArgument);
command.AddArgument(destinationPathArgument);
command.AddArgument(partition);
return command;
}
private static Command CreateMbrPartClone()
{
var srcPathArgument = new Argument<string>(
name: "SourcePath",
description: "Path to source physical drive or image file.");
var srcPartitionNumber = new Argument<int>(
name: "Partition",
description: "Source partition to clone from.");
var destPathArgument = new Argument<string>(
name: "DestinationPath",
description: "Path to destination physical drive or image file.");
var destPartitionNumber = new Argument<int>(
name: "DestinationPartitionNumber",
description: "Destination partition number to clone to.");
var command = new Command("clone", "Clone partition from a physical drive or image file.");
command.SetHandler(CommandHandler.MbrPartClone, srcPathArgument, srcPartitionNumber, destPathArgument, destPartitionNumber);
command.AddArgument(srcPathArgument);
command.AddArgument(srcPartitionNumber);
command.AddArgument(destPathArgument);
command.AddArgument(destPartitionNumber);
return command;
}
}
}