|
| 1 | +module Node.OS |
| 2 | + ( OS, NetworkInterface, CPU |
| 3 | + , eol |
| 4 | + , Arch(..), arch |
| 5 | + , cpus |
| 6 | + , Endianness(..), endianness |
| 7 | + , freemem |
| 8 | + , loadavg |
| 9 | + , homedir |
| 10 | + , hostname |
| 11 | + , Platform(..), platform |
| 12 | + , release |
| 13 | + , tmpdir |
| 14 | + , totalmem |
| 15 | + , ostype |
| 16 | + , uptime |
| 17 | + , networkInterfaces |
| 18 | + ) where |
| 19 | + |
| 20 | +import Prelude |
| 21 | + |
| 22 | +import Data.Array ((!!)) |
| 23 | +import Data.Maybe (fromMaybe) |
| 24 | +import Data.StrMap (StrMap) |
| 25 | +import Data.Time (Milliseconds, Seconds) |
| 26 | + |
| 27 | +import Control.Bind ((=<<)) |
| 28 | +import Control.Monad.Eff (Eff) |
| 29 | + |
| 30 | +type NetworkInterface = { address :: String |
| 31 | + , netmask :: String |
| 32 | + , family :: String |
| 33 | + , mac :: String |
| 34 | + , internal :: Boolean |
| 35 | + } |
| 36 | + |
| 37 | +type CPU = { model :: String |
| 38 | + , speed :: Int |
| 39 | + , times :: { user :: Milliseconds |
| 40 | + , nice :: Milliseconds |
| 41 | + , sys :: Milliseconds |
| 42 | + , idle :: Milliseconds |
| 43 | + , irq :: Milliseconds } } |
| 44 | + |
| 45 | +foreign import data OS :: ! |
| 46 | + |
| 47 | +foreign import eol :: Char |
| 48 | + |
| 49 | +foreign import archImpl :: forall eff. Eff ( os :: OS | eff ) String |
| 50 | + |
| 51 | +foreign import cpus :: forall eff. Eff ( os :: OS | eff ) (Array CPU) |
| 52 | + |
| 53 | +foreign import endiannessImpl :: forall eff. Eff ( os :: OS | eff ) String |
| 54 | + |
| 55 | +foreign import freemem :: forall eff. Eff ( os :: OS | eff ) Number |
| 56 | + |
| 57 | +foreign import homedir :: forall eff. Eff ( os :: OS | eff ) String |
| 58 | + |
| 59 | +foreign import hostname :: forall eff. Eff ( os :: OS | eff ) String |
| 60 | + |
| 61 | +foreign import loadavgImpl :: forall eff. Eff ( os :: OS | eff ) (Array Number) |
| 62 | + |
| 63 | +foreign import platformImpl :: forall eff. Eff ( os :: OS | eff ) String |
| 64 | + |
| 65 | +foreign import release :: forall eff. Eff ( os :: OS | eff ) String |
| 66 | + |
| 67 | +foreign import tmpdir :: forall eff. Eff ( os :: OS | eff ) String |
| 68 | + |
| 69 | +foreign import totalmem :: forall eff. Eff ( os :: OS | eff ) Number |
| 70 | + |
| 71 | +foreign import ostype :: forall eff. Eff ( os :: OS | eff ) String |
| 72 | + |
| 73 | +foreign import uptime :: forall eff. Eff ( os :: OS | eff ) Seconds |
| 74 | + |
| 75 | +foreign import networkInterfaces :: forall eff. Eff ( os :: OS | eff ) (StrMap (Array NetworkInterface)) |
| 76 | + |
| 77 | +loadavg :: forall eff. Eff ( os :: OS | eff ) { one :: Number, five :: Number, fifteen :: Number } |
| 78 | +loadavg = pure <<< fromMaybe {one: 0.0, five: 0.0, fifteen: 0.0} <<< extract =<< loadavgImpl |
| 79 | + where |
| 80 | + extract xs = do |
| 81 | + one <- xs !! 0 |
| 82 | + five <- xs !! 1 |
| 83 | + fifteen <- xs !! 2 |
| 84 | + pure {one, five, fifteen} |
| 85 | + |
| 86 | +data Arch = X64 | ARM | IA32 | UnknownArch |
| 87 | + |
| 88 | +derive instance eqArch :: Eq Arch |
| 89 | + |
| 90 | +instance showArch :: Show Arch where |
| 91 | + show X64 = "X64" |
| 92 | + show ARM = "ARM" |
| 93 | + show IA32 = "IA32" |
| 94 | + show UnknownArch = "UnknownArch" |
| 95 | + |
| 96 | +arch :: forall eff. Eff ( os :: OS | eff ) Arch |
| 97 | +arch = do |
| 98 | + a <- archImpl |
| 99 | + pure case a of |
| 100 | + "x64" -> X64 |
| 101 | + "arm" -> ARM |
| 102 | + "ia32" -> IA32 |
| 103 | + _ -> UnknownArch |
| 104 | + |
| 105 | +data Endianness = LittleEndian | BigEndian | UnknownEndian |
| 106 | + |
| 107 | +derive instance eqEndianness :: Eq Endianness |
| 108 | + |
| 109 | +instance showEndianness :: Show Endianness where |
| 110 | + show LittleEndian = "LittleEndian" |
| 111 | + show BigEndian = "BigEndian" |
| 112 | + show UnknownEndian = "UnknownEndian" |
| 113 | + |
| 114 | +endianness :: forall eff. Eff ( os :: OS | eff ) Endianness |
| 115 | +endianness = do |
| 116 | + e <- endiannessImpl |
| 117 | + pure case e of |
| 118 | + "BE" -> BigEndian |
| 119 | + "LE" -> LittleEndian |
| 120 | + _ -> UnknownEndian |
| 121 | + |
| 122 | +data Platform = Darwin | FreeBSD | Linux | SunOS | Win32 | UnknownPlatform |
| 123 | + |
| 124 | +derive instance eqPlatform :: Eq Platform |
| 125 | + |
| 126 | +instance showPlatform :: Show Platform where |
| 127 | + show Darwin = "Darwin" |
| 128 | + show FreeBSD = "FreeBSD" |
| 129 | + show Linux = "Linux" |
| 130 | + show SunOS = "SunOS" |
| 131 | + show Win32 = "Win32" |
| 132 | + show UnknownPlatform = "UnknownPlatform" |
| 133 | + |
| 134 | +platform :: forall eff. Eff ( os :: OS | eff ) Platform |
| 135 | +platform = do |
| 136 | + p <- platformImpl |
| 137 | + pure case p of |
| 138 | + "linux" -> Linux |
| 139 | + "darwin" -> Darwin |
| 140 | + "win32" -> Win32 |
| 141 | + "freebsd" -> FreeBSD |
| 142 | + "sunos" -> SunOS |
| 143 | + _ -> UnknownPlatform |
0 commit comments