{-# 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'