on master help sign in
Update yaml2json.hs
85c2339Ray Qiu
exe/yaml2json.hs+5−7
@@ -1,5 +1,3 @@
1−{-# LANGUAGE ScopedTypeVariables #-}
2−
3import Data.Yaml (decodeFileEither, decodeEither')
1import Data.Yaml (decodeFileEither, decodeEither')
4import System.Environment (getArgs)
2import System.Environment (getArgs)
5import System.Exit
3import System.Exit
@@ -11,18 +9,18 @@ import Data.ByteString (getContents)
11helpMessage :: IO ()
9helpMessage :: IO ()
12helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure
10helpMessage = putStrLn "yaml2json FILE\n use - as FILE to indicate stdin" >> exitFailure
13
11
12+showJSON :: Show a => Either a Value -> IO b
14showJSON ejson =
13showJSON ejson =
15 case ejson of
14 case ejson of
16 Left err -> print err >> exitFailure
15 Left err -> print err >> exitFailure
17− Right (res :: Value) -> putStr (encode res) >> exitSuccess
16+ Right res -> putStr (encode (res :: Value)) >> exitSuccess
18
17
19main :: IO ()
18main :: IO ()
20main = do
19main = do
21 args <- getArgs
20 args <- getArgs
22 case args of
21 case args of
23− [] -> helpMessage
24− (f:a:_) -> helpMessage
25 -- strict getContents will read in all of stdin at once
22 -- strict getContents will read in all of stdin at once
26− ("-":[]) -> getContents >>= showJSON . decodeEither'
23+ (["-"]) -> getContents >>= showJSON . decodeEither'
27− (f:[]) -> decodeFileEither f >>= showJSON
24+ ([f]) -> decodeFileEither f >>= showJSON
25+ _ -> helpMessage
28
26