diff options
author | Andrew Cady <d@jerkface.net> | 2018-03-10 11:39:05 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2018-03-10 11:39:05 -0500 |
commit | e4ad9a4b5512ed3a7779ae848f2c95df24ee46b8 (patch) | |
tree | 6890e698b3e613cf9f23489c15ffa306db85b002 | |
parent | d0d5ec92d912a286d51ea4df2c95f7cc5fd44bb1 (diff) |
add client & server modes
-rw-r--r-- | Main.hs | 59 | ||||
-rw-r--r-- | package.yaml | 1 |
2 files changed, 45 insertions, 15 deletions
@@ -1,39 +1,68 @@ | |||
1 | {-# LANGUAGE NoImplicitPrelude #-} | 1 | {-# LANGUAGE NoImplicitPrelude #-} |
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | module Main where | 2 | module Main where |
4 | |||
5 | import Rebase.Prelude | 3 | import Rebase.Prelude |
4 | |||
5 | import Options.Applicative (execParser, help, helper, info, long, | ||
6 | metavar, strOption) | ||
6 | import System.Directory (createDirectoryIfMissing, renameFile) | 7 | import System.Directory (createDirectoryIfMissing, renameFile) |
7 | import System.FilePath (takeFileName, (</>)) | 8 | import System.FilePath (takeFileName, (</>)) |
8 | import System.FSNotify (Event (..), watchDir, withManager) | 9 | import System.FSNotify (Event (..), watchDir, withManager) |
9 | import System.Process.Typed (proc, runProcess) | 10 | import System.Process.Typed (proc, runProcess) |
10 | 11 | ||
11 | verbose :: Bool | ||
12 | verbose = True | ||
13 | |||
14 | pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath | 12 | pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath |
15 | pdfDirectory = "." | 13 | pdfDirectory = "." |
16 | seenDir = pdfDirectory </> "seen" | 14 | seenDir = pdfDirectory </> "seen" |
17 | pdfPrinterExecutable = "PDFtoPrinter.exe" | 15 | pdfPrinterExecutable = "PDFtoPrinter.exe" |
18 | 16 | ||
17 | verbose :: Bool | ||
18 | verbose = True | ||
19 | |||
20 | data Options = Options | ||
21 | { sendTo :: Maybe String | ||
22 | } | ||
23 | |||
19 | main :: IO () | 24 | main :: IO () |
20 | main = do | 25 | main = execParser (info (options <**> helper) mempty) >>= chooseMain |
26 | where | ||
27 | options = fmap Options $ optional $ strOption $ long "send-to" | ||
28 | <> metavar "RSYNC-DEST" | ||
29 | <> help "Where to send the PDFs via rsync, in rsync target format (host:path)" | ||
30 | |||
31 | chooseMain :: Options -> IO () | ||
32 | chooseMain (Options Nothing) = serverMain | ||
33 | chooseMain (Options (Just target)) = clientMain target | ||
34 | |||
35 | serverMain :: IO () | ||
36 | serverMain = do | ||
21 | createDirectoryIfMissing False seenDir | 37 | createDirectoryIfMissing False seenDir |
22 | withManager $ \mgr -> do | 38 | handlePdfsForever pdfDirectory pdfPrinter |
23 | void $ watchDir mgr pdfDirectory (const True) handleEvent | ||
24 | forever $ threadDelay 1000000 | ||
25 | 39 | ||
26 | handleEvent :: Event -> IO () | 40 | clientMain :: String -> IO () |
27 | handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f | 41 | clientMain target = handlePdfsForever pdfDirectory (pdfSender target) |
28 | handleEvent x = when verbose $ print x | ||
29 | 42 | ||
30 | handleNewPdf :: FilePath -> IO () | 43 | pdfPrinter :: FilePath -> IO () |
31 | handleNewPdf f = | 44 | pdfPrinter f = |
32 | -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe | 45 | -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe |
33 | -- returns success even when it fails to parse the PDF. | 46 | -- returns success even when it fails to parse the PDF. |
34 | runProcessVerbose pdfPrinterExecutable [f] >> | 47 | runProcessVerbose pdfPrinterExecutable [f] >> |
35 | moveFileIntoDir f seenDir | 48 | moveFileIntoDir f seenDir |
36 | 49 | ||
50 | pdfSender :: String -> FilePath -> IO () | ||
51 | pdfSender target f = | ||
52 | runProcessVerbose "rsync" ["--remove-source-files", f, target] >> return () | ||
53 | |||
54 | handlePdfsForever :: FilePath -> (FilePath -> IO ()) -> IO () | ||
55 | handlePdfsForever dir h = handleEventsForever dir (handleAdds (".pdf" `isSuffixOf`) h) | ||
56 | |||
57 | handleAdds :: (String -> Bool) -> (FilePath -> IO ()) -> Event -> IO () | ||
58 | handleAdds predicate handleFile (Added f _) | predicate f = handleFile f | ||
59 | handleAdds _ _ x = when verbose $ print x | ||
60 | |||
61 | handleEventsForever :: FilePath -> (Event -> IO ()) -> IO () | ||
62 | handleEventsForever dir eventHandler = do | ||
63 | withManager $ \mgr -> do | ||
64 | void $ watchDir mgr dir (const True) eventHandler | ||
65 | forever $ threadDelay 1000000 | ||
37 | 66 | ||
38 | runProcessVerbose :: FilePath -> [String] -> IO ExitCode | 67 | runProcessVerbose :: FilePath -> [String] -> IO ExitCode |
39 | runProcessVerbose exe args = do | 68 | runProcessVerbose exe args = do |
diff --git a/package.yaml b/package.yaml index 435c2f7..3dfc2f3 100644 --- a/package.yaml +++ b/package.yaml | |||
@@ -12,6 +12,7 @@ dependencies: | |||
12 | - fsnotify | 12 | - fsnotify |
13 | - typed-process | 13 | - typed-process |
14 | - filepath | 14 | - filepath |
15 | - optparse-applicative | ||
15 | 16 | ||
16 | executables: | 17 | executables: |
17 | pdf-autoprint: | 18 | pdf-autoprint: |