-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.hs
93 lines (80 loc) · 3.17 KB
/
Config.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{-# LANGUAGE CPP #-}
{-
Copyright (C) 2009 John MacFarlane <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}
{- Functions for parsing command line options and reading the config file.
-}
module Gitit.Config ( getConfigFromOpts )
where
import Gitit.State (Config(..), defaultConfig)
import System.Environment
import System.Exit
import System.IO (stderr, hPutStrLn)
import System.Console.GetOpt
import Control.Monad (liftM, foldM)
data Opt
= Help
| ConfigFile FilePath
| Port Int
| Debug
| Version
| PrintDefaultConfig
deriving (Eq)
flags :: [OptDescr Opt]
flags =
[ Option ['h'] ["help"] (NoArg Help)
"Print this help message"
, Option ['v'] ["version"] (NoArg Version)
"Print version information"
, Option [] ["print-default-config"] (NoArg PrintDefaultConfig)
"Print default configuration"
, Option ['p'] ["port"] (ReqArg (Port . read) "PORT")
"Specify port"
, Option ['d'] ["debug"] (NoArg Debug)
"Print debugging information on each request"
, Option ['f'] ["config-file"] (ReqArg ConfigFile "FILE")
"Specify configuration file"
]
parseArgs :: [String] -> IO [Opt]
parseArgs argv = do
progname <- getProgName
case getOpt Permute flags argv of
(opts,_,[]) -> return opts
(_,_,errs) -> hPutStrLn stderr (concat errs ++ usageInfo (usageHeader progname) flags) >>
exitWith (ExitFailure 1)
usageHeader :: String -> String
usageHeader progname = "Usage: " ++ progname ++ " [opts...]"
copyrightMessage :: String
copyrightMessage = "\nCopyright (C) 2008 John MacFarlane\n" ++
"This is free software; see the source for copying conditions. There is no\n" ++
"warranty, not even for merchantability or fitness for a particular purpose."
compileInfo :: String
compileInfo =
#ifdef _PLUGINS
" +plugins"
#else
" -plugins"
#endif
handleFlag :: Config -> Opt -> IO Config
handleFlag conf opt = do
progname <- getProgName
case opt of
Help -> hPutStrLn stderr (usageInfo (usageHeader progname) flags) >> exitWith ExitSuccess
Version -> hPutStrLn stderr (progname ++ " version " ++ _VERSION ++ compileInfo ++ copyrightMessage) >> exitWith ExitSuccess
PrintDefaultConfig -> print conf >> exitWith ExitSuccess
Debug -> return $ conf { debugMode = True }
Port p -> return $ conf { portNumber = p }
ConfigFile f -> liftM read (readFile f)
getConfigFromOpts :: IO Config
getConfigFromOpts = getArgs >>= parseArgs >>= foldM handleFlag defaultConfig