improving spawn command execution
This commit is contained in:
@@ -3,13 +3,15 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
|
||||||
import System.Exit (ExitCode(ExitFailure))
|
import System.Exit ( ExitCode(ExitFailure) )
|
||||||
|
|
||||||
|
|
||||||
import System.Process
|
import System.Process
|
||||||
( readProcessWithExitCode
|
( readProcessWithExitCode
|
||||||
, readProcess
|
, proc
|
||||||
|
, createProcess
|
||||||
, terminateProcess
|
, terminateProcess
|
||||||
|
, readProcess
|
||||||
, spawnProcess
|
, spawnProcess
|
||||||
, ProcessHandle
|
, ProcessHandle
|
||||||
)
|
)
|
||||||
@@ -37,7 +39,7 @@ import Data.Aeson
|
|||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
||||||
-- TODO: add IO arguments; flag to optionally cleanup environment
|
-- TODO: add IO arguments;
|
||||||
-- TODO: create DSL from the standard input
|
-- TODO: create DSL from the standard input
|
||||||
--
|
--
|
||||||
-- TODO: add GDTA syntax to data types
|
-- TODO: add GDTA syntax to data types
|
||||||
@@ -271,6 +273,7 @@ execProcP2Prc_ p2prcCmd ops stdi =
|
|||||||
(show stdi)
|
(show stdi)
|
||||||
|
|
||||||
|
|
||||||
|
-- TODO: replace this for execProcP2Prc_
|
||||||
eitherExecProcess :: CLICmd -> [CLIOpt] -> StdInput -> IO (Either Error String)
|
eitherExecProcess :: CLICmd -> [CLIOpt] -> StdInput -> IO (Either Error String)
|
||||||
eitherExecProcess cmd opts input =
|
eitherExecProcess cmd opts input =
|
||||||
do
|
do
|
||||||
@@ -304,6 +307,26 @@ spawnProcP2Prc p2prcCmd =
|
|||||||
spawnProcess p2prcCmd . optsToCLI
|
spawnProcess p2prcCmd . optsToCLI
|
||||||
|
|
||||||
|
|
||||||
|
-- TODO: replace spawnProcess with this
|
||||||
|
eitherSpawnProcP2Prc :: CLICmd -> [CLIOpt] -> IO (Either Error ProcessHandle)
|
||||||
|
eitherSpawnProcP2Prc cmd opts =
|
||||||
|
do
|
||||||
|
|
||||||
|
let prc = proc cmd (optsToCLI opts)
|
||||||
|
|
||||||
|
creationResult <- createProcess prc
|
||||||
|
|
||||||
|
let (_, _, _, ph) = creationResult
|
||||||
|
|
||||||
|
case creationResult of
|
||||||
|
(_, _, Just _, _) -> do
|
||||||
|
terminateProcess ph
|
||||||
|
pure $ Left $ MkErrorSpawningProcess $ "Error executing: " ++ cmd
|
||||||
|
|
||||||
|
_ -> pure $ Right ph
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
eitherErrorDecode :: Either String a -> Either Error a
|
eitherErrorDecode :: Either String a -> Either Error a
|
||||||
eitherErrorDecode esa =
|
eitherErrorDecode esa =
|
||||||
case esa of
|
case esa of
|
||||||
@@ -313,6 +336,7 @@ eitherErrorDecode esa =
|
|||||||
|
|
||||||
data Error
|
data Error
|
||||||
= MkUnknownError String
|
= MkUnknownError String
|
||||||
|
| MkErrorSpawningProcess String
|
||||||
| MkSystemError Int String
|
| MkSystemError Int String
|
||||||
deriving Show
|
deriving Show
|
||||||
|
|
||||||
@@ -329,11 +353,11 @@ getP2PrcCmd = do
|
|||||||
-- assumes the program is ran inside the haskell module in p2prc's repo
|
-- assumes the program is ran inside the haskell module in p2prc's repo
|
||||||
-- assumes that last path segment is "haskell" and that p2prc binary's name is "p2p-rendering-computation"
|
-- assumes that last path segment is "haskell" and that p2prc binary's name is "p2p-rendering-computation"
|
||||||
|
|
||||||
-- TODO: change to safe type of function
|
-- TODO: change to "eitherExecProcess"
|
||||||
T.unpack . T.strip . T.pack <$> readProcess "pwd" [] "" >>=
|
T.unpack . T.strip . T.pack <$> readProcess "pwd" [] "" >>=
|
||||||
\pwdOut ->
|
\pwdOut ->
|
||||||
|
|
||||||
-- TODO: change to safe type of function
|
-- TODO: change to "eitherExecProcess"
|
||||||
readProcess "sed" ["s/haskell/p2p-rendering-computation/"] pwdOut
|
readProcess "sed" ["s/haskell/p2p-rendering-computation/"] pwdOut
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user