Skip to content

Commit 42ea8f9

Browse files
committed
Add ability to stop adding process if any errors occurred
1 parent 153c78a commit 42ea8f9

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/EasySign.CommandLine/BundleWorker.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ public abstract partial class CommandProvider<T>
2424
/// <summary>
2525
/// Runs the add command.
2626
/// </summary>
27-
/// <param name="statusContext">The status context for interacting with <see cref="AnsiConsole.Status"/>.</param>
28-
/// <param name="replace">A value indicating whether to replace existing entries.</param>
29-
protected virtual void RunAdd(StatusContext statusContext, bool replace)
27+
/// <param name="statusContext">
28+
/// The status context for interacting with <see cref="AnsiConsole.Status"/>.
29+
/// </param>
30+
/// <param name="replace">
31+
/// A value indicating whether to replace existing entries.
32+
/// </param>
33+
/// <param name="continueOnError">
34+
/// A value indicating whether to continue adding files if an error occurs.
35+
/// </param>
36+
protected virtual void RunAdd(StatusContext statusContext, bool replace, bool continueOnError)
3037
{
3138
if (Bundle == null)
3239
{
@@ -41,7 +48,8 @@ protected virtual void RunAdd(StatusContext statusContext, bool replace)
4148

4249
statusContext.Status("[yellow]Adding Files[/]");
4350

44-
_ = Parallel.ForEach(Utilities.SafeEnumerateFiles(Bundle.RootPath, "*"), file =>
51+
bool errorOccurred = false;
52+
_ = Parallel.ForEach(Utilities.SafeEnumerateFiles(Bundle.RootPath, "*"), (file, state) =>
4553
{
4654
if (file == Bundle.BundlePath) return;
4755
var entryName = Manifest.GetNormalizedEntryName(Path.GetRelativePath(Bundle.RootPath, file));
@@ -68,10 +76,29 @@ protected virtual void RunAdd(StatusContext statusContext, bool replace)
6876
}
6977
catch (Exception ex)
7078
{
79+
errorOccurred = true;
80+
7181
AnsiConsole.MarkupLine($"[{Color.Red}]Error:[/] {entryName} ({ex.GetType().Name}: {ex.Message})");
82+
83+
if (!continueOnError)
84+
{
85+
state.Stop();
86+
}
7287
}
7388
});
7489

90+
if (errorOccurred)
91+
{
92+
AnsiConsole.WriteLine();
93+
AnsiConsole.MarkupLine("[red]One or more errors occurred, check the console output or logs for more information[/]");
94+
95+
if (!continueOnError)
96+
{
97+
AnsiConsole.MarkupLine("[red]No changes were made to the bundle[/]");
98+
return;
99+
}
100+
}
101+
75102
statusContext.Status("[yellow]Saving Bundle[/]");
76103
Bundle.Update();
77104
}

src/EasySign.CommandLine/CommandProvider.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ public Command Add
3636
var replaceOpt = new Option<bool>("--replace", "Replace existing entries");
3737
replaceOpt.AddAlias("-r");
3838

39+
var continueOpt = new Option<bool>("--continue", "Continue adding files if an error occurs");
40+
continueOpt.AddAlias("-c");
41+
3942
var command = new Command("add", "Create new bundle or update an existing one")
4043
{
4144
BundlePath,
4245
replaceOpt,
46+
continueOpt,
4347
};
4448

45-
command.SetHandler((bundlePath, replace) =>
49+
command.SetHandler((bundlePath, replace, continueOnError) =>
4650
{
4751
InitializeBundle(bundlePath);
48-
Utilities.RunInStatusContext("[yellow]Preparing[/]", ctx => RunAdd(ctx, replace));
49-
}, BundlePath, replaceOpt);
52+
Utilities.RunInStatusContext("[yellow]Preparing[/]", ctx => RunAdd(ctx, replace, continueOnError));
53+
}, BundlePath, replaceOpt, continueOpt);
5054

5155
return command;
5256
}

0 commit comments

Comments
 (0)