{-# LANGUAGE FlexibleInstances #-}
module HSH.Helpers.Instances where

import HSH
import System.Exit
import System.Posix.Process

instance RunResult (IO (Either String ()) ) where
    run cmd = checkResults =<< run cmd 

{- | Evaluates result codes and raises an error for any bad ones it finds. -}
checkResults :: (String, ProcessStatus) -> IO (Either String ())
checkResults (cmd, ps) =
       case ps of
         Exited (ExitSuccess) -> return $ Right ()
         Exited (ExitFailure x) ->
             return $ Left $ cmd ++ ": exited with code " ++ show x
         Terminated sig ->
             return $ Left $ cmd ++ ": terminated by signal " ++ show sig
         Stopped sig ->
             return $ Left $ cmd ++ ": stopped by signal " ++ show sig