shh-0.2.0.1: Simple shell scripting from Haskell

Safe HaskellNone
LanguageHaskell2010

Shh

Description

Shh provides a shell-like environment for Haskell.

Synopsis

Documentation

initInteractive :: IO () Source #

This function needs to be called in order to use the library succesfully from GHCi.

Constructing a Proc

You will rarely have to use these as most of the time these are created for you by using the loadEnv template Haskell function.

exe :: (Unit a, ExecArgs a) => String -> a Source #

Execute the given command. Further arguments can be passed in.

exe "ls" "-l"

See also loadExe and loadEnv.

mkProc :: String -> [String] -> Proc () Source #

Create a Proc from a command and a list of arguments.

runProc :: Proc a -> IO a Source #

Run's a Proc in IO. This is usually not required, as most commands in Shh are polymorphic in their return type, and work just fine in IO directly.

data Proc a Source #

Type representing a series or pipeline (or both) of shell commands.

Instances
Monad Proc Source # 
Instance details

Defined in Shh.Internal

Methods

(>>=) :: Proc a -> (a -> Proc b) -> Proc b #

(>>) :: Proc a -> Proc b -> Proc b #

return :: a -> Proc a #

fail :: String -> Proc a #

Functor Proc Source # 
Instance details

Defined in Shh.Internal

Methods

fmap :: (a -> b) -> Proc a -> Proc b #

(<$) :: a -> Proc b -> Proc a #

Applicative Proc Source # 
Instance details

Defined in Shh.Internal

Methods

pure :: a -> Proc a #

(<*>) :: Proc (a -> b) -> Proc a -> Proc b #

liftA2 :: (a -> b -> c) -> Proc a -> Proc b -> Proc c #

(*>) :: Proc a -> Proc b -> Proc b #

(<*) :: Proc a -> Proc b -> Proc a #

MonadIO Proc Source # 
Instance details

Defined in Shh.Internal

Methods

liftIO :: IO a -> Proc a #

ProcFailure Proc Source # 
Instance details

Defined in Shh.Internal

PipeResult Proc Source # 
Instance details

Defined in Shh.Internal

Methods

(|>) :: Proc a -> Proc a -> Proc a Source #

(|!>) :: Proc a -> Proc a -> Proc a Source #

(&>) :: Proc a -> Stream -> Proc a Source #

(&!>) :: Proc a -> Stream -> Proc a Source #

Semigroup (Proc a) Source #

The Semigroup instance for Proc pipes the stdout of one process into the stdin of the next. However, consider using |> instead which behaves when used in an IO context. If you use <> in an IO monad you will be using the IO instance of semigroup which is a sequential execution. |> prevents that error.

Instance details

Defined in Shh.Internal

Methods

(<>) :: Proc a -> Proc a -> Proc a #

sconcat :: NonEmpty (Proc a) -> Proc a #

stimes :: Integral b => b -> Proc a -> Proc a #

a ~ () => Monoid (Proc a) Source # 
Instance details

Defined in Shh.Internal

Methods

mempty :: Proc a #

mappend :: Proc a -> Proc a -> Proc a #

mconcat :: [Proc a] -> Proc a #

ExecArgs (Proc ()) Source # 
Instance details

Defined in Shh.Internal

Methods

toArgs :: [String] -> Proc () Source #

Piping and Redirection

class PipeResult f where Source #

This class is used to allow most of the operators in Shh to be polymorphic in their return value. This makes using them in an IO context easier (we can avoid having to prepend everything with a runProc).

Methods

(|>) :: Proc a -> Proc a -> f a Source #

Use this to send the output of on process into the input of another. This is just like a shells `|` operator.

The result is polymorphic in it's output, and can result in either another `Proc a` or an `IO a` depending on the context in which it is used.

>>> echo "Hello" |> wc
      1       1       6

(|!>) :: Proc a -> Proc a -> f a Source #

