parent
cb5ccab636
commit
23910e70e3
|
@ -14,7 +14,6 @@ import Options.Applicative (Parser, ParserInfo)
|
||||||
import qualified Control.Exception
|
import qualified Control.Exception
|
||||||
import qualified Data.Aeson
|
import qualified Data.Aeson
|
||||||
import qualified Data.Aeson.Encode.Pretty
|
import qualified Data.Aeson.Encode.Pretty
|
||||||
import qualified Data.ByteString.Char8
|
|
||||||
import qualified Data.ByteString.Lazy
|
import qualified Data.ByteString.Lazy
|
||||||
import qualified Data.Text.IO as Text.IO
|
import qualified Data.Text.IO as Text.IO
|
||||||
import qualified Dhall
|
import qualified Dhall
|
||||||
|
@ -33,6 +32,7 @@ data Options
|
||||||
, conversion :: Conversion
|
, conversion :: Conversion
|
||||||
, approximateSpecialDoubles :: Bool
|
, approximateSpecialDoubles :: Bool
|
||||||
, file :: Maybe FilePath
|
, file :: Maybe FilePath
|
||||||
|
, output :: Maybe FilePath
|
||||||
}
|
}
|
||||||
| Version
|
| Version
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ parseOptions =
|
||||||
<*> Dhall.JSON.parseConversion
|
<*> Dhall.JSON.parseConversion
|
||||||
<*> parseApproximateSpecialDoubles
|
<*> parseApproximateSpecialDoubles
|
||||||
<*> optional parseFile
|
<*> optional parseFile
|
||||||
|
<*> optional parseOutput
|
||||||
)
|
)
|
||||||
<|> parseVersion
|
<|> parseVersion
|
||||||
where
|
where
|
||||||
|
@ -94,6 +95,13 @@ parseOptions =
|
||||||
<> Options.metavar "FILE"
|
<> Options.metavar "FILE"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parseOutput =
|
||||||
|
Options.strOption
|
||||||
|
( Options.long "output"
|
||||||
|
<> Options.help "Write JSON to a file instead of standard output"
|
||||||
|
<> Options.metavar "FILE"
|
||||||
|
)
|
||||||
|
|
||||||
parserInfo :: ParserInfo Options
|
parserInfo :: ParserInfo Options
|
||||||
parserInfo =
|
parserInfo =
|
||||||
Options.info
|
Options.info
|
||||||
|
@ -137,7 +145,12 @@ main = do
|
||||||
|
|
||||||
json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode file text)
|
json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode file text)
|
||||||
|
|
||||||
Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
|
let write =
|
||||||
|
case output of
|
||||||
|
Nothing -> Data.ByteString.Lazy.putStr
|
||||||
|
Just file_ -> Data.ByteString.Lazy.writeFile file_
|
||||||
|
|
||||||
|
write (encode json <> "\n")
|
||||||
|
|
||||||
handle :: IO a -> IO a
|
handle :: IO a -> IO a
|
||||||
handle = Control.Exception.handle handler
|
handle = Control.Exception.handle handler
|
||||||
|
|
|
@ -29,6 +29,7 @@ parseOptions =
|
||||||
<*> parseQuoted
|
<*> parseQuoted
|
||||||
<*> Dhall.JSON.parseConversion
|
<*> Dhall.JSON.parseConversion
|
||||||
<*> optional parseFile
|
<*> optional parseFile
|
||||||
|
<*> optional parseOutput
|
||||||
)
|
)
|
||||||
<|> parseVersion
|
<|> parseVersion
|
||||||
where
|
where
|
||||||
|
@ -52,6 +53,13 @@ parseOptions =
|
||||||
<> Options.help "Display version"
|
<> Options.help "Display version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parseOutput =
|
||||||
|
Options.strOption
|
||||||
|
( Options.long "output"
|
||||||
|
<> Options.help "Write YAML to a file instead of standard output"
|
||||||
|
<> Options.metavar "FILE"
|
||||||
|
)
|
||||||
|
|
||||||
parserInfo :: ParserInfo (Maybe Options)
|
parserInfo :: ParserInfo (Maybe Options)
|
||||||
parserInfo =
|
parserInfo =
|
||||||
Options.info
|
Options.info
|
||||||
|
@ -76,7 +84,12 @@ main = do
|
||||||
Nothing -> Text.IO.getContents
|
Nothing -> Text.IO.getContents
|
||||||
Just path -> Text.IO.readFile path
|
Just path -> Text.IO.readFile path
|
||||||
|
|
||||||
Data.ByteString.putStr =<< dhallToYaml options file contents
|
let write =
|
||||||
|
case output of
|
||||||
|
Nothing -> Data.ByteString.putStr
|
||||||
|
Just file_ -> Data.ByteString.writeFile file_
|
||||||
|
|
||||||
|
write =<< dhallToYaml options file contents
|
||||||
|
|
||||||
handle :: IO a -> IO a
|
handle :: IO a -> IO a
|
||||||
handle = Control.Exception.handle handler
|
handle = Control.Exception.handle handler
|
||||||
|
|
|
@ -23,6 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as ByteString
|
||||||
import qualified Data.Text.IO as Text.IO
|
import qualified Data.Text.IO as Text.IO
|
||||||
import qualified Data.Text.Prettyprint.Doc as Pretty
|
import qualified Data.Text.Prettyprint.Doc as Pretty
|
||||||
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
||||||
|
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
|
||||||
import qualified GHC.IO.Encoding
|
import qualified GHC.IO.Encoding
|
||||||
import qualified Options.Applicative as Options
|
import qualified Options.Applicative as Options
|
||||||
import qualified System.Console.ANSI as ANSI
|
import qualified System.Console.ANSI as ANSI
|
||||||
|
@ -49,6 +50,7 @@ data Options
|
||||||
{ schema :: Text
|
{ schema :: Text
|
||||||
, conversion :: Conversion
|
, conversion :: Conversion
|
||||||
, file :: Maybe FilePath
|
, file :: Maybe FilePath
|
||||||
|
, output :: Maybe FilePath
|
||||||
, ascii :: Bool
|
, ascii :: Bool
|
||||||
, plain :: Bool
|
, plain :: Bool
|
||||||
}
|
}
|
||||||
|
@ -62,6 +64,7 @@ parseOptions =
|
||||||
<$> parseSchema
|
<$> parseSchema
|
||||||
<*> parseConversion
|
<*> parseConversion
|
||||||
<*> optional parseFile
|
<*> optional parseFile
|
||||||
|
<*> optional parseOutput
|
||||||
<*> parseASCII
|
<*> parseASCII
|
||||||
<*> parsePlain
|
<*> parsePlain
|
||||||
)
|
)
|
||||||
|
@ -88,6 +91,13 @@ parseOptions =
|
||||||
<> Options.metavar "FILE"
|
<> Options.metavar "FILE"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parseOutput =
|
||||||
|
Options.strOption
|
||||||
|
( Options.long "output"
|
||||||
|
<> Options.help "Write Dhall expression to a file instead of standard output"
|
||||||
|
<> Options.metavar "FILE"
|
||||||
|
)
|
||||||
|
|
||||||
parseASCII =
|
parseASCII =
|
||||||
Options.switch
|
Options.switch
|
||||||
( Options.long "ascii"
|
( Options.long "ascii"
|
||||||
|
@ -138,16 +148,24 @@ main = do
|
||||||
|
|
||||||
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
||||||
|
|
||||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
case output of
|
||||||
|
Nothing -> do
|
||||||
|
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||||
|
|
||||||
let ansiStream =
|
let ansiStream =
|
||||||
if supportsANSI && not plain
|
if supportsANSI && not plain
|
||||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||||
else Pretty.unAnnotateS stream
|
else Pretty.unAnnotateS stream
|
||||||
|
|
||||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||||
|
|
||||||
Text.IO.putStrLn ""
|
Text.IO.putStrLn ""
|
||||||
|
|
||||||
|
Just file_ ->
|
||||||
|
IO.withFile file_ IO.WriteMode $ \h -> do
|
||||||
|
Pretty.Text.renderIO h stream
|
||||||
|
|
||||||
|
Text.IO.hPutStrLn h ""
|
||||||
|
|
||||||
handle :: IO a -> IO a
|
handle :: IO a -> IO a
|
||||||
handle = Control.Exception.handle handler
|
handle = Control.Exception.handle handler
|
||||||
|
|
|
@ -38,6 +38,7 @@ data Options = Options
|
||||||
, quoted :: Bool
|
, quoted :: Bool
|
||||||
, conversion :: Conversion
|
, conversion :: Conversion
|
||||||
, file :: Maybe FilePath
|
, file :: Maybe FilePath
|
||||||
|
, output :: Maybe FilePath
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultOptions :: Options
|
defaultOptions :: Options
|
||||||
|
@ -48,6 +49,7 @@ defaultOptions =
|
||||||
, quoted = False
|
, quoted = False
|
||||||
, conversion = NoConversion
|
, conversion = NoConversion
|
||||||
, file = Nothing
|
, file = Nothing
|
||||||
|
, output = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
parseDocuments :: Parser Bool
|
parseDocuments :: Parser Bool
|
||||||
|
|
|
@ -23,6 +23,7 @@ import qualified Data.ByteString.Char8 as BSL8
|
||||||
import qualified Data.Text.IO as Text.IO
|
import qualified Data.Text.IO as Text.IO
|
||||||
import qualified Data.Text.Prettyprint.Doc as Pretty
|
import qualified Data.Text.Prettyprint.Doc as Pretty
|
||||||
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
||||||
|
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
|
||||||
import qualified Dhall.Pretty
|
import qualified Dhall.Pretty
|
||||||
import qualified GHC.IO.Encoding
|
import qualified GHC.IO.Encoding
|
||||||
import qualified Options.Applicative as Options
|
import qualified Options.Applicative as Options
|
||||||
|
@ -40,6 +41,7 @@ data CommandOptions
|
||||||
{ schema :: Text
|
{ schema :: Text
|
||||||
, conversion :: Conversion
|
, conversion :: Conversion
|
||||||
, file :: Maybe FilePath
|
, file :: Maybe FilePath
|
||||||
|
, output :: Maybe FilePath
|
||||||
, ascii :: Bool
|
, ascii :: Bool
|
||||||
, plain :: Bool
|
, plain :: Bool
|
||||||
}
|
}
|
||||||
|
@ -61,6 +63,7 @@ parseOptions =
|
||||||
<$> parseSchema
|
<$> parseSchema
|
||||||
<*> parseConversion
|
<*> parseConversion
|
||||||
<*> optional parseFile
|
<*> optional parseFile
|
||||||
|
<*> optional parseOutput
|
||||||
<*> parseASCII
|
<*> parseASCII
|
||||||
<*> parsePlain
|
<*> parsePlain
|
||||||
)
|
)
|
||||||
|
@ -87,6 +90,13 @@ parseOptions =
|
||||||
<> Options.metavar "FILE"
|
<> Options.metavar "FILE"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parseOutput =
|
||||||
|
Options.strOption
|
||||||
|
( Options.long "output"
|
||||||
|
<> Options.help "Write Dhall expression to a file instead of standard output"
|
||||||
|
<> Options.metavar "FILE"
|
||||||
|
)
|
||||||
|
|
||||||
parseASCII =
|
parseASCII =
|
||||||
Options.switch
|
Options.switch
|
||||||
( Options.long "ascii"
|
( Options.long "ascii"
|
||||||
|
@ -129,17 +139,24 @@ main = do
|
||||||
|
|
||||||
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
||||||
|
|
||||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
case output of
|
||||||
|
Nothing -> do
|
||||||
|
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||||
|
|
||||||
let ansiStream =
|
let ansiStream =
|
||||||
if supportsANSI && not plain
|
if supportsANSI && not plain
|
||||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||||
else Pretty.unAnnotateS stream
|
else Pretty.unAnnotateS stream
|
||||||
|
|
||||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||||
|
|
||||||
Text.IO.putStrLn ""
|
Text.IO.putStrLn ""
|
||||||
|
|
||||||
|
Just file_ ->
|
||||||
|
IO.withFile file_ IO.WriteMode $ \h -> do
|
||||||
|
Pretty.Text.renderIO h stream
|
||||||
|
|
||||||
|
Text.IO.hPutStrLn h ""
|
||||||
|
|
||||||
handle :: IO a -> IO a
|
handle :: IO a -> IO a
|
||||||
handle = Control.Exception.handle handler
|
handle = Control.Exception.handle handler
|
||||||
|
|
Loading…
Reference in New Issue
Block a user