imported fquery-0.2
[fquery.git] / Main.hs
blobf5f04a80996f2c5fda2c31466d8afa50b39b8647
1 -- Main.hs
2 --
3 -- Adelie is a collection of scripts to querying portage packages.
5 module Main (main) where
7 import System (getArgs, getProgName)
9 import Adelie.Colour
10 import Adelie.Options
11 import Adelie.QChangelog
12 import Adelie.QCheck
13 import Adelie.QDepend
14 import Adelie.QHasUse
15 import Adelie.QList
16 import Adelie.QOwn
17 import Adelie.QSize
18 import Adelie.QUse
19 import Adelie.QWant
21 type CommandProc = [String] -> IO ()
23 data Command
24 = Short String String CommandProc
25 | Long String String String CommandProc
27 logCommands = [
28 (Long "c" "changes"
29 "list changes since the installed version"
30 qChangelog),
31 (Short "cl"
32 "find the changelog of a package"
33 qLogFile)
36 listCommands = [
37 (Long "f" "files"
38 "list the contents of a package"
39 (qList ListAll)),
40 (Short "fd"
41 "list the directories in a package"
42 (qList ListDirs)),
43 (Short "ff"
44 "list the files in a package"
45 (qList ListFiles)),
46 (Short "fl"
47 "list the links in a package"
48 (qList ListLinks))
51 ownCommands = [
52 (Long "b" "belongs"
53 "find the package(s) owning a file"
54 qOwn),
55 (Short "bp"
56 "find the package(s) owning a file with regexp"
57 qOwnRegex),
58 (Long "s" "size"
59 "find the size of files in a package"
60 qSize),
61 (Long "k" "check"
62 "check MD5sums and timestamps of a package"
63 qCheck)
66 dependCommands = [
67 (Long "d" "depends"
68 "list packages directly depending on this package"
69 qDepend),
70 (Short "dd"
71 "list direct dependencies of a package"
72 qWant)
75 useCommands = [
76 (Long "u" "uses"
77 "describe a package's USE flags"
78 qUse),
79 (Long "h" "hasuse"
80 "list all packages with a USE flag"
81 qHasUse)
84 allCommands = logCommands ++ listCommands ++ ownCommands ++ dependCommands ++ useCommands
86 ----------------------------------------------------------------
88 main :: IO ()
89 main = do
90 args0 <- getArgs
91 let (options, commands) = span isOption args0
92 mapM_ parseOptions options
93 case commands of
94 [] -> usage
95 (cmd:cargs) -> (runCommand cmd allCommands) cargs
98 isOption :: String -> Bool
99 isOption = ('-' ==) . head
101 ----------------------------------------------------------------
103 parseOptions :: String -> IO ()
104 parseOptions [] = return ()
106 parseOptions "-C" = setColourEnabled False
107 parseOptions "--nocolor" = setColourEnabled False
108 parseOptions "--nocolour" = setColourEnabled False
110 parseOptions _ = return ()
112 ----------------------------------------------------------------
114 runCommand :: String -> [Command] -> CommandProc
115 runCommand _ [] = (\ _ -> usage)
117 runCommand command ((Short cmd _ f):cs)
118 | command == cmd = f
119 | otherwise = runCommand command cs
121 runCommand command ((Long cmd0 cmd1 _ f):cs)
122 | command == cmd0 = f
123 | command == cmd1 = f
124 | otherwise = runCommand command cs
126 ----------------------------------------------------------------
128 usage :: IO ()
129 usage = do
130 prog <- getProgName
131 putStrLn "fquery 0.2\n"
132 putStrLn $ "Usage: " ++ prog ++ " [options] <command> <arguments>\n"
134 cyan >> putStr "Options:" >> off2
135 inYellow (putStr " -C --nocolour") >> tab >> putStrLn "turn off colours"
138 cyan >> putStr "Commands for Installed Packages:" >> off2
139 mapM_ putCommand logCommands; nl
140 mapM_ putCommand listCommands; nl
141 mapM_ putCommand ownCommands; nl
142 mapM_ putCommand dependCommands; nl
143 mapM_ putCommand useCommands; nl
145 putCommand :: Command -> IO ()
146 putCommand (Short cmd desc _) = f `withDesc` desc
147 where f = green >> putStr cmd >> off >> tab
149 putCommand (Long cmd0 cmd1 desc _) = f `withDesc` desc
150 where f = green >> putStr (cmd0 ++ " " ++ cmd1) >> off
152 withDesc :: IO () -> String -> IO ()
153 f `withDesc` desc = putStr " " >> f >> tab >> putStrLn desc
155 tab = putChar '\t'
156 nl = putChar '\n'