packages feed

language-conf 0.2.1.0 → 0.2.2.0

raw patch · 5 files changed

+84/−19 lines, 5 filesdep +optparse-applicative

Dependencies added: optparse-applicative

Files

bin/ConfFmt.hs view
@@ -1,20 +1,52 @@-import qualified Data.Conf             as Conf-import qualified Data.Conf.PrettyPrint as Conf-import qualified Data.Text.IO          as Text (readFile)-import           System.Environment    (getArgs)-import           System.Exit           (exitFailure)+{-# LANGUAGE RecordWildCards #-}+import           Control.Monad+import qualified Data.Conf                 as Conf+import qualified Data.Text.IO              as Text (hGetContents, readFile)+import           Options.Applicative+import           Options.Applicative.Types+import           System.Environment        (getArgs)+import           System.Exit               (exitFailure) import           System.IO-import qualified Text.Megaparsec       as Megaparsec+import qualified Text.Megaparsec           as Megaparsec +data ConfFmtOptions+    = ConfFmtOptions { cfoInplace :: Bool+                     , cfoOutput  :: Maybe FilePath+                     , cfoInput   :: Maybe FilePath+                     }++options :: Parser ConfFmtOptions+options = ConfFmtOptions+    <$> cfoInplaceAP+    <*> cfoOutputAP+    <*> cfoInputAP++cfoInputAP = optional (argument str+    (metavar "INPUTFILE"+    <> help "The input file (omission will cause `conffmt` to use stdin)"))++cfoOutputAP = optional (option str (short 'o' <> long "output"))+cfoInplaceAP = switch (long "inplace"+    <> short 'i'+    <> help "Format the FILE inplace, replacing it's contents")+ main :: IO () main = do-    as <- getArgs-    case as of-        (filePath:_) -> do-            contents <- Text.readFile filePath-            case Megaparsec.parse Conf.conf filePath contents of-                Left err -> hPrint stderr err-                Right ast -> print (Conf.pPrint ast)-        _ -> do-            hPutStrLn stderr "Usage: conffmt <conf-file>"-            exitFailure+    ConfFmtOptions{..} <- execParser (info (helper <*> options)+                                   ( fullDesc+                                     <> progDesc "Format a .conf FILE"+                                     <> header "conffmt - Conf formatter using Haskell language-conf"+                                   ))+    (contents, fp) <- case cfoInput of+        Just fp ->+            (,) <$> Text.readFile fp <*> pure fp+        Nothing ->+            (,) <$> Text.hGetContents stdin <*> pure "<stdin>"+    case Megaparsec.parse Conf.conf fp contents of+        Left err -> hPrint stderr err+        Right ast -> do+            let output = show (Conf.pPrintConf ast)+            case (cfoInplace, cfoOutput) of+                (_, Just outputFp) -> writeFile outputFp output+                (True, _) -> writeFile fp output+                _ -> putStr output
bin/ToConf.hs view
@@ -21,7 +21,7 @@                     let conf :: Aeson.Result Conf.Conf                         conf = Aeson.fromJSON v                     writeFile output $ show $ case conf of-                        Aeson.Success c -> Conf.pPrint c+                        Aeson.Success c -> Conf.pPrintConf c                         Aeson.Error e -> error ("Tranformation failure: " <> e)                 Nothing -> error ("Parse failure " <> fp)         _ -> error "Usage: toconf <inputfile> <outputfile>"
language-conf.cabal view
@@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack  name:           language-conf-version:        0.2.1.0+version:        0.2.2.0 synopsis:       Conf parsers and pretty-printers for the Haskell programming language. description:    @language-conf@ contains @.conf@ (e.g. nginx configuration) parsers and pretty-printers for the Haskell programming language.                 "Data.Conf" exports all the API surface in the package.@@ -74,6 +74,7 @@   build-depends:       base     , text+    , optparse-applicative     , megaparsec     , language-conf     , pretty
src/Data/Conf/PrettyPrint.hs view
@@ -7,6 +7,8 @@ Stability: experimental Portability: unknown +import Data.Char+import Debug.Trace Pretty-printer for "Data.Conf". Declares a 'Pretty' instance for 'ConfStatement'. -}@@ -19,11 +21,14 @@     )   where +import           Data.Char                      (isSpace) import           Data.Conf.Types import           Data.Text                      (Text) import qualified Data.Text                      as Text import           Text.PrettyPrint.HughesPJClass +import           Debug.Trace+ -- | Pretty-prints a 'Conf' to a 'Doc' -- -- 'pPrint' restricted to 'Conf'@@ -32,8 +37,18 @@ -- print (pPrintConf c) -- @ --+-- Because of https://github.com/haskell/pretty/issues/26, it's not easy to+-- prevent trailing spaces while generating the output. This function patches+-- it at the end so there're no trailing spaces.+-- -- See "Text.PrettyPrint"-pPrintConf = pPrint :: Conf -> Doc+pPrintConf :: Conf -> Doc+pPrintConf c =+    let d = pPrint c+        ds = init (unlines (map stripEnd (lines (show d))))+    in text ds+  where+    stripEnd = reverse . dropWhile isSpace . reverse  instance Pretty ConfStatement where     pPrint s = case s of
test/Data/ConfSpec.hs view
@@ -11,6 +11,23 @@  spec :: Spec spec = do+    describe "pPrintConf" $ do+        it "#3 - pretty prints nested lines without trailing spaces" $ do+            let Right inp = parse conf "" $ Text.unlines+                      [ "http {"+                      , "  listen 8989;"+                      , ""+                      , "  proxy_pass something;"+                      , "}"+                      ]+            show (pPrintConf inp) `shouldBe` init (unlines+                      [ "http {"+                      , "  listen 8989;"+                      , ""+                      , "  proxy_pass something;"+                      , "}"+                      ])+     describe "comment" $         it "parses a comment" $ do             let inp = Text.unlines [ "# something here"