Similar to |!> except that it connects stderr to stdin of the next process in the chain.

NB: The next command to be |> on will recapture the stdout of both preceding processes, because they are both going to the same handle!

This is probably not what you want, see the &> and &!> operators for redirection.

(&>) :: Proc a -> Stream -> f a Source #

Redirect stdout of this process to another location

ls &> Append "/dev/null"

(&!>) :: Proc a -> Stream -> f a Source #

Redirect stderr of this process to another location

ls &!> StdOut
Instances
PipeResult IO Source # 
Instance details

Defined in Shh.Internal

Methods

(|>) :: Proc a -> Proc a -> IO a Source #

(|!>) :: Proc a -> Proc a -> IO a Source #

(&>) :: Proc a -> Stream -> IO a Source #

(&!>) :: Proc a -> Stream -> IO a Source #

PipeResult Proc Source # 
Instance details

Defined in Shh.Internal

Methods

(|>) :: Proc a -> Proc a -> Proc a Source #

(|!>) :: Proc a -> Proc a -> Proc a Source #

(&>) :: Proc a -> Stream -> Proc a Source #

(&!>) :: Proc a -> Stream -> Proc a Source #

(<|) :: PipeResult f => Proc a -> Proc a -> f a Source #

Flipped version of |>

data Stream Source #

Type used to represent destinations for redirects. Truncate file is like > file in a shell, and Append file is like >> file.

devNull :: Stream Source #

Shortcut for Truncate "/dev/null"

Reading stdout

Lazy/Streaming reads

These reads are lazy. The process is run long enough to produce the amount of output that is actually used. It is therefor suitable for use with infinite output streams. The process is terminated as soon the function finishes. Note that the result is forced to normal form to prevent any accidental reading after the process has terminated.

withRead :: (NFData b, MonadIO io) => Proc a -> (String -> IO b) -> io b Source #

Run a process and capture it's output lazily. Once the continuation is completed, the handles are closed, and the process is terminated.

withReadSplit0 :: (NFData b, MonadIO io) => Proc a -> ([String] -> IO b) -> io b Source #

Like withRead except it splits the string with split0 first.

withReadLines :: (NFData b, MonadIO io) => Proc a -> ([String] -> IO b) -> io b Source #

Like withRead except it splits the string with lines first.

NB: Please consider using withReadSplit0 where you can.

withReadWords :: (NFData b, MonadIO io) => Proc a -> ([String] -> IO b) -> io b Source #

Like withRead except it splits the string with words first.

Strict reads

readProc :: MonadIO io => Proc a -> io String Source #

Read the stdout of a Proc in any MonadIO (including other Procs). This is strict, so the whole output is read into a String. See withRead for a lazy version that can be used for streaming.

readTrim :: MonadIO io => Proc a -> io String Source #

Like readProc, but trim leading and tailing whitespace.

readSplit0 :: Proc () -> IO [String] Source #

A convinience function for reading in a "\NUL" seperated list of strings. This is commonly used when dealing with paths.

readSplit0 $ find "-print0"

readLines :: Proc () -> IO [String] Source #

A convinience function for reading the output lines of a Proc.

Note: Please consider using readSplit0 instead if you can.

readWords :: Proc () -> IO [String] Source #

Read output into a list of words

readAuto :: Read a => Proc () -> IO a Source #

Like readProc, but attempts to read the result.

String manipulation

Utility functions for dealing with common string issues in shell scripting.

trim :: String -> String Source #

Trim leading and tailing whitespace.

split0 :: String -> [String] Source #

Function that splits '\0' seperated list of strings. Useful in conjuction with find . "-print0".

Writing to stdin

writeProc :: MonadIO io => Proc a -> String -> io a Source #

Provide the stdin of a Proc from a String

(<<<) :: MonadIO io => Proc a -> String -> io a Source #

Infix version of writeProc

(>>>) :: MonadIO io => String -> Proc a -> io a Source #

