Skip to content
Snippets Groups Projects
Commit b25e8ba4 authored by Mr. Snow Ball / projects's avatar Mr. Snow Ball / projects :arrows_counterclockwise:
Browse files

Refactor: move error handling into qubesAdminCall

parent bec3825d
No related branches found
No related tags found
No related merge requests found
...@@ -79,8 +79,8 @@ qubesAdminConnect serviceName args = do ...@@ -79,8 +79,8 @@ qubesAdminConnect serviceName args = do
let processConfig = setStdin nullStream $ setStdout createPipe $ shell $ BLC.unpack cmd let processConfig = setStdin nullStream $ setStdout createPipe $ shell $ BLC.unpack cmd
startProcess processConfig startProcess processConfig
qubesAdminCall :: BL.ByteString -> [BL.ByteString] -> IO QubesAdminReturn qubesTryAdminCall :: BL.ByteString -> [BL.ByteString] -> IO QubesAdminReturn
qubesAdminCall serviceName args = do qubesTryAdminCall serviceName args = do
process <- qubesAdminConnect serviceName args process <- qubesAdminConnect serviceName args
let stdout = getStdout process let stdout = getStdout process
hSetBinaryMode stdout True hSetBinaryMode stdout True
...@@ -90,6 +90,12 @@ qubesAdminCall serviceName args = do ...@@ -90,6 +90,12 @@ qubesAdminCall serviceName args = do
Exception {} -> return reply Exception {} -> return reply
Event {} -> fail "service has returned events instead of a reply" Event {} -> fail "service has returned events instead of a reply"
qubesAdminCall :: BL.ByteString -> [BL.ByteString] -> IO BL.ByteString
qubesAdminCall serviceName args = qubesTryAdminCall serviceName args >>= get where
get Ok {okContent} = return okContent
get x@Exception {} = fail $ "service has returned an exception: " <> show x
get Event {} = fail "service has returned events instead of a reply"
qubesAdminCallP :: BL.ByteString -> [BL.ByteString] -> Producer QubesAdminReturn (P.SafeT IO) () qubesAdminCallP :: BL.ByteString -> [BL.ByteString] -> Producer QubesAdminReturn (P.SafeT IO) ()
qubesAdminCallP serviceName args = do qubesAdminCallP serviceName args = do
process <- liftIO $ qubesAdminConnect serviceName args process <- liftIO $ qubesAdminConnect serviceName args
...@@ -215,10 +221,8 @@ instance Read QubesVMState where ...@@ -215,10 +221,8 @@ instance Read QubesVMState where
_ -> UnknownState _ -> UnknownState
qubesListVMs :: IO (Map.Map BL.ByteString QubesVMInfo) qubesListVMs :: IO (Map.Map BL.ByteString QubesVMInfo)
qubesListVMs = qubesAdminCall "admin.vm.List" [] >>= fromOk >>= parse qubesListVMs = qubesAdminCall "admin.vm.List" [] >>= parse
where where
fromOk (Ok x) = return x
fromOk x = fail $ "unexpected reply: " <> show x
parse reply = BLC.split '\n' reply parse reply = BLC.split '\n' reply
& filter (/="") & filter (/="")
& map parseLine & map parseLine
...@@ -237,10 +241,8 @@ qubesListVMs = qubesAdminCall "admin.vm.List" [] >>= fromOk >>= parse ...@@ -237,10 +241,8 @@ qubesListVMs = qubesAdminCall "admin.vm.List" [] >>= fromOk >>= parse
tryReadProp name = readMaybe . BLC.unpack =<< getProp name tryReadProp name = readMaybe . BLC.unpack =<< getProp name
qubesGetProperty :: BL.ByteString -> IO (Bool, BL.ByteString, BL.ByteString) qubesGetProperty :: BL.ByteString -> IO (Bool, BL.ByteString, BL.ByteString)
qubesGetProperty name = qubesAdminCall "admin.property.Get" [name] >>= fromOk >>= parse qubesGetProperty name = qubesAdminCall "admin.property.Get" [name] >>= parse
where where
fromOk (Ok x) = return x
fromOk x = fail $ "unexpected reply: " <> show x
parse reply = return (isDefault == "default=True", BL.drop 5 typeStr, value) parse reply = return (isDefault == "default=True", BL.drop 5 typeStr, value)
where where
splitOn ch = fmap BLC.tail . BLC.break (==ch) splitOn ch = fmap BLC.tail . BLC.break (==ch)
...@@ -251,10 +253,8 @@ qubesGetDefaultPool = third <$> qubesGetProperty "default_pool" ...@@ -251,10 +253,8 @@ qubesGetDefaultPool = third <$> qubesGetProperty "default_pool"
where third (_, _, x) = x where third (_, _, x) = x
qubesGetPoolInfo :: BL.ByteString -> IO [(BL.ByteString, BL.ByteString)] qubesGetPoolInfo :: BL.ByteString -> IO [(BL.ByteString, BL.ByteString)]
qubesGetPoolInfo name = qubesAdminCall "admin.pool.Info" [name] >>= fromOk >>= parse qubesGetPoolInfo name = qubesAdminCall "admin.pool.Info" [name] >>= parse
where where
fromOk (Ok x) = return x
fromOk x = fail $ "unexpected reply: " <> show x
parse reply = BLC.split '\n' reply parse reply = BLC.split '\n' reply
& filter (/="") & filter (/="")
& map parseLine & map parseLine
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment