diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-10-17 21:23:45 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-10-17 21:23:45 +0200 |
| commit | e65e1eaac335a4738abb9e8ee8da7a229f96c2c0 (patch) | |
| tree | 8711caffd49f24ee8136523e0aefc76e37d8666a /tprint/src | |
| parent | 005dc408dc09c3b479398ebe3e92efa2cd54846e (diff) | |
| download | thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.gz thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.bz2 thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.tar.xz thermoprint-e65e1eaac335a4738abb9e8ee8da7a229f96c2c0.zip | |
Drafts
Diffstat (limited to 'tprint/src')
| -rw-r--r-- | tprint/src/Main.hs | 222 |
1 files changed, 181 insertions, 41 deletions
diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index 565295b..0f88a86 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs | |||
| @@ -1,72 +1,178 @@ | |||
| 1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards, RankNTypes #-} |
| 2 | 2 | ||
| 3 | import Thermoprint | 3 | import Thermoprint |
| 4 | import Thermoprint.Api | 4 | import Thermoprint.Api |
| 5 | 5 | ||
| 6 | import qualified BBCode (parse) | 6 | import qualified BBCode (parse, make) |
| 7 | 7 | ||
| 8 | import Options.Applicative | 8 | import Options.Applicative |
| 9 | 9 | ||
| 10 | import Data.Either | 10 | import Data.Either |
| 11 | import Data.Maybe | ||
| 11 | import Control.Monad | 12 | import Control.Monad |
| 12 | import Control.Monad.Trans.Either | 13 | import Control.Monad.Trans.Either |
| 13 | 14 | ||
| 14 | import System.IO | 15 | import System.IO |
| 16 | import qualified System.IO as IO | ||
| 15 | import System.Exit | 17 | import System.Exit |
| 18 | import System.Environment | ||
| 16 | 19 | ||
| 17 | import Data.Proxy | 20 | import Data.Proxy |
| 21 | import Servant.API | ||
| 18 | import Servant.Client | 22 | import Servant.Client |
| 19 | 23 | ||
| 24 | import Data.Int (Int64) | ||
| 25 | |||
| 20 | thermoprintApi :: Proxy ThermoprintApi | 26 | thermoprintApi :: Proxy ThermoprintApi |
| 21 | thermoprintApi = Proxy | 27 | thermoprintApi = Proxy |
| 22 | 28 | ||
| 23 | data Options = Options | 29 | data TPrint = TPrint TPrintMode TPrintOptions |
| 24 | { baseUrl :: BaseUrl | 30 | |
| 25 | , printerId :: Integer | 31 | data TPrintOptions = TPrintOptions |
| 26 | , dryRun :: Bool | 32 | { baseUrl :: BaseUrl |
| 27 | } | 33 | } |
| 28 | 34 | ||
| 29 | options :: Parser Options | 35 | data TPrintMode = Print PrintOptions |
| 30 | options = Options | 36 | | PrintDraft PrintDraftOptions |
| 31 | <$> option baseUrlReader ( | 37 | | Query QueryOptions |
| 32 | long "url" | 38 | | Add AddOptions |
| 33 | <> short 'u' | 39 | | Get GetOptions |
| 34 | <> metavar "URL" | 40 | | Write WriteOptions |
| 35 | <> help "The base url of the api" | 41 | | Del DelOptions |
| 36 | <> value (BaseUrl Http "localhost" 8080) | 42 | |
| 37 | <> showDefaultWith showBaseUrl | 43 | data PrintOptions = PrintOptions |
| 38 | ) | 44 | { printerId :: Integer |
| 39 | <*> option auto ( | 45 | , dryRun :: Bool |
| 40 | long "printer" | 46 | } |
| 41 | <> short 'p' | 47 | |
| 42 | <> metavar "INT" | 48 | data PrintDraftOptions = PrintDraftOptions |
| 43 | <> help "The number of the printer to use" | 49 | { printOptions :: PrintOptions |
| 44 | <> value 0 | 50 | , pDraftId :: Int64 |
| 45 | <> showDefault | 51 | , deleteAfter :: Bool |
| 46 | ) | 52 | } |
| 47 | <*> flag False True ( | 53 | |
| 48 | long "dry-run" | 54 | data QueryOptions = QueryOptions |
| 49 | <> short 'd' | 55 | |
| 50 | <> help "Instead of sending data to printer output the parsed stream to stderr" | 56 | data AddOptions = AddOptions |
| 51 | <> showDefault | 57 | { title :: String |
| 52 | ) | 58 | } |
| 53 | where | 59 | |
| 54 | baseUrlReader = str >>= either readerError return . parseBaseUrl | 60 | data GetOptions = GetOptions |
| 61 | { gDraftId :: Int64 | ||
| 62 | , getTitle :: Bool | ||
| 63 | } | ||
| 64 | |||
| 65 | data WriteOptions = WriteOptions | ||
| 66 | { wDraftId :: Int64 | ||
| 67 | , newTitle :: Maybe String | ||
| 68 | } | ||
| 69 | |||
| 70 | data DelOptions = DelOptions | ||
| 71 | { dDraftId :: Int64 | ||
| 72 | } | ||
| 73 | |||
| 55 | 74 | ||
| 56 | main :: IO () | 75 | main :: IO () |
| 57 | main = execParser opts >>= main' | 76 | main = do |
| 77 | envUrl <- lookupEnv "TPRINT" | ||
| 78 | let | ||
| 79 | defaultUrl = fromMaybe (BaseUrl Http "localhost" 8080) (envUrl >>= either (const Nothing) Just . parseBaseUrl) | ||
| 80 | execParser (opts defaultUrl) >>= main' | ||
| 58 | where | 81 | where |
| 59 | opts = info (helper <*> options) ( | 82 | opts url = info (helper <*> opts' url) ( |
| 60 | fullDesc | 83 | fullDesc |
| 61 | <> header "tprint - A cli tool for interfacing with the REST api as provided by thermoprint-servant" | 84 | <> header "tprint - A cli tool for interfacing with the REST api as provided by thermoprint-servant" |
| 62 | ) | 85 | ) |
| 86 | opts' url = TPrint | ||
| 87 | <$> modeSwitch | ||
| 88 | <*> commonOpts url | ||
| 89 | commonOpts url = TPrintOptions | ||
| 90 | <$> option baseUrlReader ( | ||
| 91 | long "url" | ||
| 92 | <> short 'u' | ||
| 93 | <> metavar "URL" | ||
| 94 | <> help "The base url of the api. Also reads TPRINT from environment." | ||
| 95 | <> value url | ||
| 96 | <> showDefaultWith showBaseUrl | ||
| 97 | ) | ||
| 98 | baseUrlReader = str >>= either readerError return . parseBaseUrl | ||
| 99 | modeSwitch = subparser $ mconcat $ map (\(n, f, h) -> command n $ info (helper <*> f) $ progDesc h) | ||
| 100 | [ ("print", print, "Read bbcode from stdin and send it to be printed") | ||
| 101 | , ("print-draft", printD, "Send a draft to be printed") | ||
| 102 | , ("query", query, "List drafts") | ||
| 103 | , ("add", add, "Read bbcode from stdin and add it as a draft") | ||
| 104 | , ("get", get, "Get a draft and print it as bbcode to stdout") | ||
| 105 | , ("write", write, "Read bbcode from stdin and overwrite an existing draft") | ||
| 106 | , ("del", del, "Delete a draft") | ||
| 107 | ] | ||
| 108 | draftN s = option auto ( | ||
| 109 | long "draft" | ||
| 110 | <> short 'n' | ||
| 111 | <> metavar "INT" | ||
| 112 | <> help s | ||
| 113 | ) | ||
| 114 | print = Print <$> print' | ||
| 115 | print' = PrintOptions | ||
| 116 | <$> option auto ( | ||
| 117 | long "printer" | ||
| 118 | <> short 'p' | ||
| 119 | <> metavar "INT" | ||
| 120 | <> help "The number of the printer to use" | ||
| 121 | <> value 0 | ||
| 122 | <> showDefault | ||
| 123 | ) | ||
| 124 | <*> flag False True ( | ||
| 125 | long "dry-run" | ||
| 126 | <> short 'd' | ||
| 127 | <> help "Instead of sending data to printer output the parsed stream to stderr" | ||
| 128 | <> showDefault | ||
| 129 | ) | ||
| 130 | printD = (PrintDraft <$>) $ PrintDraftOptions | ||
| 131 | <$> print' | ||
| 132 | <*> draftN "The number of the draft to print" | ||
| 133 | <*> flag False True ( | ||
| 134 | long "delete" | ||
| 135 | <> help "Delete the draft after printing" | ||
| 136 | ) | ||
| 137 | query = (Query <$>) $ pure QueryOptions | ||
| 138 | add = (Add <$>) $ AddOptions | ||
| 139 | <$> strArgument ( | ||
| 140 | metavar "TITLE" | ||
| 141 | <> help "The human readable title for the new draft" | ||
| 142 | ) | ||
| 143 | get = (Get <$>) $ GetOptions | ||
| 144 | <$> draftN "The number of the draft to retrieve" | ||
| 145 | <*> flag False True ( | ||
| 146 | long "title" | ||
| 147 | <> short 't' | ||
| 148 | <> help "Get title instead of content" | ||
| 149 | ) | ||
| 150 | write = (Write <$>) $ WriteOptions | ||
| 151 | <$> draftN "The number of the draft to overwrite" | ||
| 152 | <*> optional ( strArgument ( | ||
| 153 | metavar "TITLE" | ||
| 154 | <> help "The human readable title for the updated draft (defaults to retrieving the old one before overwriting)" | ||
| 155 | ) | ||
| 156 | ) | ||
| 157 | del = (Del <$>) $ DelOptions | ||
| 158 | <$> draftN "The number of the draft to delete" | ||
| 159 | |||
| 160 | either' :: (a -> String) -> EitherT a IO b -> IO b | ||
| 161 | either' f a = either (die . f) return =<< runEitherT a | ||
| 63 | 162 | ||
| 64 | main' Options{..} = do | 163 | main' (TPrint mode TPrintOptions{..}) = do |
| 65 | let | 164 | let |
| 66 | print :: Integer -> Block String -> EitherT ServantError IO () | 165 | -- print :: Integer -> Block String -> EitherT ServantError IO () |
| 67 | print = client thermoprintApi baseUrl | 166 | -- queryDrafts :: EitherT ServantError IO [(Integer, String)] |
| 167 | -- addDraft :: (String, Block String) -> EitherT ServantError IO Int64 | ||
| 168 | -- getDraft :: Int64 -> EitherT ServantError IO (String, Block String) | ||
| 169 | -- writeDraft :: Int64 -> (String, Block String) -> EitherT ServantError IO Int64 | ||
| 170 | -- delDraft :: Int64 -> EitherT ServantError IO () | ||
| 171 | (print :<|> queryDrafts :<|> addDraft :<|> getDraft :<|> writeDraft :<|> delDraft) = client thermoprintApi baseUrl | ||
| 172 | case mode of | ||
| 173 | Print PrintOptions{..} -> do | ||
| 68 | input <- BBCode.parse `liftM` getContents | 174 | input <- BBCode.parse `liftM` getContents |
| 69 | input' <- either (\err -> hPutStrLn stderr ("Parse error: " ++ err) >> exitFailure) return input | 175 | input' <- either (die . ("Parse error: " ++)) return input |
| 70 | case dryRun of | 176 | case dryRun of |
| 71 | False -> do | 177 | False -> do |
| 72 | res <- runEitherT $ print printerId input' | 178 | res <- runEitherT $ print printerId input' |
| @@ -75,3 +181,37 @@ main = execParser opts >>= main' | |||
| 75 | Right _ -> exitSuccess | 181 | Right _ -> exitSuccess |
| 76 | True -> do | 182 | True -> do |
| 77 | hPutStrLn stderr $ show input' | 183 | hPutStrLn stderr $ show input' |
| 184 | PrintDraft PrintDraftOptions{..} -> do | ||
| 185 | let PrintOptions{..} = printOptions | ||
| 186 | (_, input) <- either' (\e -> "Error while retrieving draft: " ++ show e) $ getDraft pDraftId | ||
| 187 | case dryRun of | ||
| 188 | False -> do | ||
| 189 | res <- runEitherT $ print printerId input | ||
| 190 | case res of | ||
| 191 | Left err -> hPutStrLn stderr $ show err | ||
| 192 | Right _ -> when deleteAfter $ either' (\e -> "Error while deleting draft: " ++ show e) $ delDraft pDraftId | ||
| 193 | True -> do | ||
| 194 | hPutStrLn stderr $ show input | ||
| 195 | Query QueryOptions -> do | ||
| 196 | drafts <- either' (\e -> "Error while retrieving drafts: " ++ show e) queryDrafts | ||
| 197 | mapM_ (\(n, t) -> putStrLn $ "[" ++ show n ++ "]\n" ++ (unlines $ map (\s -> " " ++ s) $ lines t)) drafts | ||
| 198 | when (null drafts) $ hPutStrLn stderr "No drafts" | ||
| 199 | Add AddOptions{..} -> do | ||
| 200 | input <- BBCode.parse `liftM` getContents | ||
| 201 | input' <- either (die . ("Parse error: " ++)) return input | ||
| 202 | n <- either' (\e -> "Error while saving draft: " ++ show e) $ addDraft (title, input') | ||
| 203 | IO.print n | ||
| 204 | Get GetOptions{..} -> do | ||
| 205 | (title, draft) <- either' (\e -> "Error while retrieving draft: " ++ show e) $ getDraft gDraftId | ||
| 206 | case getTitle of | ||
| 207 | False -> putStr $ BBCode.make draft | ||
| 208 | True -> putStrLn title | ||
| 209 | Write WriteOptions{..} -> do | ||
| 210 | input <- BBCode.parse `liftM` getContents | ||
| 211 | input' <- either (die . ("Parse error: " ++)) return input | ||
| 212 | title <- case newTitle of | ||
| 213 | Just new -> return new | ||
| 214 | Nothing -> fst <$> (either' (\e -> "Error while retrieving draft: " ++ show e) $ getDraft wDraftId) | ||
| 215 | either' (\e -> "Error while overwriting draft: " ++ show e) $ writeDraft wDraftId (title, input') | ||
| 216 | Del DelOptions{..} -> either' (\e -> "Error while deleting draft: " ++ show e) $ delDraft dDraftId | ||
| 217 | _ -> undefined | ||
