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