module HSH.Helpers.Utils where
import HSH
import Text.StringTemplate.Helpers
import Control.Monad.Error
import System.IO.Error
failIf ioP m = do
p <- ioP
if p then fail m else return ()
inPath :: FilePath -> ErrorT String IO ()
inPath p = do
ErrorT $ do
res <- tryS $ runIO $ render1 [("p",p)] $ "which $p$"
case res of
Left _ -> return . Left $
render1 [("p",p)] "$p$ is not in \\$PATH, maybe you need to modify your shell environment"
Right _ -> return . Right $ ()
tryS :: IO a -> IO (Either String a)
tryS ma = return . mapeither show id =<< try ma
where
mapeither :: (a -> a') -> (b -> b') -> Either a b -> Either a' b'
mapeither f g (Left x) = Left $ f x
mapeither f g (Right x) = Right $ g x