aboutsummaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2015-11-01 09:56:07 +0300
committerIgor Pashev <pashev.igor@gmail.com>2015-11-01 09:56:07 +0300
commitaf7c57b627c6b83e3d342d9e6c4f95b6041612d8 (patch)
treef3244b1b213a1bba586e0991a5194ff4673c22b3 /cli
downloadzerobin-af7c57b627c6b83e3d342d9e6c4f95b6041612d8.tar.gz
Initial commit1.0.0
Diffstat (limited to 'cli')
-rw-r--r--cli/Main.hs76
1 files changed, 76 insertions, 0 deletions
diff --git a/cli/Main.hs b/cli/Main.hs
new file mode 100644
index 0000000..e32a78f
--- /dev/null
+++ b/cli/Main.hs
@@ -0,0 +1,76 @@
+{-# LANGUAGE QuasiQuotes #-}
+
+module Main where
+
+import Data.Version (showVersion)
+import Paths_zerobin (version) -- from cabal
+import System.Environment (getArgs)
+import System.Exit (exitFailure)
+import System.IO (stderr, hPutStrLn)
+import Text.RawString.QQ (r)
+import ZeroBin (share, Expiration(..), pasteEc)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Char8 as C
+import qualified System.Console.Docopt.NoTH as O
+
+usage :: String
+usage = "zerobin " ++ showVersion version
+ ++ " pastes to " ++ pasteEc ++ [r|
+zerobin prints URI to be shared or error message
+
+Usage:
+ zerobin [options] TEXT
+
+Options:
+ -e, --expire=E Set expiration of paste: once, day, week, month [default: week]
+ -f, --file Paste the content of file TEXT instead of plain TEXT
+
+ -h, --help Show this message
+
+Examples:
+ zerobin hello paste "hello" for a week
+ zerobin -f /etc/fstab paste file /etc/fstab for a week
+ zerobin -e once hello paste "hello", it will burn after reading
+|]
+
+
+getExpiration :: String -> Maybe Expiration
+getExpiration e =
+ case e of
+ "once" -> Just Once
+ "day" -> Just Day
+ "week" -> Just Week
+ "month" -> Just Month
+ _ -> Nothing
+
+die :: String -> IO ()
+die msg = do
+ hPutStrLn stderr $ "zerobin: " ++ msg
+ exitFailure
+
+getContent :: Bool -> String -> IO BS.ByteString
+getContent asFile text =
+ if not asFile
+ then return $ C.pack text
+ else BS.readFile text
+
+
+main :: IO ()
+main = do
+ doco <- O.parseUsageOrExit usage
+ args <- O.parseArgsOrExit doco =<< getArgs
+ if args `O.isPresent` O.longOption "help"
+ then putStrLn $ O.usage doco
+ else do
+ let get = O.getArgOrExitWith doco
+ expire <- args `get` O.longOption "expire"
+ text <- args `get` O.argument "TEXT"
+ cnt <- getContent (args `O.isPresent` O.longOption "file") text
+ case getExpiration expire of
+ Nothing -> die "invalid value for expiration"
+ Just e -> do
+ rc <- share e cnt
+ case rc of
+ Left err -> die err
+ Right uri -> putStrLn uri
+