Skip to content

Instantly share code, notes, and snippets.

@myme
Last active October 23, 2018 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myme/ed1e460567828375c34654448dfb4637 to your computer and use it in GitHub Desktop.
Save myme/ed1e460567828375c34654448dfb4637 to your computer and use it in GitHub Desktop.
How to get output up until failure?
❯ stack turtle.hs
"foo"
"bar"
"Failed!"
❯ stack turtle.hs
Line "1"
Line "10"
Line "1019"
Line "1021"
Line "1027"
Line "1035"
["Failed!"]
#!/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
#!/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