on master help sign in
Merge pull request #30 from gregwebs/yaml2json

Yaml2json

commit5b794576ab8fc26b5ece211106e1dec28042f674 authorMichael Snoyman <michael@snoyman.com> dateSun 8 Sep 2013, 07:39 (13 years ago) parentd8bd9ebb224366
3 files changed+48
.gitignore+2 exe/yaml2json.hs+28 yaml.cabal+18
.gitignore+2
@@ -3,3 +3,5 @@ dist
33tmp.yaml
44tmp2.yaml
55cabal-dev/
6+.cabal-sandbox/
7+cabal.sandbox.config
exe/yaml2json.hsnew file+28
@@ -0,0 +1,28 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+
3+import Data.Yaml (decodeFileEither, decodeEither')
4+import System.Environment (getArgs)
5+import System.Exit
6+import Data.Aeson (encode, Value)
7+import Prelude hiding (putStr, getContents)
8+import Data.ByteString.Lazy (putStr)
9+import Data.ByteString (getContents)
10+
11+helpMessage :: IO ()
12+helpMessage = putStrLn "yaml2json FILE\n use - as FILE 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
18+
19+main :: IO ()
20+main = do
21+ args <- getArgs
22+ case args of
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
28+
yaml.cabal+18
@@ -29,6 +29,10 @@ extra-source-files: c/helper.h,
2929 test/largest-string.yaml
3030 test/json.yaml
3131
32+flag no-exe
33+ description: don't install the yaml2json executable
34+ default: False
35+
3236flag system-libyaml
3337 description: Use the system-wide libyaml instead of the bundled copy
3438 default: False
@@ -63,6 +67,20 @@ library
6367 libyaml/writer.c
6468 include-dirs: libyaml
6569
70+executable yaml2json
71+ if flag(no-exe)
72+ Buildable: False
73+ else
74+ Buildable: True
75+
76+ hs-source-dirs: exe
77+ main-is: yaml2json.hs
78+ build-depends: base >= 4 && < 5
79+ , yaml
80+ , bytestring >= 0.9.1.4
81+ , aeson >= 0.5
82+
83+
6684test-suite test
6785 type: exitcode-stdio-1.0
6886 hs-source-dirs: test