diff options
| author | Mirek Kratochvil <exa.exa@gmail.com> | 2023-05-27 20:20:43 +0200 |
|---|---|---|
| committer | Mirek Kratochvil <exa.exa@gmail.com> | 2023-05-27 20:20:43 +0200 |
| commit | 73498534cfdccab95e580f8d6b121655d00e7578 (patch) | |
| tree | f1160d0c93833cbf668700a096e2bf4a2b7ff4ea /mustache/app | |
| parent | 35837f5607986b18746590c1611927d59cbe8c87 (diff) | |
| download | reploy-73498534cfdccab95e580f8d6b121655d00e7578.tar.gz reploy-73498534cfdccab95e580f8d6b121655d00e7578.tar.bz2 | |
fork mustache
Diffstat (limited to 'mustache/app')
| -rw-r--r-- | mustache/app/Main.hs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/mustache/app/Main.hs b/mustache/app/Main.hs new file mode 100644 index 0000000..2f201e4 --- /dev/null +++ b/mustache/app/Main.hs @@ -0,0 +1,72 @@ +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE NamedFieldPuns #-} +module Main (main) where + + +import Data.Aeson (Value, eitherDecode) +import Data.Bifunctor (first) +import qualified Data.ByteString as B (readFile) +import qualified Data.ByteString.Lazy as BS (readFile) +import Data.Foldable (for_) +import qualified Data.Text.IO as TIO (putStrLn) +import Data.Yaml (decodeEither') + +import System.Console.CmdArgs.Implicit (Data, Typeable, argPos, args, + cmdArgs, def, help, summary, + typ, (&=)) +import System.FilePath (takeExtension) +import Text.Mustache (automaticCompile, substitute, + toMustache) + + +data Arguments = Arguments + { template :: FilePath + , templateDirs :: [FilePath] + , dataFiles :: [FilePath] + } deriving (Show, Data, Typeable) + + +commandArgs :: Arguments +commandArgs = Arguments + { template = def + &= argPos 0 + &= typ "TEMPLATE" + , dataFiles = def + &= args + &= typ "DATA-FILES" + , templateDirs = ["."] + &= help "The directories in which to search for the templates" + &= typ "DIRECTORIES" + } &= summary "Simple mustache template subtitution" + + +readJSON :: FilePath -> IO (Either String Value) +readJSON = fmap eitherDecode . BS.readFile + + +readYAML :: FilePath -> IO (Either String Value) +readYAML = fmap (first show . decodeEither') . B.readFile + + +main :: IO () +main = do + (Arguments { template, templateDirs, dataFiles }) <- cmdArgs commandArgs + + eitherTemplate <- automaticCompile templateDirs template + + case eitherTemplate of + Left err -> print err + Right compiledTemplate -> + for_ dataFiles $ \file -> do + + let decoder = + case takeExtension file of + ".yml" -> readYAML + ".yaml" -> readYAML + _ -> readJSON + decoded <- decoder file + + either + putStrLn + (TIO.putStrLn . substitute compiledTemplate . toMustache) + decoded |
