From 005dc408dc09c3b479398ebe3e92efa2cd54846e Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 17 Oct 2015 02:26:25 +0200 Subject: Working prototype --- tprint/src/Main.hs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tprint/src/Main.hs (limited to 'tprint/src/Main.hs') diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs new file mode 100644 index 0000000..565295b --- /dev/null +++ b/tprint/src/Main.hs @@ -0,0 +1,77 @@ +{-# LANGUAGE RecordWildCards #-} + +import Thermoprint +import Thermoprint.Api + +import qualified BBCode (parse) + +import Options.Applicative + +import Data.Either +import Control.Monad +import Control.Monad.Trans.Either + +import System.IO +import System.Exit + +import Data.Proxy +import Servant.Client + +thermoprintApi :: Proxy ThermoprintApi +thermoprintApi = Proxy + +data Options = Options + { baseUrl :: BaseUrl + , printerId :: Integer + , dryRun :: Bool + } + +options :: Parser Options +options = Options + <$> option baseUrlReader ( + long "url" + <> short 'u' + <> metavar "URL" + <> help "The base url of the api" + <> value (BaseUrl Http "localhost" 8080) + <> showDefaultWith showBaseUrl + ) + <*> option auto ( + long "printer" + <> short 'p' + <> metavar "INT" + <> help "The number of the printer to use" + <> value 0 + <> showDefault + ) + <*> flag False True ( + long "dry-run" + <> short 'd' + <> help "Instead of sending data to printer output the parsed stream to stderr" + <> showDefault + ) + where + baseUrlReader = str >>= either readerError return . parseBaseUrl + +main :: IO () +main = execParser opts >>= main' + where + opts = info (helper <*> options) ( + fullDesc + <> header "tprint - A cli tool for interfacing with the REST api as provided by thermoprint-servant" + ) + + main' Options{..} = do + let + print :: Integer -> Block String -> EitherT ServantError IO () + print = client thermoprintApi baseUrl + input <- BBCode.parse `liftM` getContents + input' <- either (\err -> hPutStrLn stderr ("Parse error: " ++ err) >> exitFailure) return input + case dryRun of + False -> do + res <- runEitherT $ print printerId input' + case res of + Left err -> hPutStrLn stderr $ show err + Right _ -> exitSuccess + True -> do + hPutStrLn stderr $ show input' -- cgit v1.2.3