Copyright | (c) Ivan Lazar Miljenovic |
---|---|
License | MIT |
Maintainer | Ivan.Miljenovic@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Run system commands in a streaming fashion.
WARNING: If using this module, you will need to have
ghc-options -threaded
in your .cabal
file otherwise it will
likely hang!
These functions are typically written to be used in a continuation-passing style to allow for proper finalisation. If you have many of these nested, it may be easier to use the Streaming.Process.Lifted module.
These functions will all throw ProcessExitedUnsuccessfully
if the
process/command itself fails.
- withStreamingProcess :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => CreateProcess -> ByteString m v -> (StdOutErr n () -> m r) -> m r
- withStreamingCommand :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => String -> ByteString m v -> (StdOutErr n () -> m r) -> m r
- streamInput :: (MonadIO m, MonadMask m) => CreateProcess -> ByteString m r -> m r
- streamInputCommand :: (MonadIO m, MonadMask m) => String -> ByteString m r -> m r
- withStreamingOutput :: (MonadIO n, MonadIO m, MonadMask m) => CreateProcess -> (ByteString n () -> m r) -> m r
- withStreamingOutputCommand :: (MonadIO n, MonadIO m, MonadMask m) => String -> (ByteString n () -> m r) -> m r
- data StreamProcess stdin stdout stderr = StreamProcess {
- toStdin :: !stdin
- fromStdout :: !stdout
- fromStderr :: !stderr
- switchOutputs :: StreamProcess stdin stdout stderr -> StreamProcess stdin stderr stdout
- newtype WithStream n m = WithStream {
- withStream :: forall r. (ByteString n () -> m r) -> m r
- type WithStream' m = WithStream m m
- newtype SupplyStream m = SupplyStream {
- supplyStream :: forall r. ByteString m r -> m r
- withStreamProcess :: (InputSource stdin, OutputSink stdout, OutputSink stderr, MonadIO m, MonadMask m) => CreateProcess -> (StreamProcess stdin stdout stderr -> m r) -> m r
- withStreamCommand :: (InputSource stdin, OutputSink stdout, OutputSink stderr, MonadIO m, MonadMask m) => String -> (StreamProcess stdin stdout stderr -> m r) -> m r
- withProcessHandles :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => ByteString m v -> StreamProcess (SupplyStream m) (WithStream' m) (WithStream' m) -> (StdOutErr n () -> m r) -> m r
- processInput :: (MonadIO m, MonadMask m) => StreamProcess (SupplyStream m) ClosedStream ClosedStream -> ByteString m r -> m r
- withProcessOutput :: (MonadIO n, MonadIO m, MonadMask m) => StreamProcess ClosedStream (WithStream n m) ClosedStream -> (ByteString n () -> m r) -> m r
- type StdOutErr m r = ByteString (ByteString m) r
- withStreamOutputs :: (MonadMask m, MonadIO m, MonadBaseControl IO m, MonadBase IO n) => StreamProcess stdin (WithStream' m) (WithStream' m) -> (StdOutErr n () -> m r) -> m r
- module Data.Streaming.Process
- concurrently :: MonadBaseControl IO m => m a -> m b -> m (a, b)
High level functions
withStreamingProcess :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => CreateProcess -> ByteString m v -> (StdOutErr n () -> m r) -> m r Source #
Feeds the provided data into the specified process, then concurrently streams stdout and stderr into the provided continuation.
Note that the monad used in the StdOutErr
argument to the
continuation can be different from the final result, as it's up
to the caller to make sure the result is reached.
withStreamingCommand :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => String -> ByteString m v -> (StdOutErr n () -> m r) -> m r Source #
As with withStreamingProcess
, but run the specified command in
a shell.
streamInput :: (MonadIO m, MonadMask m) => CreateProcess -> ByteString m r -> m r Source #
Feed input into a process with no expected output.
streamInputCommand :: (MonadIO m, MonadMask m) => String -> ByteString m r -> m r Source #
As with streamInput
but run the specified command in a shell.
withStreamingOutput :: (MonadIO n, MonadIO m, MonadMask m) => CreateProcess -> (ByteString n () -> m r) -> m r Source #
Obtain the output of a process with no input (ignoring error output).
withStreamingOutputCommand :: (MonadIO n, MonadIO m, MonadMask m) => String -> (ByteString n () -> m r) -> m r Source #
As with withStreamingOutput
but run the specified command in a
shell.
Lower level
data StreamProcess stdin stdout stderr Source #
Represents the input and outputs for a streaming process.
StreamProcess | |
|
switchOutputs :: StreamProcess stdin stdout stderr -> StreamProcess stdin stderr stdout Source #
Switch the two outputs. Useful for example if using
withStreamProcess
and withProcessHandles
but wanting to deal
with any potential output from stderr before stdout.
newtype WithStream n m Source #
A wrapper for something taking a continuation with a stream of bytes as input.
WithStream | |
|
(MonadIO m, MonadMask m, MonadIO n) => OutputSink (WithStream n m) Source # | |
type WithStream' m = WithStream m m Source #
An alias for the common case of n ~ m
.
newtype SupplyStream m Source #
A wrapper for being able to provide a stream of bytes.
SupplyStream | |
|
(MonadMask m, MonadIO m) => InputSource (SupplyStream m) Source # | |
withStreamProcess :: (InputSource stdin, OutputSink stdout, OutputSink stderr, MonadIO m, MonadMask m) => CreateProcess -> (StreamProcess stdin stdout stderr -> m r) -> m r Source #
A variant of withCheckedProcess
that will on an exception kill
the child process and attempt to perform cleanup (though you
should also attempt to do so in your own code).
Will throw ProcessExitedUnsuccessfully
on a non-successful exit code.
Compared to withCheckedProcessCleanup
from conduit-extra
,
this has the three parameters grouped into StreamProcess
to
make it more of a continuation.
withStreamCommand :: (InputSource stdin, OutputSink stdout, OutputSink stderr, MonadIO m, MonadMask m) => String -> (StreamProcess stdin stdout stderr -> m r) -> m r Source #
A variant of withStreamProcess
that runs the provided
command in a shell.
withProcessHandles :: (MonadBaseControl IO m, MonadIO m, MonadMask m, MonadBase IO n) => ByteString m v -> StreamProcess (SupplyStream m) (WithStream' m) (WithStream' m) -> (StdOutErr n () -> m r) -> m r Source #
Feeds the provided data into the input handle, then concurrently streams stdout and stderr into the provided continuation.
Note that the monad used in the StdOutErr
argument to the
continuation can be different from the final result, as it's up
to the caller to make sure the result is reached.
processInput :: (MonadIO m, MonadMask m) => StreamProcess (SupplyStream m) ClosedStream ClosedStream -> ByteString m r -> m r Source #
Stream input into a process, ignoring any output.
withProcessOutput :: (MonadIO n, MonadIO m, MonadMask m) => StreamProcess ClosedStream (WithStream n m) ClosedStream -> (ByteString n () -> m r) -> m r Source #
Read the output from a process, ignoring stdin and stderr.
Interleaved stdout and stderr
type StdOutErr m r = ByteString (ByteString m) r Source #
withStreamOutputs :: (MonadMask m, MonadIO m, MonadBaseControl IO m, MonadBase IO n) => StreamProcess stdin (WithStream' m) (WithStream' m) -> (StdOutErr n () -> m r) -> m r Source #
Get both stdout and stderr concurrently.
Re-exports
All of Data.Streaming.Process is available for you to use.
The concurrently
function will probably be useful if manually
handling process inputs and outputs.
module Data.Streaming.Process
concurrently :: MonadBaseControl IO m => m a -> m b -> m (a, b) #
Generalized version of concurrently
.