0 | module Control.RIO.Sys
 1 |
 2 | import Control.RIO.App
 3 | import Control.RIO.File
 4 | import System
 5 |
 6 | %default total
 7 |
 8 | --------------------------------------------------------------------------------
 9 | --          Record
10 | --------------------------------------------------------------------------------
11 |
12 | public export
13 | record SysErr where
14 |   constructor MkSE
15 |   cmd : String
16 |   err : Int
17 |
18 | export
19 | printErr : SysErr -> String
20 | printErr (MkSE cmd err) =
21 |   "Command terminated with error code \{show err}: \{cmd}"
22 |
23 | ||| Record representing capabilities for running system calls
24 | public export
25 | record Sys where
26 |   constructor MkSys
27 |   sys_ : String -> IO (Either SysErr ())
28 |   run_ : String -> IO (Either SysErr String)
29 |
30 | --------------------------------------------------------------------------------
31 | --          Interface
32 | --------------------------------------------------------------------------------
33 |
34 | export
35 | sys : Sys => Has SysErr xs => String -> App xs ()
36 | sys cmd = injectIO (sys_ %search cmd)
37 |
38 | export
39 | run : Sys => Has SysErr xs => String -> App xs String
40 | run cmd = injectIO (run_ %search cmd)
41 |
42 | --------------------------------------------------------------------------------
43 | --          Implementation
44 | --------------------------------------------------------------------------------
45 |
46 | sysImpl : String -> IO (Either SysErr ())
47 | sysImpl cmd = do
48 |   0 <- system cmd | n => pure (Left $ MkSE cmd n)
49 |   pure (Right ())
50 |
51 | covering
52 | runImpl : String -> IO (Either SysErr String)
53 | runImpl cmd = do
54 |   (res,0) <- System.run cmd | (_,n) => pure (Left $ MkSE cmd n)
55 |   pure (Right res)
56 |
57 | export covering
58 | system : Sys
59 | system = MkSys sysImpl runImpl
60 |
61 | --------------------------------------------------------------------------------
62 | --          Utilities
63 | --------------------------------------------------------------------------------
64 |
65 | ||| Forcefully deletes a directory with all its content
66 | export
67 | rmDir : Sys => FS => Has SysErr xs => Path t -> App xs ()
68 | rmDir dir = when !(exists dir) $ sys "rm -rf \{dir}"
69 |