@@ -12,6 +12,14 @@ internal class CertGenerateCommand : Command
1212 public static Option < string > PasswordOption { get ; }
1313 public static Option < int > ValidDaysOption { get ; }
1414 public static Option < bool > InstallOption { get ; }
15+ public static Option < IfExists > IfExistsOption { get ; }
16+
17+ internal enum IfExists
18+ {
19+ Error ,
20+ Overwrite ,
21+ Skip
22+ }
1523
1624 static CertGenerateCommand ( )
1725 {
@@ -45,6 +53,11 @@ static CertGenerateCommand()
4553 Description = "Install the certificate to the local machine store after generation" ,
4654 DefaultValueFactory = ( argumentResult ) => false ,
4755 } ;
56+ IfExistsOption = new Option < IfExists > ( "--if-exists" )
57+ {
58+ Description = "Skip generation if the certificate file already exists" ,
59+ DefaultValueFactory = ( argumentResult ) => IfExists . Error ,
60+ } ;
4861 }
4962
5063 public CertGenerateCommand ( )
@@ -56,7 +69,7 @@ public CertGenerateCommand()
5669 Options . Add ( PasswordOption ) ;
5770 Options . Add ( ValidDaysOption ) ;
5871 Options . Add ( InstallOption ) ;
59- Options . Add ( WinSdkRootCommand . VerboseOption ) ;
72+ Options . Add ( IfExistsOption ) ;
6073 }
6174
6275 public class Handler ( ICertificateService certificateService ) : AsynchronousCommandLineAction
@@ -69,14 +82,26 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
6982 var password = parseResult . GetRequiredValue ( PasswordOption ) ;
7083 var validDays = parseResult . GetRequiredValue ( ValidDaysOption ) ;
7184 var install = parseResult . GetRequiredValue ( InstallOption ) ;
85+ var ifExists = parseResult . GetRequiredValue ( IfExistsOption ) ;
7286 var verbose = parseResult . GetValue ( WinSdkRootCommand . VerboseOption ) ;
7387
7488 // Check if certificate file already exists
7589 if ( File . Exists ( output ) )
7690 {
7791 Console . Error . WriteLine ( $ "❌ Certificate file already exists: { output } ") ;
78- Console . Error . WriteLine ( "Please specify a different output path or remove the existing file." ) ;
79- return 1 ;
92+ if ( ifExists == IfExists . Error )
93+ {
94+ Console . Error . WriteLine ( "Please specify a different output path or remove the existing file." ) ;
95+ return 1 ;
96+ }
97+ else if ( ifExists == IfExists . Skip )
98+ {
99+ return 0 ;
100+ }
101+ else if ( ifExists == IfExists . Overwrite )
102+ {
103+ Console . WriteLine ( $ "⚠️ Overwriting existing certificate file: { output } ") ;
104+ }
80105 }
81106
82107 // Use the consolidated certificate generation method with all console output and error handling
@@ -86,7 +111,7 @@ await certificateService.GenerateDevCertificateWithInferenceAsync(
86111 manifestPath : manifestPath ,
87112 password : password ,
88113 validDays : validDays ,
89- skipIfExists : false , // We already checked above
114+ skipIfExists : false ,
90115 updateGitignore : true ,
91116 install : install ,
92117 quiet : false ,
0 commit comments