Skip to content

Commit 436a4ea

Browse files
committed
Improve error handling
1 parent 503a84e commit 436a4ea

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/EasySign.CommandLine/BundleWorker.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,33 @@ protected virtual void RunAdd(StatusContext statusContext, string[] files, bool
9494
if (files.Length == 0)
9595
{
9696
Logger.LogDebug("Discovering files in the directory: {RootPath}", Bundle.RootPath);
97-
files = Utilities.SafeEnumerateFiles(Bundle.RootPath, "*", recursive).ToArray();
98-
Logger.LogInformation("Discovered {FileCount} files", files.Length);
97+
98+
if ((files = Utilities.SafeEnumerateFiles(Bundle.RootPath, "*", recursive).ToArray()).Length == 0)
99+
{
100+
Logger.LogWarning("No files found in the directory: {RootPath}", Bundle.RootPath);
101+
AnsiConsole.MarkupLine($"[{Color.Red}]No files found in the directory: {Bundle.RootPath}[/]");
102+
return;
103+
}
104+
else
105+
{
106+
Logger.LogInformation("Discovered {FileCount} files in the directory: {RootPath}", files.Length, Bundle.RootPath);
107+
AnsiConsole.MarkupLine($"[{Color.Green}]Discovered {files.Length} files in the directory: {Bundle.RootPath}[/]");
108+
}
99109
}
100110

101111
Logger.LogInformation("Starting file adder multi-thread task");
102112
bool errorOccurred = false;
113+
bool bundleUpdated = false;
114+
103115
_ = Parallel.ForEach(files, (file, state) =>
104116
{
105-
if (file == Bundle.BundlePath) return;
117+
if (file == Bundle.BundlePath)
118+
{
119+
Logger.LogWarning("File {file} is the bundle file itself", file);
120+
AnsiConsole.MarkupLine($"[{Color.Yellow}]Ignored:[/] File {file} is the bundle file itself");
121+
return;
122+
}
123+
106124
string entryName = Manifest.GetNormalizedEntryName(Path.GetRelativePath(Bundle.RootPath, file));
107125

108126
Logger.LogInformation("Processing file: {EntryName}", entryName);
@@ -129,6 +147,7 @@ protected virtual void RunAdd(StatusContext statusContext, string[] files, bool
129147

130148
Bundle.DeleteEntry(entryName);
131149
Bundle.AddEntry(file);
150+
bundleUpdated = true;
132151

133152
Logger.LogInformation("Entry: {EntryName} Replaced", entryName);
134153
AnsiConsole.MarkupLine($"[{Color.Cyan2}]Replaced:[/] {entryName}");
@@ -138,6 +157,7 @@ protected virtual void RunAdd(StatusContext statusContext, string[] files, bool
138157
Logger.LogDebug("Adding entry: {EntryName}", entryName);
139158

140159
Bundle.AddEntry(file);
160+
bundleUpdated = true;
141161

142162
Logger.LogInformation("Entry: {EntryName} Added", entryName);
143163
AnsiConsole.MarkupLine($"[blue]Added:[/] {entryName}");
@@ -161,22 +181,24 @@ protected virtual void RunAdd(StatusContext statusContext, string[] files, bool
161181
if (errorOccurred)
162182
{
163183
AnsiConsole.WriteLine();
164-
AnsiConsole.MarkupLine("[red]One or more errors occurred, check the console output or logs for more information[/]");
165-
166-
if (!continueOnError)
167-
{
168-
Logger.LogWarning("Add operation aborted");
169-
AnsiConsole.MarkupLine("[red]No changes were made to the bundle[/]");
170-
return;
171-
}
184+
AnsiConsole.MarkupLine("[orange]One or more errors occurred, check the console output or logs for more information[/]");
172185
}
173186

174-
Logger.LogInformation("Saving bundle");
175-
statusContext.Status("[yellow]Saving Bundle[/]");
176-
Bundle.Update();
187+
if (bundleUpdated && (continueOnError || !errorOccurred))
188+
{
189+
Logger.LogInformation("Saving bundle");
190+
statusContext.Status("[yellow]Saving Bundle[/]");
177191

178-
Logger.LogInformation("Bundle saved successfully");
179-
AnsiConsole.MarkupLine($"[green]Bundle file: {Bundle.BundlePath} Saved successfully[/]");
192+
Bundle.Update();
193+
194+
Logger.LogInformation("Bundle saved successfully");
195+
AnsiConsole.MarkupLine($"[green]Bundle file: {Bundle.BundlePath} Saved successfully[/]");
196+
}
197+
else
198+
{
199+
Logger.LogInformation("No changes were made to the bundle");
200+
AnsiConsole.MarkupLine("[yellow]No changes were made to the bundle[/]");
201+
}
180202
}
181203

182204
/// <summary>

0 commit comments

Comments
 (0)