-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTee.hs
More file actions
61 lines (44 loc) · 1.46 KB
/
Tee.hs
File metadata and controls
61 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Coreutils.Tee where
import Control.Exception
import Control.Monad
import qualified Streaming.ByteString as Q
import System.Console.GetOpt
import System.Exit
import System.IO
import Coreutils.Util
data Tee = Tee
instance Util Tee where
run _ = teeMain
newtype Options = Options { optMode :: IOMode }
teeMain :: [String] -> IO ()
teeMain args = do
unless (null errors) $
die $ unlines errors
either die (`runTee` filenames) $
foldM (flip id) defaults opts
where
(opts, filenames, errors) = getOpt RequireOrder options args
runTee :: Options -> [FilePath] -> IO ()
-- get handles for all output files and stdout, run them through tee
runTee o fs =
bracket acquire release tee
where
acquire = mapM (`openBinaryFile` optMode o) fs
release = mapM_ hClose
tee :: [Handle] -> IO ()
-- build up n computations that copy the stream and write it a file
tee = Q.stdout . foldr (\h -> Q.toHandle h . Q.copy) Q.stdin
-- | Options
defaults :: Options
defaults = Options { optMode = WriteMode }
options :: [OptDescr (Options -> Either String Options)]
options =
[ Option "a" ["append"]
(NoArg
(\opt -> Right opt { optMode = AppendMode }))
"append to given files, do not overwrite"
, Option "h" ["help"]
(NoArg
(\_ -> Left $ usageInfo "tee" options))
"show this help text"
]