Merge pull request #2900 from unisonweb/topic/avoid-System.Process.showCommandForUser

This commit is contained in:
Arya Irani 2022-02-14 12:16:25 -07:00 committed by GitHub
commit 3e80018dbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,7 +24,6 @@ import Control.Monad.Except (MonadError, throwError)
import qualified Data.ByteString.Base16 as ByteString
import qualified Data.Char as Char
import qualified Data.Text as Text
import Shellmet (($?), ($^), ($|))
import System.Exit (ExitCode (ExitSuccess))
import System.FilePath ((</>))
import System.IO.Unsafe (unsafePerformIO)
@ -301,3 +300,38 @@ gitTextIn :: MonadIO m => GitRepo -> [Text] -> m Text
gitTextIn localPath args = do
when debugGit $ traceM (Text.unpack . Text.unwords $ ["$ git"] <> setupGitDir localPath <> args)
liftIO $ "git" $| setupGitDir localPath <> args
-- copying the Shellmet API for now; we can rename or change it up later
{- | This operator runs shell command with given options but doesn't print the
command itself.
>>> "echo" $^ ["Foo", "Bar"]
Foo Bar
-}
infix 5 $^
($^) :: MonadIO m => FilePath -> [Text] -> m ()
cmd $^ args = UnliftIO.callProcess cmd (map Text.unpack args)
{- | Run shell command with given options and return stripped stdout of the
executed command.
>>> "echo" $| ["Foo", "Bar"]
"Foo Bar"
-}
infix 5 $|
($|) :: MonadIO m => FilePath -> [Text] -> m Text
cmd $| args = post <$> UnliftIO.readProcess cmd (map Text.unpack args) stdin
where
stdin = ""
post = Text.strip . Text.pack
{- | Do some IO actions when process failed with 'IOError'.
>>> "exit" ["0"] $? putStrLn "Command failed"
exit 0
>>> "exit" ["1"] $? putStrLn "Command failed"
exit 1
Command failed
-}
infixl 4 $?
($?) :: IO a -> IO a -> IO a
action $? handler = action `UnliftIO.catch` \(_ :: IOError) -> handler
{-# INLINE ($?) #-}