{-# LANGUAGE TypeFamilies, CPP #-}
module Control.Shell
(
Shell, ExitReason (..)
, shell, shell_, exitString
, (|>), capture, captureStdErr, capture2, capture3, stream, lift
, try, orElse, exit
, Guard (..), guard, when, unless
, withEnv, withoutEnv, lookupEnv, getEnv, cmdline
, MonadIO (..), Env (..)
, run, sudo
, unsafeLiftIO
, absPath, shellEnv, getShellEnv, joinResult, runSh
, cpdir, pwd, ls, mkdir, rmdir, inDirectory, isDirectory
, withHomeDirectory, inHomeDirectory, withAppDirectory, inAppDirectory
, forEachFile, forEachFile_, forEachDirectory, forEachDirectory_
, isFile, rm, mv, cp, input, output
, withFile, withBinaryFile, openFile, openBinaryFile
, FileMode (..)
, withTempFile, withCustomTempFile
, withTempDirectory, withCustomTempDirectory
, inTempDirectory, inCustomTempDirectory
, Handle, IOMode (..), BufferMode (..)
, hFlush, hClose, hReady
, hGetBuffering, hSetBuffering
, getStdIn, getStdOut, getStdErr
, hPutStr, hPutStrLn, echo, echo_, ask, stdin
, hGetLine, hGetContents
, module Control.Shell.Color
, hGetBytes, hPutBytes, hGetByteLine, hGetByteContents
, module System.FilePath
, module Control.Monad
) where
import qualified System.Environment as Env
import System.IO.Unsafe
import Control.Shell.Base hiding (getEnv, takeEnvLock, releaseEnvLock, setShellEnv)
import qualified Control.Shell.Base as CSB
import Control.Shell.Handle
import Control.Shell.File
import Control.Shell.Directory
import Control.Shell.Temp
import Control.Shell.Control
import Control.Shell.Color
import Control.Monad hiding (guard, when, unless)
import System.FilePath
exitString :: ExitReason -> String
exitString Success = ""
exitString (Failure "") = "abnormal termination"
exitString (Failure s) = s
getShellEnv :: Shell Env
getShellEnv = CSB.getEnv
insert :: Eq k => k -> v -> [(k, v)] -> [(k, v)]
insert k' v' (kv@(k, _) : kvs)
| k == k' = (k', v') : kvs
| otherwise = kv : insert k' v' kvs
insert k v _ = [(k, v)]
delete :: Eq k => k -> [(k, v)] -> [(k, v)]
delete k' (kv@(k, _) : kvs)
| k == k' = kvs
| otherwise = kv : delete k' kvs
delete _ _ = []
cmdline :: [String]
cmdline = unsafePerformIO Env.getArgs
withEnv :: String -> String -> Shell a -> Shell a
withEnv k v m = do
e <- CSB.getEnv
inEnv (e {envEnvVars = insert k v (envEnvVars e)}) m
withoutEnv :: String -> Shell a -> Shell a
withoutEnv k m = do
e <- CSB.getEnv
inEnv (e {envEnvVars = delete k (envEnvVars e)}) m
lookupEnv :: String -> Shell (Maybe String)
lookupEnv k = lookup k . envEnvVars <$> CSB.getEnv
getEnv :: String -> Shell String
getEnv key = maybe "" id `fmap` lookupEnv key
sudo :: FilePath -> [String] -> Shell ()
sudo cmd as = run "sudo" (cmd:"--":as)
inTempDirectory :: Shell a -> Shell a
inTempDirectory = withTempDirectory . flip inDirectory
inCustomTempDirectory :: FilePath -> Shell a -> Shell a
inCustomTempDirectory dir m = withCustomTempDirectory dir $ flip inDirectory m
getStdIn, getStdOut, getStdErr :: Shell Handle
getStdIn = envStdIn <$> CSB.getEnv
getStdOut = envStdOut <$> CSB.getEnv
getStdErr = envStdErr <$> CSB.getEnv