kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

AsyncCommand use main.stdout / main.stderr #61

Closed jakeheis closed 6 years ago

jakeheis commented 6 years ago

Right now, AsyncCommand will always change the process's stdout and stderr to a Pipe (https://github.com/kareman/SwiftShell/blob/master/Sources/SwiftShell/Command.swift#L291).

There should be a way to start an AsyncCommand which still directs output to main.stdout and main.stderr. Simply forwarding the output using something like:

command.stdout.onOutput { $0.write(to: &stdout) }

is not sufficient, as pipes are block buffered but main.stdout is line buffered, so the command's output will not be written to stdout in the expected way.

I think this could potentially be solved by changing AsyncCommand.init to something like:

init(unlaunched process: Process, stdout: WritableStream? = nil, stderr: WritableStream? = nil)
kareman commented 6 years ago

Yes that would be useful. But AsyncCommand.init is not public, so we need to change the public api. Do you think the use case is common enough to warrant a new function runAsyncAndPrint, which returns a superclass of AsyncCommand without stdout and stderror?

patchthecode commented 6 years ago

Usually one use case is enough to constantly bite you in the ass i always say 😄. I like this library. I vote yes.

kareman commented 6 years ago

I’m about to do some refactoring of Command.swift to move anything Linux-specific to its own file. I will implement runAsyncAndPrint after that.

Does anyone have a better idea for the name? It is a bit wordy, but I think it fits in nicely with run, runAsync and runAndPrint.

Ideally this functionality should be implemented with an extra parameter on runAsync, but the return type needs to be different.

kareman commented 6 years ago

@jakeheis I implemented runAsyncAndPrint in branch 61-AsyncCommand_use_main.stdout.

patchthecode commented 6 years ago

will take pull.

kareman commented 6 years ago

Just released version 4.1 with this feature.