-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMain.hs
61 lines (55 loc) · 2.06 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Main where
import Control.Monad.Except
import qualified Data.Aeson as J
import Data.Bifunctor (first)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Data.Text as T
import Kriti (renderPretty, runKritiBSWith)
import Kriti.CustomFunctions (basicFuncMap)
import Options.Applicative
import Prettyprinter
import System.IO (IOMode (ReadMode), openFile)
data KritiOptions = KritiOptions
{ _koJSONFile :: FilePath,
_koTemplateFile :: FilePath,
_koBindingSymbol :: T.Text
}
deriving (Show, Eq)
-- TODO: we should add support for STDIN too
kritiOpts :: Parser KritiOptions
kritiOpts =
KritiOptions
<$> strOption
( long "json" <> short 'j' <> metavar "JSON_FILE"
<> help "The JSON file to read"
)
<*> strOption
( long "template" <> short 't' <> metavar "TEMPLATE_FILE"
<> help "The template file to use"
)
<*> strOption
( long "bind" <> short 'b' <> metavar "BINDING_SYMBOL"
<> showDefault
<> value "$"
<> help "The symbol that's used to represent a JSON binding within the template"
)
main :: IO ()
main = do
let parserOptions =
info (kritiOpts <**> helper) $
fullDesc
<> progDesc "transform JSON using the Kriti language"
<> header "kriti - a minimal JSON templating language based on Go's template language."
kritiOptions <- execParser parserOptions
result <- runExceptT $ runKriti kritiOptions
either (print . pretty) (C8.putStrLn . J.encode) result
runKriti :: KritiOptions -> ExceptT T.Text IO J.Value
runKriti (KritiOptions jsonFile templateFile rootSymbol) = do
let checkFilePath = flip openFile ReadMode
void $ liftIO $ checkFilePath jsonFile
void $ liftIO $ checkFilePath templateFile
json <- ExceptT $ first T.pack . J.eitherDecode <$> LBS.readFile jsonFile
template <- liftIO $ B.readFile templateFile
ExceptT $ pure $ first renderPretty $ runKritiBSWith template [(rootSymbol, json)] basicFuncMap