浏览代码

Some higher-level command

jherve 1 年之前
父节点
当前提交
c1c341a9fc
共有 1 个文件被更改,包括 38 次插入10 次删除
  1. 38 10
      src/Main.purs

+ 38 - 10
src/Main.purs

@@ -2,9 +2,12 @@ module Main where
 
 import Prelude
 
+import Data.Either (Either(..))
+import Data.Generic.Rep (class Generic)
+import Data.Show.Generic (genericShow)
 import Effect (Effect)
 import Effect.Aff (Aff, launchAff_)
-import Effect.Class.Console (log)
+import Effect.Class.Console (log, logShow)
 import Node.ChildProcess.Types (Exit(..))
 import Node.Library.Execa (execa)
 
@@ -12,14 +15,39 @@ import Node.Library.Execa (execa)
 main :: Effect Unit
 main = do
   log "hello"
-  launchAff_ $ runCommand "pwd" []
+  launchAff_ $ unit <$ runCommand "pwd" []
+  launchAff_ do
+    res <- runCommand' toResult $ toCommand $ RecSelCommand {filePath: "/tmp/dummy_jobs/jobs.rec", recordType: "job_offer"}
+    logShow res
 
-runCommand ∷ String → Array String → Aff Unit
+runCommand ∷ String → Array String → Aff CommandResult
 runCommand cmd args = do
-    process <- execa cmd args identity
-    result <- process.getResult
-    case result.exit of
-      Normally 0 -> do
-        log "worked"
-        log result.stdout
-      _ -> log "failed"
+  process <- execa cmd args identity
+  result <- process.getResult
+  let
+    ret = case result.exit of
+      Normally 0 -> Right result.stdout
+      _ -> Left result.exit
+
+  pure ret
+
+data CommandWithOpts = CommandWithOpts String (Array String)
+type CommandResult = Either Exit String
+type HighLevelCommandResult a = Either Exit a
+
+runCommand' ∷ forall a. (String -> a) -> CommandWithOpts → Aff (HighLevelCommandResult a)
+runCommand' f (CommandWithOpts cmd args) = do
+  res <- runCommand cmd args
+  pure $ map f res
+
+data RecSelCommand = RecSelCommand {filePath :: String, recordType :: String}
+data RecSelCommandResult = RecSelCommandResult String
+
+derive instance Generic RecSelCommandResult _
+instance Show RecSelCommandResult where show = genericShow
+
+toCommand ∷ RecSelCommand → CommandWithOpts
+toCommand (RecSelCommand {filePath, recordType}) = CommandWithOpts "recsel" [filePath, "-t", recordType]
+
+toResult :: String -> RecSelCommandResult
+toResult s = RecSelCommandResult s