Remove.hs: implement Remove with id
[hdata.git] / src / Remove.hs
blobd6e7e2824e7470f3d98726b9079bd2656f321e95
1 {-
2 Remove.hs
4 Copyright 2013 Louis-Guillaume Gagnon <louis.guillaume.gagnon@gmail.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 module Remove (
21 remove,
22 usageRemove
23 ) where
25 import Data.Char (isDigit)
26 import System.IO (hFlush,stdout)
28 import Tools.Constants
29 import Tools.Filter (rowToString)
30 import Tools.Operation (isHelp)
31 import Tools.SQL (getEntry, removeEntry)
33 remove :: [String] -> IO ()
34 remove [] = error $ "remove: too few arguments ('" ++ progName ++
35 " remove help') for help"
36 remove argv | length argv > 1 = error $ "remove: too many arguments ('" ++
37 progName ++ " remove help') for help"
38 | isHelp (argv!!0) = putStrLn usageRemove
39 | or (map (not . isDigit) id) = error $ "remove: invalid id: " ++ id
40 | otherwise = askRemove id
41 where id = argv!!0
43 askRemove :: String -> IO ()
44 askRemove id = do
45 entry <- getEntry $ read id
46 case entry of
47 Left msg -> error $ "remove: " ++ msg
48 Right row -> do putStrLn $ rowToString row
49 putStr "Remove this entry? [y/n] "
50 hFlush stdout
51 answer <- getLine
52 case answer of
53 "y" -> removeEntry $ read id
54 _ -> return ()
56 usageRemove :: String
57 usageRemove = "usage: " ++ progName ++ " remove <id>"