Last active
October 23, 2018 20:42
-
-
Save myme/ed1e460567828375c34654448dfb4637 to your computer and use it in GitHub Desktop.
How to get output up until failure?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ stack turtle.hs | |
"foo" | |
"bar" | |
"Failed!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ stack turtle.hs | |
Line "1" | |
Line "10" | |
Line "1019" | |
Line "1021" | |
Line "1027" | |
Line "1035" | |
["Failed!"] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- | |
stack script | |
--resolver lts-12.10 | |
--package exceptions | |
--package foldl | |
--package turtle | |
-} | |
{-# OPTIONS_GHC -Wall #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Monad.Catch | |
import Data.IORef | |
import Turtle | |
tryThing :: IO [Text] | |
tryThing = do | |
out <- newIORef [] | |
let reducer xs x' = do | |
modifyIORef out (<> [x']) | |
return xs | |
let shell' = do | |
line <- inshell "echo foo; echo bar; exit 1" empty | |
return $ lineToText line | |
let handler ExitSuccess = select ["Done!"] | |
handler _ = select ["Failed!"] | |
_ <- foldShell (shell' `catch` handler) $ FoldShell reducer [] pure | |
readIORef out | |
main :: IO () | |
main = tryThing >>= print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
{- | |
stack script | |
--resolver lts-12.10 | |
--package exceptions | |
--package foldl | |
--package turtle | |
-} | |
{-# OPTIONS_GHC -Wall #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Exception | |
import Turtle | |
tryThing :: IO [Text] | |
tryThing = do | |
let reducer xs x' = do | |
print x' | |
if length xs > 4 | |
then throwIO $ ExitFailure 1 | |
else return $ xs <> [lineToText x'] | |
foldShell (inshell "ls /proc" empty) $ FoldShell reducer [] pure | |
main :: IO () | |
main = tryThing `catch` handler >>= print | |
where handler ExitSuccess = return ["Done!"] | |
handler _ = return ["Failed!"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment