@@ -18,7 +18,42 @@ public abstract partial class CommandProvider<T>
1818 /// Initializes the bundle.
1919 /// </summary>
2020 /// <param name="bundlePath">Path of the bundle.</param>
21- public abstract void InitializeBundle ( string bundlePath ) ;
21+ protected abstract void InitializeBundle ( string bundlePath ) ;
22+
23+ /// <summary>
24+ /// Loads the bundle from file and handles load errors.
25+ /// </summary>
26+ /// <param name="readOnly">
27+ /// A value indicating whether to load the bundle in read-only mode.
28+ /// </param>
29+ protected bool LoadBundle ( bool readOnly = true )
30+ {
31+ if ( Bundle == null )
32+ {
33+ throw new ApplicationException ( "Bundle is not initialized" ) ;
34+ }
35+
36+ try
37+ {
38+ Bundle . LoadFromFile ( readOnly ) ;
39+
40+ if ( ! string . IsNullOrEmpty ( Bundle . Manifest . UpdatedBy ) && Bundle . Manifest . UpdatedBy != Bundle . GetType ( ) . FullName )
41+ {
42+ Logger . LogWarning ( "Bundle was created by a different application" ) ;
43+ AnsiConsole . MarkupLine ( $ "[{ Color . Orange1 } ]Warning:[/] Bundle was created by a different application") ;
44+ }
45+
46+ return true ;
47+ }
48+ catch ( Exception ex )
49+ {
50+ Logger . LogError ( ex , "Failed to load bundle from file: {BundlePath}" , Bundle . BundlePath ) ;
51+ AnsiConsole . MarkupLine ( $ "[{ Color . Red } ]Failed to load bundle from file: { Bundle . BundlePath } [/]") ;
52+ AnsiConsole . MarkupLine ( $ "[{ Color . Red } ]Error:[/] { ex . GetType ( ) . Name } : { ex . Message } ") ;
53+ }
54+
55+ return false ;
56+ }
2257
2358 /// <summary>
2459 /// Runs the add command.
@@ -45,7 +80,8 @@ protected virtual void RunAdd(StatusContext statusContext, bool replace, bool co
4580 {
4681 Logger . LogDebug ( "A bundle file exists, loading bundle" ) ;
4782 statusContext . Status ( "[yellow]Loading Bundle[/]" ) ;
48- Bundle . LoadFromFile ( false ) ;
83+
84+ if ( ! LoadBundle ( false ) ) return ;
4985 }
5086
5187 statusContext . Status ( "[yellow]Adding Files[/]" ) ;
@@ -151,7 +187,7 @@ protected virtual void RunSign(StatusContext statusContext, X509Certificate2Coll
151187
152188 Logger . LogDebug ( "Loading bundle" ) ;
153189 statusContext . Status ( "[yellow]Loading Bundle[/]" ) ;
154- Bundle . LoadFromFile ( false ) ;
190+ if ( ! LoadBundle ( false ) ) return ;
155191
156192 int divider = 0 ;
157193 int signs = 0 ;
@@ -194,7 +230,7 @@ protected virtual void RunSign(StatusContext statusContext, X509Certificate2Coll
194230 if ( prvKey == null )
195231 {
196232 Logger . LogError ( "Failed to acquire RSA private key for {cert}" , cert ) ;
197- AnsiConsole . MarkupLine ( $ "[{ Color . Green } ] Failed to Acquire RSA Private Key[/]") ;
233+ AnsiConsole . MarkupLine ( $ "[{ Color . Red } ] Failed to Acquire RSA Private Key[/]") ;
198234 continue ;
199235 }
200236
@@ -246,7 +282,7 @@ protected virtual void RunVerify(StatusContext statusContext)
246282
247283 Logger . LogDebug ( "Loading bundle" ) ;
248284 statusContext . Status ( "[yellow]Loading Bundle[/]" ) ;
249- Bundle . LoadFromFile ( ) ;
285+ if ( ! LoadBundle ( ) ) return ;
250286
251287 Logger . LogInformation ( "Starting certificate and signature verification" ) ;
252288 statusContext . Status ( "[yellow]Verification Phase 1: Certificates and signatures[/]" ) ;
@@ -414,7 +450,7 @@ protected bool VerifyCertificate(X509Certificate2 certificate)
414450
415451 Logger . LogInformation ( "Certificate verification with self-signing root CA for {cert}: {result}" , certificate , selfSignVerification ) ;
416452
417- if ( ! selfSignVerification )
453+ if ( selfSignVerification )
418454 {
419455 AnsiConsole . MarkupLine ( $ "[{ Color . Green } ] Certificate Verification with Self-Signing Root CA Successful[/]") ;
420456 return true ;
0 commit comments