Ver código fonte

Add fromError function

jherve 1 ano atrás
pai
commit
342fc508ba
1 arquivos alterados com 22 adições e 8 exclusões
  1. 22 8
      src/Main.purs

+ 22 - 8
src/Main.purs

@@ -5,6 +5,7 @@ import Prelude
 import Data.Either (Either(..))
 import Data.Generic.Rep (class Generic)
 import Data.Show.Generic (genericShow)
+import Data.Tuple (Tuple(..))
 import Effect (Effect)
 import Effect.Aff (Aff, launchAff_)
 import Effect.Class.Console (log, logShow)
@@ -17,28 +18,33 @@ main = do
   log "hello"
   launchAff_ $ unit <$ runCommand "pwd" []
   launchAff_ do
-    res <- runCommand' toResult $ toCommand $ RecSelCommand {filePath: "/tmp/dummy_jobs/jobs.rec", recordType: "job_offer"}
+    res <- runCommand' toResult fromError $ toCommand $ RecSelCommand {filePath: "/tmp/dummy_jobs/jobs.rec", recordType: "job_offer"}
     logShow res
 
-runCommand ∷ String → Array String → Aff CommandResult
+runCommand ∷ String → Array String → Aff (Either CommandError String)
 runCommand cmd args = do
   process <- execa cmd args identity
   result <- process.getResult
   let
     ret = case result.exit of
       Normally 0 -> Right result.stdout
-      _ -> Left result.exit
+      _ -> Left $ Tuple result.exit result.stderr
 
   pure ret
 
 data CommandWithOpts = CommandWithOpts String (Array String)
-type CommandResult = Either Exit String
-type HighLevelCommandResult a = Either Exit a
+type CommandError = Tuple Exit String
 
-runCommand' ∷ forall a. (String -> a) -> CommandWithOpts → Aff (HighLevelCommandResult a)
-runCommand' f (CommandWithOpts cmd args) = do
+runCommand' ∷ forall e a. (String -> a) -> (CommandError -> e) -> CommandWithOpts → Aff (Either e a)
+runCommand' handleRes handleErr (CommandWithOpts cmd args) = do
   res <- runCommand cmd args
-  pure $ map f res
+
+  let
+    ret = case res of
+      Right s -> Right $ handleRes s
+      Left err -> Left $ handleErr err
+
+  pure ret
 
 data RecSelCommand = RecSelCommand {filePath :: String, recordType :: String}
 data RecSelCommandResult = RecSelCommandResult String
@@ -46,8 +52,16 @@ data RecSelCommandResult = RecSelCommandResult String
 derive instance Generic RecSelCommandResult _
 instance Show RecSelCommandResult where show = genericShow
 
+data RecSelCommandError = RecSelCommandError CommandError
+
+derive instance Generic RecSelCommandError _
+instance Show RecSelCommandError where show = genericShow
+
 toCommand ∷ RecSelCommand → CommandWithOpts
 toCommand (RecSelCommand {filePath, recordType}) = CommandWithOpts "recsel" [filePath, "-t", recordType]
 
 toResult :: String -> RecSelCommandResult
 toResult s = RecSelCommandResult s
+
+fromError :: CommandError -> RecSelCommandError
+fromError t = RecSelCommandError t