2019-05-30 17:54:35 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE PatternGuards #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import qualified Control.Exception
|
2019-06-08 05:26:05 +02:00
|
|
|
import Control.Exception (SomeException)
|
2019-05-30 17:54:35 +02:00
|
|
|
import Control.Monad (when)
|
2019-06-19 02:45:01 +02:00
|
|
|
import qualified Data.ByteString.Char8 as BSL8
|
2019-05-30 17:54:35 +02:00
|
|
|
import Data.Monoid ((<>))
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text.IO as Text
|
|
|
|
import Data.Version (showVersion)
|
|
|
|
import qualified GHC.IO.Encoding
|
|
|
|
import qualified Options.Applicative as O
|
|
|
|
import Options.Applicative (Parser, ParserInfo)
|
|
|
|
import qualified System.Exit
|
|
|
|
import qualified System.IO
|
|
|
|
|
2019-06-08 05:26:05 +02:00
|
|
|
import Dhall.JSONToDhall (Conversion, parseConversion)
|
|
|
|
import Dhall.YamlToDhall (Options(..), dhallFromYaml)
|
|
|
|
|
2019-05-30 17:54:35 +02:00
|
|
|
import qualified Paths_dhall_json as Meta
|
|
|
|
|
|
|
|
-- ---------------
|
|
|
|
-- Command options
|
|
|
|
-- ---------------
|
|
|
|
|
2019-06-08 05:26:05 +02:00
|
|
|
data CommandOptions = CommandOptions
|
|
|
|
{ version :: Bool
|
|
|
|
, schema :: Text
|
|
|
|
, conversion :: Conversion
|
|
|
|
} deriving Show
|
|
|
|
|
2019-05-30 17:54:35 +02:00
|
|
|
-- | Command info and description
|
2019-06-08 05:26:05 +02:00
|
|
|
parserInfo :: ParserInfo CommandOptions
|
2019-05-30 17:54:35 +02:00
|
|
|
parserInfo = O.info
|
|
|
|
( O.helper <*> parseOptions)
|
|
|
|
( O.fullDesc
|
|
|
|
<> O.progDesc "Populate Dhall value given its Dhall type (schema) from a YAML expression"
|
|
|
|
)
|
|
|
|
|
2019-06-08 05:26:05 +02:00
|
|
|
|
2019-05-30 17:54:35 +02:00
|
|
|
|
|
|
|
-- | Parser for all the command arguments and options
|
2019-06-08 05:26:05 +02:00
|
|
|
parseOptions :: Parser CommandOptions
|
|
|
|
parseOptions = CommandOptions <$> parseVersion
|
|
|
|
<*> parseSchema
|
|
|
|
<*> parseConversion
|
2019-05-30 17:54:35 +02:00
|
|
|
where
|
|
|
|
parseSchema = O.strArgument
|
|
|
|
( O.metavar "SCHEMA"
|
|
|
|
<> O.help "Dhall type expression (schema)"
|
|
|
|
)
|
|
|
|
parseVersion = O.switch
|
|
|
|
( O.long "version"
|
|
|
|
<> O.short 'V'
|
|
|
|
<> O.help "Display version"
|
|
|
|
)
|
|
|
|
|
|
|
|
-- ----------
|
|
|
|
-- Main
|
|
|
|
-- ----------
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8
|
|
|
|
|
2019-06-08 05:26:05 +02:00
|
|
|
CommandOptions{..} <- O.execParser parserInfo
|
2019-05-30 17:54:35 +02:00
|
|
|
|
|
|
|
when version $ do
|
|
|
|
putStrLn (showVersion Meta.version)
|
|
|
|
System.Exit.exitSuccess
|
|
|
|
|
|
|
|
handle $ do
|
|
|
|
stdin <- BSL8.getContents
|
|
|
|
|
2019-06-08 05:26:05 +02:00
|
|
|
Text.putStr =<< dhallFromYaml (Options schema conversion) stdin
|
2019-05-30 17:54:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
handle :: IO a -> IO a
|
|
|
|
handle = Control.Exception.handle handler
|
|
|
|
where
|
|
|
|
handler :: SomeException -> IO a
|
|
|
|
handler e = do
|
|
|
|
System.IO.hPutStrLn System.IO.stderr ""
|
|
|
|
System.IO.hPrint System.IO.stderr e
|
|
|
|
System.Exit.exitFailure
|