Commits

nakamura committed 04ed28e

create higher order function for GetDaemonStatus

Comments (0)

Files changed (1)

 commandInfoV dn = commandInfoWith dn $ \dst lst -> pprintDaemonState dst ++ pprintDaemonState lst
 
 commandInfoWith :: String -> (DaemonState -> LoggerState -> String) -> ClientCommand
-commandInfoWith dn printer = do
-    h <- connectMaster
-    hPrintM h $ GetDaemonStatus dn
-    rsp <- hGetResponse h
-    case rsp of
-        Nothing                     -> return $ Left "Unknown Response"
-        Just (NG e)                 -> return $ Left ("NG: " ++ e)
-        Just (DaemonStatus dst lst) -> return $ Right $ printer dst lst
-        Just r                      -> return $ Left ("Invalid Response: " ++ show r)
+commandInfoWith dn printer = withDaemonStatus dn $ return . Right . uncurry printer
 
 commandTail :: [String] -> ClientCommand
 commandTail args = commandWithCommand "tail" args
 assertResponseOK (Just OK)     = return $ Right "OK"
 assertResponseOK (Just r)      = return $ Left ("Invalid Response: " ++ show r)
 
+withDaemonStatus :: String -> ((DaemonState, LoggerState) -> ClientCommand) -> ClientCommand
+withDaemonStatus dn c = do
+    h <- connectMaster
+    hPrintM h $ GetDaemonStatus dn
+    rsp <- hGetResponse h
+    case rsp of
+        Nothing                     -> return $ Left "Unknown Response"
+        Just (NG e)                 -> return $ Left ("NG: " ++ e)
+        Just (DaemonStatus dst lst) -> c (dst, lst)
+        Just r                      -> return $ Left ("Invalid Response: " ++ show r)
+
 showDaemonStatus :: UTCTime -> DaemonStatus -> String
 showDaemonStatus now (Running pid t)  = "running (pid: " ++ show pid ++ ") " ++ showSeconds (truncate $ diffUTCTime now t)
 showDaemonStatus _   (Exited code)    = "exited (eixt with: " ++ show code ++ ")"