Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Add command line option to render many documents #59

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dhall-json.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Executable dhall-to-yaml
dhall-json ,
optparse-applicative ,
yaml >= 0.5.0 && < 0.11,
vector ,
text
GHC-Options: -Wall

Expand Down
18 changes: 17 additions & 1 deletion dhall-to-yaml/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.ByteString
import qualified Data.Text.IO
import qualified Data.Vector
import qualified Data.Yaml
import qualified Dhall
import qualified Dhall.JSON
Expand All @@ -23,13 +24,15 @@ import qualified System.IO
data Options = Options
{ explain :: Bool
, omitNull :: Bool
, documents :: Bool
, conversion :: Conversion
}

parseOptions :: Parser Options
parseOptions = Options.Applicative.helper <*> do
explain <- parseExplain
omitNull <- parseOmitNull
documents <- parseDocuments
conversion <- Dhall.JSON.parseConversion
return (Options {..})
where
Expand All @@ -45,6 +48,12 @@ parseOptions = Options.Applicative.helper <*> do
<> Options.Applicative.help "Omit record fields that are null"
)

parseDocuments =
Options.Applicative.switch
( Options.Applicative.long "documents"
<> Options.Applicative.help "If given a Dhall list, output a document for every element"
)

parserInfo :: ParserInfo Options
parserInfo =
Options.Applicative.info
Expand All @@ -68,7 +77,14 @@ main = do

json <- omittingNull <$> explaining (Dhall.JSON.codeToValue conversion "(stdin)" stdin)

Data.ByteString.putStr $ Data.Yaml.encode json
let yaml = case (documents, json) of
(True, Data.Yaml.Array elems)
-> Data.ByteString.intercalate "\n---\n"
$ fmap Data.Yaml.encode
$ Data.Vector.toList elems
_ -> Data.Yaml.encode json

Data.ByteString.putStr yaml

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down