]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
error out if the input string is not a valid filepath
[functorrent.git] / src / Main.hs
index bc4b8d31013948dbfd35c053c5fb18b3d623b2f2..d0812656c5498d30166c6b762c8ba6038a15f77c 100644 (file)
@@ -2,16 +2,17 @@
 module Main where
 
 import Prelude hiding (length, readFile, writeFile)
-import Data.ByteString.Char8 (ByteString, length, readFile, writeFile, length)
+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 Bencode (decode, BVal(..))
-import Logger (initLogger, logMessage, logStop)
-import Metainfo (announce, lengthInBytes, mkMetaInfo, info, name)
-import Peer (getPeers, getPeerResponse, handShakeMsg)
-import Tracker (connect, prepareRequest)
+import FuncTorrent.Bencode (decode, BVal(..))
+import FuncTorrent.Logger (initLogger, logMessage, logStop)
+import FuncTorrent.Metainfo (announce, lengthInBytes, mkMetaInfo, info, name)
+import FuncTorrent.Peer (peers, getPeerResponse, handShakeMsg)
+import FuncTorrent.Tracker (connect, prepareRequest)
 
 logError :: ParseError -> (String -> IO ()) -> IO ()
 logError e logMsg = logMsg $ "parse error: \n" ++ show e
@@ -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 ()
@@ -53,7 +58,7 @@ main = do
               -- TODO: Write to ~/.functorrent/caches
               writeFile (name (info m) ++ ".cache") body
 
-              let peerResponse = show $ getPeers $ getPeerResponse body
+              let peerResponse = show $ peers $ getPeerResponse body
               logMsg $ "Peers List : " ++ peerResponse
 
               let hsMsgLen = show $ length $ handShakeMsg d' peerId