diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 00:15:54 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 00:15:54 +0400 |
commit | e611467a3bd8b46367fed030a9286031faaa547d (patch) | |
tree | 858873a33b786f736c9d9a70c9555a7205e9a32a /examples/MkTorrent.hs | |
parent | 980b65c6cbe5d02ab6479ffb76783178ace0575d (diff) |
Merge command options with command flags
Diffstat (limited to 'examples/MkTorrent.hs')
-rw-r--r-- | examples/MkTorrent.hs | 85 |
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 | |||
27 | import Data.Torrent.Magnet hiding (Magnet (Magnet)) | 27 | import Data.Torrent.Magnet hiding (Magnet (Magnet)) |
28 | import Data.Torrent.Magnet (Magnet) | 28 | import 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 | ||
115 | data AmendOpts = AmendOpts FilePath | 113 | data AmendOpts = AmendOpts FilePath |
116 | deriving Show | 114 | deriving Show |
117 | 115 | ||
118 | amendOpts :: Parser AmendOpts | ||
119 | amendOpts = AmendOpts <$> torrentFile | ||
120 | |||
121 | amendInfo :: ParserInfo AmendOpts | 116 | amendInfo :: ParserInfo AmendOpts |
122 | amendInfo = info (helper <*> amendOpts) modifier | 117 | amendInfo = 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 | ||
126 | type Amend = Torrent -> Torrent | 122 | type 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 | ||
209 | data MagnetFlags = MagnetFlags | 205 | data 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 | ||
213 | data MagnetOpts = MagnetOpts FilePath MagnetFlags | ||
214 | deriving Show | ||
215 | |||
216 | magnetFlags :: Parser MagnetFlags | ||
217 | magnetFlags = MagnetFlags | ||
218 | <$> switch | ||
219 | ( long "detailed" | ||
220 | ) | ||
221 | |||
222 | magnetOpts :: Parser MagnetOpts | ||
223 | magnetOpts = MagnetOpts <$> torrentFile <*> magnetFlags | ||
224 | |||
225 | magnetInfo :: ParserInfo MagnetOpts | 210 | magnetInfo :: ParserInfo MagnetOpts |
226 | magnetInfo = info (helper <*> magnetOpts) modifier | 211 | magnetInfo = info (helper <*> parser) modifier |
227 | where | 212 | where |
228 | modifier = progDesc "Print magnet link" | 213 | modifier = progDesc "Print magnet link" |
229 | 214 | parser = MagnetOpts | |
230 | mkMagnet :: MagnetFlags -> Torrent -> Magnet | 215 | <$> torrentFile |
231 | mkMagnet MagnetFlags {..} = if detailed then detailedMagnet else simpleMagnet | 216 | <*> switch ( long "detailed" ) |
232 | 217 | ||
233 | magnet :: MagnetOpts -> IO Bool | 218 | magnet :: MagnetOpts -> IO Bool |
234 | magnet (MagnetOpts tpath flags) = do | 219 | magnet 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 | ||
242 | data ShowFlags = ShowFlags | 229 | data 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 | ||
246 | data ShowOpts = ShowOpts FilePath ShowFlags | ||
247 | deriving Show | ||
248 | |||
249 | showFlags :: Parser ShowFlags | ||
250 | showFlags = ShowFlags | ||
251 | <$> switch | ||
252 | ( long "infohash" | ||
253 | <> help "Show only hash of the torrent info part" | ||
254 | ) | ||
255 | |||
256 | showOpts :: Parser ShowOpts | ||
257 | showOpts = ShowOpts <$> torrentFile <*> showFlags | ||
258 | |||
259 | showInfo :: ParserInfo ShowOpts | 234 | showInfo :: ParserInfo ShowOpts |
260 | showInfo = info (helper <*> showOpts) modifier | 235 | showInfo = 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 | |
264 | showTorrent :: ShowFlags -> Torrent -> ShowS | 239 | <$> torrentFile |
265 | showTorrent ShowFlags {..} torrent | 240 | <*> switch |
241 | ( long "infohash" | ||
242 | <> help "Show only hash of the torrent info part" | ||
243 | ) | ||
244 | |||
245 | showTorrent :: ShowOpts -> Torrent -> ShowS | ||
246 | showTorrent 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 | ||
269 | putTorrent :: ShowOpts -> IO Bool | 250 | putTorrent :: ShowOpts -> IO Bool |
270 | putTorrent (ShowOpts torrentPath flags) = do | 251 | putTorrent 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" |