error out if the input string is not a valid filepath
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 27 Mar 2015 05:50:41 +0000 (11:20 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 27 Mar 2015 05:50:41 +0000 (11:20 +0530)
functorrent.cabal
src/Main.hs

index fc4cfb35bb18cc4a28bfb98f4255c952b66776c4..b56110ba1d3331d8aeb7eed06dade562db9a0bc0 100644 (file)
@@ -52,7 +52,8 @@ executable functorrent
                        cryptohash,
                        HTTP,
                        network-uri,
-                       parsec
+                       parsec,
+                       directory
 
 test-suite functorrent-test
   type:              exitcode-stdio-1.0
index 17bd05355c8032608409f1b8a26026a8062e17d1..d0812656c5498d30166c6b762c8ba6038a15f77c 100644 (file)
@@ -5,6 +5,7 @@ import Prelude hiding (length, readFile, writeFile)
 import Data.ByteString.Char8 (ByteString, readFile, writeFile, length)
 import System.Environment (getArgs)
 import System.Exit (exitSuccess)
+import System.Directory (doesFileExist)
 import Text.ParserCombinators.Parsec (ParseError)
 
 import FuncTorrent.Bencode (decode, BVal(..))
@@ -27,7 +28,11 @@ usage = putStrLn "usage: functorrent torrent-file"
 
 parse :: [String] -> IO ByteString
 parse [] = usage >> exit
-parse [a] = readFile a
+parse [a] = do
+  fileExist <- doesFileExist a
+  if fileExist
+    then readFile a
+    else error "file does not exist"
 parse _ = exit
 
 main :: IO ()