Skip to content

Commit 2b105ec

Browse files
committed
Add --unreserve option for rollback
If something goes wrong, this will allow us to rollback the bulk reservations.
1 parent daf4172 commit 2b105ec

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/GalleryTools/Commands/ReserveNamespacesCommand.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ public static void Configure(CommandLineApplication config)
3636
$"The duration in seconds to sleep between each reservation (default: 0).",
3737
CommandOptionType.SingleValue);
3838

39+
var unreserveOption = config.Option(
40+
"-u | --unreserve",
41+
$"Unreserve namespaces, instead of reserving them. Meant for a rollback of bulk reserving. Note that you must clear the progress file to reuse the same input file.",
42+
CommandOptionType.NoValue);
43+
3944
config.OnExecute(() =>
4045
{
41-
return ExecuteAsync(pathOptions, sleepDurationOption).GetAwaiter().GetResult();
46+
return ExecuteAsync(pathOptions, sleepDurationOption, unreserveOption).GetAwaiter().GetResult();
4247
});
4348
}
4449

4550
private static async Task<int> ExecuteAsync(
4651
CommandOption pathOption,
47-
CommandOption sleepDurationOption)
52+
CommandOption sleepDurationOption,
53+
CommandOption unreserveOption)
4854
{
4955
if (!pathOption.HasValue())
5056
{
@@ -64,9 +70,12 @@ private static async Task<int> ExecuteAsync(
6470
}
6571
}
6672

73+
var unreserve = unreserveOption.HasValue();
74+
6775
var path = pathOption.Value();
6876
var completedPath = path + ".progress";
6977
var remainingList = GetRemainingList(path, completedPath);
78+
Console.WriteLine($"{remainingList.Count} reserved namespaces(s) to {(unreserve ? "remove" : "add")}.");
7079
if (!remainingList.Any())
7180
{
7281
Console.WriteLine("No namespaces were found to reserve.");
@@ -82,20 +91,36 @@ private static async Task<int> ExecuteAsync(
8291
var totalCounter = 0;
8392
foreach (var reservedNamespace in remainingList)
8493
{
85-
Console.Write($"Reserving '{reservedNamespace.Value}' IsPrefix = {reservedNamespace.IsPrefix}...");
94+
Console.Write($"{(unreserve ? "Removing" : "Adding")} '{reservedNamespace.Value}' IsPrefix = {reservedNamespace.IsPrefix}...");
8695
try
8796
{
8897
var matching = service
8998
.FindReservedNamespacesForPrefixList(new[] { reservedNamespace.Value })
9099
.SingleOrDefault(x => ReservedNamespaceComparer.Instance.Equals(x, reservedNamespace));
91-
if (matching != null)
100+
101+
if (unreserve)
92102
{
93-
Console.WriteLine(" already exists.");
94-
AppendReservedNamespace(completedPath, reservedNamespace);
95-
continue;
103+
if (matching == null)
104+
{
105+
Console.WriteLine(" does not exist.");
106+
AppendReservedNamespace(completedPath, reservedNamespace);
107+
continue;
108+
}
109+
110+
await service.DeleteReservedNamespaceAsync(reservedNamespace.Value);
111+
}
112+
else
113+
{
114+
if (matching != null)
115+
{
116+
Console.WriteLine(" already exists.");
117+
AppendReservedNamespace(completedPath, reservedNamespace);
118+
continue;
119+
}
120+
121+
await service.AddReservedNamespaceAsync(reservedNamespace);
96122
}
97123

98-
await service.AddReservedNamespaceAsync(reservedNamespace);
99124
AppendReservedNamespace(completedPath, reservedNamespace);
100125
totalCounter++;
101126
Console.WriteLine(" done.");
@@ -113,7 +138,7 @@ private static async Task<int> ExecuteAsync(
113138
}
114139
}
115140

116-
Console.WriteLine($"All done. Added {totalCounter} reserved namespace(s).");
141+
Console.WriteLine($"All done. {(unreserve ? "Removed" : "Added")} {totalCounter} reserved namespace(s).");
117142

118143
return 0;
119144
}
@@ -135,7 +160,6 @@ private static List<ReservedNamespace> GetRemainingList(string path, string comp
135160
{
136161
Console.WriteLine($"{all.Count - remaining.Count} reserved namespaces(s) are already done.");
137162
}
138-
Console.WriteLine($"{remaining.Count} reserved namespaces(s) to add.");
139163

140164
return remaining;
141165
}

0 commit comments

Comments
 (0)