summaryrefslogtreecommitdiff
path: root/dhall-to-yaml/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dhall-to-yaml/Main.hs')
-rw-r--r--dhall-to-yaml/Main.hs68
1 files changed, 27 insertions, 41 deletions
diff --git a/dhall-to-yaml/Main.hs b/dhall-to-yaml/Main.hs
index 06aacc6..bddb5a6 100644
--- a/dhall-to-yaml/Main.hs
+++ b/dhall-to-yaml/Main.hs
@@ -1,33 +1,23 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-
module Main where
+import Control.Applicative (optional)
import Control.Exception (SomeException)
-import Data.Aeson (Value)
import Data.Monoid ((<>))
-import Dhall.JSON (Conversion, SpecialDoubleMode(..))
+import Dhall.JSON (parseOmission, parseConversion)
+import Dhall.Yaml (Options(..), dhallToYaml, parseDocuments, parseQuoted)
import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.ByteString
-import qualified Data.Text.IO
-import qualified Dhall
-import qualified Dhall.JSON
-import qualified Dhall.Yaml
+import qualified Data.Text as Text
+import qualified Data.Text.IO as Text.IO
import qualified GHC.IO.Encoding
-import qualified Options.Applicative
+import qualified Options.Applicative as Options
import qualified System.Exit
import qualified System.IO
-data Options = Options
- { explain :: Bool
- , omission :: Value -> Value
- , documents :: Bool
- , quoted :: Bool
- , conversion :: Conversion
- }
-
parseOptions :: Parser Options
parseOptions =
Options
@@ -36,49 +26,45 @@ parseOptions =
<*> parseDocuments
<*> parseQuoted
<*> Dhall.JSON.parseConversion
+ <*> optional parseFile
where
parseExplain =
- Options.Applicative.switch
- ( Options.Applicative.long "explain"
- <> Options.Applicative.help "Explain error messages in detail"
- )
-
- parseDocuments =
- Options.Applicative.switch
- ( Options.Applicative.long "documents"
- <> Options.Applicative.help "If given a Dhall list, output a document for every element"
+ Options.switch
+ ( Options.long "explain"
+ <> Options.help "Explain error messages in detail"
)
- parseQuoted =
- Options.Applicative.switch
- ( Options.Applicative.long "quoted"
- <> Options.Applicative.help "Prevent from generating not quoted scalars"
+ parseFile =
+ Options.strOption
+ ( Options.long "file"
+ <> Options.help "Read expression from a file instead of standard input"
+ <> Options.metavar "FILE"
)
parserInfo :: ParserInfo Options
parserInfo =
- Options.Applicative.info
- (Options.Applicative.helper <*> parseOptions)
- ( Options.Applicative.fullDesc
- <> Options.Applicative.progDesc "Compile Dhall to YAML"
+ Options.info
+ (Options.helper <*> parseOptions)
+ ( Options.fullDesc
+ <> Options.progDesc "Compile Dhall to YAML"
)
main :: IO ()
main = do
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
- Options {..} <- Options.Applicative.execParser parserInfo
+ options@Options {..} <- Options.execParser parserInfo
handle $ do
- let explaining = if explain then Dhall.detailed else id
-
- stdin <- Data.Text.IO.getContents
-
- json <- omission <$> explaining (Dhall.JSON.codeToValue conversion UseYAMLEncoding "(stdin)" stdin)
+ contents <- case file of
+ Nothing -> Text.IO.getContents
+ Just path -> Text.IO.readFile path
- let yaml = Dhall.Yaml.jsonToYaml json documents quoted
+ let path = case file of
+ Nothing -> "(stdin)"
+ Just p -> Text.pack p
- Data.ByteString.putStr yaml
+ Data.ByteString.putStr =<< dhallToYaml options path contents
handle :: IO a -> IO a
handle = Control.Exception.handle handler