private ICommandRunner GetCommandRunner(DockerContainerTransportSettings settings, bool handleRawOutput = false) { if (OuterConnection == null) { return(LocalCommandRunner.CreateInstance(handleRawOutput, settings)); } else { return(new RemoteCommandRunner(settings, OuterConnection, handleRawOutput)); } }
/// <inheritdoc/> public override void BeginExecuteAsyncCommand(string commandText, bool runInShell, IDebugUnixShellCommandCallback callback, out IDebugUnixShellAsyncCommand asyncCommand) { if (IsClosed) { throw new ObjectDisposedException(nameof(WSLConnection)); } string args = WSLCommandLine.GetExecCommandLineArgs(this.Name, commandText); ICommandRunner commandRunner = LocalCommandRunner.CreateInstance(handleRawOutput: runInShell == false, WSLCommandLine.ExePath, args); asyncCommand = new PipeAsyncCommand(commandRunner, callback); }
private static void RunDockerCommand(DockerCommandSettings settings, Action <string> callback) { LocalCommandRunner commandRunner = new LocalCommandRunner(settings); StringBuilder errorSB = new StringBuilder(); int? exitCode = null; try { ManualResetEvent resetEvent = new ManualResetEvent(false); commandRunner.ErrorOccured += ((sender, args) => { if (!string.IsNullOrWhiteSpace(args.ErrorMessage)) { errorSB.AppendLine(args.ErrorMessage); } resetEvent.Set(); }); commandRunner.Closed += ((sender, args) => { exitCode = args; resetEvent.Set(); }); commandRunner.OutputReceived += ((sender, args) => { if (!string.IsNullOrWhiteSpace(args)) { args = args.Trim(charsToTrim); callback(args); } }); commandRunner.Start(); bool cancellationRequested = false; VS.VSOperationWaiter.Wait(UIResources.QueryingForContainersMessage, false, (cancellationToken) => { while (!resetEvent.WaitOne(2000) && !cancellationToken.IsCancellationRequested) { } cancellationRequested = cancellationToken.IsCancellationRequested; }); if (!cancellationRequested) { if (exitCode.GetValueOrDefault(-1) != 0) { string exceptionMessage = UIResources.CommandExecutionErrorWithExitCodeFormat.FormatCurrentCultureWithArgs( "{0} {1}".FormatInvariantWithArgs(settings.Command, settings.CommandArgs), exitCode, errorSB.ToString()); throw new CommandFailedException(exceptionMessage); } if (errorSB.Length > 0) { throw new CommandFailedException(errorSB.ToString()); } } } catch (Win32Exception ex) { string errorMessage = UIResources.CommandExecutionErrorFormat.FormatCurrentCultureWithArgs(settings.CommandArgs, ex.Message); throw new CommandFailedException(errorMessage, ex); } }
public static IEnumerable <DockerContainerInstance> GetLocalDockerContainers(string hostname) { List <DockerContainerInstance> containers = new List <DockerContainerInstance>(); DockerCommandSettings settings = new DockerCommandSettings(hostname, false); settings.SetCommand(dockerPSCommand, dockerPSArgs); LocalCommandRunner commandRunner = new LocalCommandRunner(settings); StringBuilder errorSB = new StringBuilder(); int? exitCode = null; try { ManualResetEvent resetEvent = new ManualResetEvent(false); commandRunner.ErrorOccured += ((sender, args) => { if (!string.IsNullOrWhiteSpace(args.ErrorMessage)) { errorSB.AppendLine(args.ErrorMessage); } resetEvent.Set(); }); commandRunner.Closed += ((sender, args) => { exitCode = args; resetEvent.Set(); }); commandRunner.OutputReceived += ((sender, args) => { if (!string.IsNullOrWhiteSpace(args)) { if (args.Trim()[0] != '{') { // output isn't json, command Error errorSB.Append(args); } else { var containerInstance = DockerContainerInstance.Create(args); if (containerInstance != null) { containers.Add(containerInstance); } } } }); commandRunner.Start(); bool cancellationRequested = false; VS.VSOperationWaiter.Wait(UIResources.QueryingForContainersMessage, false, (cancellationToken) => { while (!resetEvent.WaitOne(2000) && !cancellationToken.IsCancellationRequested) { } cancellationRequested = cancellationToken.IsCancellationRequested; }); if (!cancellationRequested) { // might need to throw an exception here too?? if (exitCode.GetValueOrDefault(-1) != 0) { // if the exit code is not zero, then the output we received possibly is the error message string exceptionMessage = UIResources.CommandExecutionErrorWithExitCodeFormat.FormatCurrentCultureWithArgs( "{0} {1}".FormatInvariantWithArgs(settings.Command, settings.CommandArgs), exitCode, errorSB.ToString()); throw new CommandFailedException(exceptionMessage); } if (errorSB.Length > 0) { throw new CommandFailedException(errorSB.ToString()); } return(containers); } return(new List <DockerContainerInstance>()); } catch (Win32Exception ex) { // docker doesn't exist string errorMessage = UIResources.CommandExecutionErrorFormat.FormatCurrentCultureWithArgs(settings.CommandArgs, ex.Message); throw new CommandFailedException(errorMessage, ex); } }