11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . IO ;
54using System . Linq ;
65using System . Runtime . InteropServices ;
76using System . Threading . Tasks ;
87using Azure . Storage . Blobs ;
8+ using CliWrap ;
99using FluentAssertions ;
1010using Newtonsoft . Json . Linq ;
1111using OctoshiftCLI . Services ;
@@ -526,7 +526,7 @@ public static string GetOsName()
526526 : throw new InvalidOperationException ( "Could not determine OS" ) ;
527527 }
528528
529- public async Task RunCliMigration ( string generateScriptCommand , string cliName , IDictionary < string , string > tokens )
529+ public async Task RunCliMigration ( string generateScriptCommand , string cliName , IReadOnlyDictionary < string , string > tokens )
530530 {
531531 await RunCliCommand ( generateScriptCommand , cliName , tokens ) ;
532532 LogMigrationScript ( "migrate.ps1" ) ;
@@ -541,51 +541,34 @@ private void LogMigrationScript(string filename)
541541 _output . WriteLine ( scriptContents ) ;
542542 }
543543
544- public async Task RunPowershellScript ( string script , IDictionary < string , string > tokens ) =>
544+ public async Task RunPowershellScript ( string script , IReadOnlyDictionary < string , string > tokens ) =>
545545 await RunShellCommand ( $ "-File { Path . Join ( GetOsDistPath ( ) , script ) } ", "pwsh" , GetOsDistPath ( ) , tokens ) ;
546546
547- public async Task RunCliCommand ( string command , string cliName , IDictionary < string , string > tokens ) =>
547+ public async Task RunCliCommand ( string command , string cliName , IReadOnlyDictionary < string , string > tokens ) =>
548548 await RunShellCommand ( command , cliName , GetOsDistPath ( ) , tokens ) ;
549549
550- private async Task RunShellCommand ( string command , string fileName , string workingDirectory = null , IDictionary < string , string > environmentVariables = null )
550+ private async Task RunShellCommand ( string command , string fileName , string workingDirectory = null , IReadOnlyDictionary < string , string > environmentVariables = null )
551551 {
552- var startInfo = new ProcessStartInfo
553- {
554- WorkingDirectory = workingDirectory ?? Directory . GetCurrentDirectory ( ) ,
555- FileName = fileName ,
556- Arguments = command
557- } ;
558-
559- if ( environmentVariables != null )
560- {
561- foreach ( var token in environmentVariables )
562- {
563- if ( startInfo . EnvironmentVariables . ContainsKey ( token . Key ) )
564- {
565- startInfo . EnvironmentVariables [ token . Key ] = token . Value ;
566- }
567- else
568- {
569- startInfo . EnvironmentVariables . Add ( token . Key , token . Value ) ;
570- }
571- }
572- }
573-
574- _output . WriteLine ( $ "Running command: { startInfo . FileName } { startInfo . Arguments } ") ;
552+ _output . WriteLine ( $ "Running command: { fileName } { command } ") ;
575553
576- var p = Process . Start ( startInfo ) ;
577- await p . WaitForExitAsync ( ) ;
554+ var result = await Cli . Wrap ( fileName )
555+ . WithArguments ( command )
556+ . WithWorkingDirectory ( workingDirectory ?? Directory . GetCurrentDirectory ( ) )
557+ . WithEnvironmentVariables ( environmentVariables ?? new Dictionary < string , string > ( ) )
558+ . WithStandardOutputPipe ( PipeTarget . ToDelegate ( line => _output . WriteLine ( line ) ) )
559+ . WithStandardErrorPipe ( PipeTarget . ToDelegate ( line => _output . WriteLine ( line ) ) )
560+ . ExecuteAsync ( ) ;
578561
579- p . ExitCode . Should ( ) . Be ( 0 , $ "{ fileName } should return an exit code of 0.") ;
562+ result . ExitCode . Should ( ) . Be ( 0 , $ "{ fileName } should return an exit code of 0.") ;
580563 }
581564
582- public async Task RunAdoToGithubCliMigration ( string generateScriptCommand , IDictionary < string , string > tokens ) =>
565+ public async Task RunAdoToGithubCliMigration ( string generateScriptCommand , IReadOnlyDictionary < string , string > tokens ) =>
583566 await RunCliMigration ( $ "ado2gh { generateScriptCommand } ", "gh" , tokens ) ;
584567
585- public async Task RunGeiCliMigration ( string generateScriptCommand , IDictionary < string , string > tokens ) =>
568+ public async Task RunGeiCliMigration ( string generateScriptCommand , IReadOnlyDictionary < string , string > tokens ) =>
586569 await RunCliMigration ( $ "gei { generateScriptCommand } ", "gh" , tokens ) ;
587570
588- public async Task RunBbsCliMigration ( string generateScriptCommand , IDictionary < string , string > tokens ) =>
571+ public async Task RunBbsCliMigration ( string generateScriptCommand , IReadOnlyDictionary < string , string > tokens ) =>
589572 await RunCliMigration ( $ "bbs2gh { generateScriptCommand } ", "gh" , tokens ) ;
590573
591574 public static string GetOsDistPath ( )
0 commit comments