Commits

nakamura committed 94bd501

clean up duplicated processes

Comments (0)

Files changed (1)

 
 commandPing :: ClientCommand
 commandPing = do
-    h <- connectMaster
-    hPrintM h Ping
-    rsp <- hGetResponse h
+    rsp <- sendRequest Ping
     case rsp of
         Nothing     -> return $ Left "Unknown Response"
         Just (NG e) -> return $ Left ("NG: " ++ e)
 
 commandRegister' :: String -> String -> [String] -> ClientCommand
 commandRegister' name cmd args = do
-    h <- connectMaster
     cwd <- io getWorkingDirectory
     env <- io getEnvironment
     let d = Daemon
                 , dEnv       = env
                 , dAutoStart = False
                 }
-    hPrintM h $ RegisterDaemon d
-    assertResponseOK =<< hGetResponse h
+    simpleCommand $ RegisterDaemon d
 
 commandUnregister :: String -> ClientCommand
-commandUnregister dn = do
-    h <- connectMaster
-    hPrintM h $ UnregisterDaemon dn
-    assertResponseOK =<< hGetResponse h
+commandUnregister dn = simpleCommand $ UnregisterDaemon dn
 
 commandUpdate :: String -> String -> [String] -> ClientCommand
 commandUpdate dn cmd args = do
             Right d -> return $ Just d
 
 commandStart :: String -> ClientCommand
-commandStart dn = do
-    h <- connectMaster
-    hPrintM h $ StartDaemon dn
-    assertResponseOK =<< hGetResponse h
+commandStart dn = simpleCommand $ StartDaemon dn
 
 commandStop :: String -> String -> ClientCommand
 commandStop dn sig = do
-    h <- connectMaster
-    hPrintM h $ SetAutoStart dn False
-    ret <- assertResponseOK =<< hGetResponse h
+    ret <- simpleCommand $ SetAutoStart dn False
     case ret of
         e@(Left _) -> return e
         Right _    -> commandKill dn sig
 
 commandKill :: String -> String -> ClientCommand
 commandKill dn sig = do
-    h <- connectMaster
     let msig = parseSignal sig
     case msig of
-        Nothing    -> return $ Left ("Invalid signal name: " ++ sig)
-        Just sig' -> do
-            hPrintM h $ KillDaemon dn sig'
-            assertResponseOK =<< hGetResponse h
+        Nothing   -> return $ Left ("Invalid signal name: " ++ sig)
+        Just sig' -> simpleCommand $ KillDaemon dn sig'
 
 commandList :: [String] -> ClientCommand
 commandList ("-v":_) = do
 
 commandListWith :: ((DaemonState, LoggerState) -> [(String, String)]) -> ClientCommand
 commandListWith printer = do
-    h <- connectMaster
-    hPrintM h GetListDaemons
-    rsp <- hGetResponse h
+    rsp <- sendRequest GetListDaemons
     case rsp of
         Nothing               -> return $ Left "Unknown Response"
         Just (NG e)           -> return $ Left ("NG: " ++ e)
     return . Left $ "Cannot execute command: " ++ show (cmd:args')
 
 commandShutdownMaster :: ClientCommand
-commandShutdownMaster = do
-    h <- connectMaster
-    hPrintM h ShutdownMaster
-    assertResponseOK =<< hGetResponse h
+commandShutdownMaster = simpleCommand ShutdownMaster
 
 commandRestartMaster :: ClientCommand
 commandRestartMaster = do
-    h <- connectMaster
     masterPath <- io getMasterPath
-    hPrintM h $ RestartMaster masterPath ["--resume"]
-    assertResponseOK =<< hGetResponse h
+    simpleCommand $ RestartMaster masterPath ["--resume"]
 
 commandVersion :: ClientCommand
 commandVersion = do
 
 commandMasterVersion :: ClientCommand
 commandMasterVersion = do
-    h <- connectMaster
-    hPrintM h $ GetMasterVersion
-    rsp <- hGetResponse h
+    rsp <- sendRequest GetMasterVersion
     case rsp of
         Nothing                  -> return $ Left "Unknown Response"
         Just (NG e)              -> return $ Left ("NG: " ++ e)
         Just (MasterVersion n v) -> return $ Right (n ++ " " ++ showVersion v)
         Just r                   -> return $ Left ("Invalid Response: " ++ show r)
 
+sendRequest :: Request -> Client (Maybe Response)
+sendRequest req = do
+    h <- connectMaster
+    hPrintM h req
+    hGetResponse h
+
 connectMaster :: Client Handle
 connectMaster = do
     sp <- asks masterSock