@@ -1,18 +1,28 @@
11{-# LANGUAGE ScopedTypeVariables #-}
2−import Data.Yaml (decodeFileEither)
2+
3+import Data.Yaml (decodeFileEither, decodeEither')
34import System.Environment (getArgs)
45import System.Exit
56import Data.Aeson (encode, Value)
6−import Prelude hiding (putStr)
7+import Prelude hiding (putStr, getContents)
78import Data.ByteString.Lazy (putStr)
9+import Data.ByteString (getContents)
10+
11+helpMessage :: IO ()
12+helpMessage = print "yaml2json FILE\n use - to indicate stdin" >> exitFailure
13+
14+showJSON ejson =
15+ case ejson of
16+ Left err -> print err >> exitFailure
17+ Right (res :: Value) -> putStr (encode res) >> exitSuccess
818
19+main :: IO ()
920main = do
1021 args <- getArgs
1122 case args of
12− [] -> print "yaml2json FILE" >> exitFailure
13− (f:args) -> do
14− ejson <- decodeFileEither f
15− case ejson of
16− Left err -> print err >> exitFailure
17− Right (res :: Value) -> putStr (encode res) >> exitSuccess
23+ [] -> helpMessage
24+ (f:a:_) -> helpMessage
25+ -- strict getContents will read in all of stdin at once
26+ ("-":[]) -> getContents >>= showJSON . decodeEither'
27+ (f:[]) -> decodeFileEither f >>= showJSON
1828