-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.hs
52 lines (47 loc) · 1.34 KB
/
test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE OverloadedStrings #-}
import Data.Typeable
import System.IO (hPutStr, hClose)
import System.Process.Typed
import Data.ByteString.Lazy.Char8
import System.Exit (ExitCode)
import Data.ByteString.Lazy (ByteString)
data Command = Command {
template :: String,
nParameters :: Int,
stdout :: ByteString,
stderr :: ByteString,
code :: ExitCode
} deriving (Show)
-- A function with no input (date)
-- need to use () to indicate it doesnt receive parameters
-- need to use 'return' to extract stuff from the monad that comes out of readProcess, i think
-- this scarily seems like imperative code, but isn't. i think im getting it
-- got the type signature from ghci, :load this file, :t runDate
-- runDate :: Control.Monad.IO.Class.MonadIO m => () -> m Command
-- but it fails...
runDate () = do
(exitCode, out, err) <- readProcess "date"
let c = Command {
template="date",
nParameters=0,
code=exitCode,
stdout=out,
stderr=err
}
return c
runCat file = do
(exitCode, out, err) <- readProcess (proc "cat" [file])
let c = Command {
template="cat [parameter]",
nParameters=1,
code=exitCode,
stdout=out,
stderr=err
}
return c
main :: IO ()
main = do
out1 <- runDate ()
out2 <- runCat "test.txt"
print out1
print out2