Flipped, infix version of writeProc

readWriteProc :: MonadIO io => Proc a -> String -> io String Source #

Read and write to a Proc...

apply :: MonadIO io => Proc a -> String -> io String Source #

Some as readWriteProc. Apply a Proc to a String.

>>> apply shasum "Hello"
"f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0  -\n"

Exceptions

If any exception is allowed to propagate out of a pipeline, all the processes comprising the pipeline will be terminated. This is contrary to how a shell normally works (even with -o pipefail!).

data Failure Source #

When a process exits with a non-zero exit code we throw this Failure exception.

The only exception to this is when a process is terminated by SIGPIPE in a pipeline, in which case we ignore it.

Constructors

Failure 
Instances
Eq Failure Source # 
Instance details

Defined in Shh.Internal

Methods

(==) :: Failure -> Failure -> Bool #

(/=) :: Failure -> Failure -> Bool #

Ord Failure Source # 
Instance details

Defined in Shh.Internal

Show Failure Source # 
Instance details

Defined in Shh.Internal

Exception Failure Source # 
Instance details

Defined in Shh.Internal

ignoreFailure :: (Functor m, ProcFailure m) => Proc a -> m () Source #

Run a Proc action, ignoring any Failure exceptions. This can be used to prevent a process from interrupting a whole pipeline.

>>> false `|>` (sleep 2 >> echo 1)
*** Exception: Command `false` failed [exit 1]
>>> (ignoreFailure  false) `|>` (sleep 2 >> echo 1)
1

catchFailure :: ProcFailure m => Proc a -> m (Either Failure a) Source #

Run a Proc action, catching an Failure exceptions and returning them.

catchCode :: (Functor m, ProcFailure m) => Proc a -> m Int Source #

Run an Proc action returning the return code if an exception was thrown, and 0 if it wasn't.

Constructing Arguments

class ExecArg a where Source #

A class for things that can be converted to arguments on the command line. The default implementation is to use show.

Minimal complete definition

Nothing

Methods

asArg :: a -> [String] Source #

asArg :: Show a => a -> [String] Source #

asArgFromList :: [a] -> [String] Source #

asArgFromList :: Show a => [a] -> [String] Source #

Instances
ExecArg Char Source # 
Instance details

Defined in Shh.Internal

ExecArg Int Source # 
Instance details

Defined in Shh.Internal

ExecArg Integer Source # 
Instance details

Defined in Shh.Internal

ExecArg Word Source # 
Instance details

Defined in Shh.Internal

ExecArg a => ExecArg [a] Source # 
Instance details

Defined in Shh.Internal

Methods

asArg :: [a] -> [String] Source #

asArgFromList :: [[a]] -> [String] Source #

Template Haskell helpers

data ExecReference Source #

Specify how executables should be referenced.

Constructors

Absolute

Find executables on PATH, but store their absolute path

SearchPath

Always search on PATH

load :: ExecReference -> [String] -> Q [Dec] Source #

Load the given executables into the program, checking their executability and creating a function missingExecutables to do a runtime check for their availability.

loadEnv :: ExecReference -> Q [Dec] Source #

Scans your '$PATH' environment variable and creates a function for each executable found. Binaries that would not create valid Haskell identifiers are ignored. It also creates the IO action missingExecutables which will do a runtime check to ensure all the executables that were found at compile time still exist.

loadAnnotated :: ExecReference -> (String -> String) -> [String] -> Q [Dec] Source #

Same as load, but allows you to modify the function names.

loadAnnotatedEnv :: ExecReference -> (String -> String) -> Q [Dec] Source #

Like loadEnv, but allows you to modify the function name that would be generated.

loadExe :: ExecReference -> String -> Q [Dec] Source #

Create a function for the executable named

loadExeAs :: ExecReference -> String -> String -> Q [Dec] Source #

$(loadExeAs fnName executable) defines a function called fnName which executes the path in executable.