Revision 88a10df5 htools/Ganeti/HTools/Utils.hs
b/htools/Ganeti/HTools/Utils.hs | ||
---|---|---|
36 | 36 |
, printTable |
37 | 37 |
, parseUnit |
38 | 38 |
, plural |
39 |
, exitIfBad |
|
40 |
, exitErr |
|
41 |
, exitWhen |
|
42 |
, exitUnless |
|
39 | 43 |
) where |
40 | 44 |
|
41 | 45 |
import Data.Char (toUpper) |
... | ... | |
43 | 47 |
|
44 | 48 |
import Debug.Trace |
45 | 49 |
|
50 |
import Ganeti.BasicTypes |
|
51 |
import System.IO |
|
52 |
import System.Exit |
|
53 |
|
|
46 | 54 |
-- * Debug functions |
47 | 55 |
|
48 | 56 |
-- | To be used only for debugging, breaks referential integrity. |
... | ... | |
198 | 206 |
scaling <- parseUnitValue unit |
199 | 207 |
return $ truncate (fromIntegral v * scaling) |
200 | 208 |
_ -> fail $ "Can't parse string '" ++ str ++ "'" |
209 |
|
|
210 |
-- | Unwraps a 'Result', exiting the program if it is a 'Bad' value, |
|
211 |
-- otherwise returning the actual contained value. |
|
212 |
exitIfBad :: String -> Result a -> IO a |
|
213 |
exitIfBad msg (Bad s) = do |
|
214 |
hPutStrLn stderr $ "Error: " ++ msg ++ ": " ++ s |
|
215 |
exitWith (ExitFailure 1) |
|
216 |
exitIfBad _ (Ok v) = return v |
|
217 |
|
|
218 |
-- | Exits immediately with an error message. |
|
219 |
exitErr :: String -> IO a |
|
220 |
exitErr errmsg = do |
|
221 |
hPutStrLn stderr $ "Error: " ++ errmsg ++ "." |
|
222 |
exitWith (ExitFailure 1) |
|
223 |
|
|
224 |
-- | Exits with an error message if the given boolean condition if true. |
|
225 |
exitWhen :: Bool -> String -> IO () |
|
226 |
exitWhen True msg = exitErr msg |
|
227 |
exitWhen False _ = return () |
|
228 |
|
|
229 |
-- | Exits with an error message /unless/ the given boolean condition |
|
230 |
-- if true, the opposite of 'exitWhen'. |
|
231 |
exitUnless :: Bool -> String -> IO () |
|
232 |
exitUnless cond = exitWhen (not cond) |
Also available in: Unified diff