summaryrefslogtreecommitdiff
path: root/examples/MkTorrent.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-05 00:15:54 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 00:15:54 +0400
commite611467a3bd8b46367fed030a9286031faaa547d (patch)
tree858873a33b786f736c9d9a70c9555a7205e9a32a /examples/MkTorrent.hs
parent980b65c6cbe5d02ab6479ffb76783178ace0575d (diff)
Merge command options with command flags
Diffstat (limited to 'examples/MkTorrent.hs')
-rw-r--r--examples/MkTorrent.hs85
1 files changed, 33 insertions, 52 deletions
diff --git a/examples/MkTorrent.hs b/examples/MkTorrent.hs
index ca7f5942..fea7ada5 100644
--- a/examples/MkTorrent.hs
+++ b/examples/MkTorrent.hs
@@ -27,8 +27,6 @@ import Data.Torrent
27import Data.Torrent.Magnet hiding (Magnet (Magnet)) 27import Data.Torrent.Magnet hiding (Magnet (Magnet))
28import Data.Torrent.Magnet (Magnet) 28import Data.Torrent.Magnet (Magnet)
29 29
30--import MkTorrent.Check
31--import MkTorrent.Create
32 30
33{----------------------------------------------------------------------- 31{-----------------------------------------------------------------------
34-- Dialogs 32-- Dialogs
@@ -109,19 +107,17 @@ torrentFile = argument Just
109 ) 107 )
110 108
111{----------------------------------------------------------------------- 109{-----------------------------------------------------------------------
112-- Amend command 110-- Amend command - edit a field of torrent file
113-----------------------------------------------------------------------} 111-----------------------------------------------------------------------}
114 112
115data AmendOpts = AmendOpts FilePath 113data AmendOpts = AmendOpts FilePath
116 deriving Show 114 deriving Show
117 115
118amendOpts :: Parser AmendOpts
119amendOpts = AmendOpts <$> torrentFile
120
121amendInfo :: ParserInfo AmendOpts 116amendInfo :: ParserInfo AmendOpts
122amendInfo = info (helper <*> amendOpts) modifier 117amendInfo = info (helper <*> parser) modifier
123 where 118 where
124 modifier = progDesc "Edit info fields of existing torrent" 119 modifier = progDesc "Edit info fields of existing torrent"
120 parser = AmendOpts <$> torrentFile
125 121
126type Amend = Torrent -> Torrent 122type Amend = Torrent -> Torrent
127 123
@@ -203,73 +199,58 @@ createInfo = info (helper <*> createOpts) modifier
203-} 199-}
204 200
205{----------------------------------------------------------------------- 201{-----------------------------------------------------------------------
206-- Magnet command 202-- Magnet command -- print magnet link for given torrent file
207-----------------------------------------------------------------------} 203-----------------------------------------------------------------------}
208 204
209data MagnetFlags = MagnetFlags 205data MagnetOpts = MagnetOpts
210 { detailed :: Bool 206 { magnetFile :: FilePath -- ^ path to torrent file
207 , detailed :: Bool -- ^ whether to append additional uri params
211 } deriving Show 208 } deriving Show
212 209
213data MagnetOpts = MagnetOpts FilePath MagnetFlags
214 deriving Show
215
216magnetFlags :: Parser MagnetFlags
217magnetFlags = MagnetFlags
218 <$> switch
219 ( long "detailed"
220 )
221
222magnetOpts :: Parser MagnetOpts
223magnetOpts = MagnetOpts <$> torrentFile <*> magnetFlags
224
225magnetInfo :: ParserInfo MagnetOpts 210magnetInfo :: ParserInfo MagnetOpts
226magnetInfo = info (helper <*> magnetOpts) modifier 211magnetInfo = info (helper <*> parser) modifier
227 where 212 where
228 modifier = progDesc "Print magnet link" 213 modifier = progDesc "Print magnet link"
229 214 parser = MagnetOpts
230mkMagnet :: MagnetFlags -> Torrent -> Magnet 215 <$> torrentFile
231mkMagnet MagnetFlags {..} = if detailed then detailedMagnet else simpleMagnet 216 <*> switch ( long "detailed" )
232 217
233magnet :: MagnetOpts -> IO Bool 218magnet :: MagnetOpts -> IO Bool
234magnet (MagnetOpts tpath flags) = do 219magnet MagnetOpts {..} = do
235 print . mkMagnet flags =<< fromFile tpath 220 print . magnetLink =<< fromFile magnetFile;
236 return True 221 return True
222 where
223 magnetLink = if detailed then detailedMagnet else simpleMagnet
237 224
238{----------------------------------------------------------------------- 225{-----------------------------------------------------------------------
239-- Show command 226-- Show command - print torrent file information
240-----------------------------------------------------------------------} 227-----------------------------------------------------------------------}
241 228
242data ShowFlags = ShowFlags 229data ShowOpts = ShowOpts
243 { infoHashOnly :: Bool 230 { showPath :: FilePath -- ^ torrent file to inspect;
231 , infoHashOnly :: Bool -- ^ omit everything except infohash.
244 } deriving Show 232 } deriving Show
245 233
246data ShowOpts = ShowOpts FilePath ShowFlags
247 deriving Show
248
249showFlags :: Parser ShowFlags
250showFlags = ShowFlags
251 <$> switch
252 ( long "infohash"
253 <> help "Show only hash of the torrent info part"
254 )
255
256showOpts :: Parser ShowOpts
257showOpts = ShowOpts <$> torrentFile <*> showFlags
258
259showInfo :: ParserInfo ShowOpts 234showInfo :: ParserInfo ShowOpts
260showInfo = info (helper <*> showOpts) modifier 235showInfo = info (helper <*> parser) modifier
261 where 236 where
262 modifier = progDesc "Print .torrent file metadata" 237 modifier = progDesc "Print .torrent file metadata"
263 238 parser = ShowOpts
264showTorrent :: ShowFlags -> Torrent -> ShowS 239 <$> torrentFile
265showTorrent ShowFlags {..} torrent 240 <*> switch
241 ( long "infohash"
242 <> help "Show only hash of the torrent info part"
243 )
244
245showTorrent :: ShowOpts -> Torrent -> ShowS
246showTorrent ShowOpts {..} torrent
266 | infoHashOnly = shows $ idInfoHash (tInfoDict torrent) 247 | infoHashOnly = shows $ idInfoHash (tInfoDict torrent)
267 | otherwise = shows $ pretty torrent 248 | otherwise = shows $ pretty torrent
268 249
269putTorrent :: ShowOpts -> IO Bool 250putTorrent :: ShowOpts -> IO Bool
270putTorrent (ShowOpts torrentPath flags) = do 251putTorrent opts @ ShowOpts {..} = do
271 torrent <- fromFile torrentPath `onException` putStrLn help 252 torrent <- fromFile showPath `onException` putStrLn help
272 putStrLn $ showTorrent flags torrent [] 253 putStrLn $ showTorrent opts torrent []
273 return True 254 return True
274 where 255 where
275 help = "Most likely this is not a valid .torrent file" 256 help = "Most likely this is not a valid .torrent file"