]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Usernames.hs
Add configuration options for daemonize, pidfile, run_as_group and run_as_user.
[dead/halcyon.git] / src / Usernames.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2
3 -- | A newtype around a list of Strings which represent the usernames
4 -- to watch. This is all to avoid an orphan instance of Configured
5 -- for [String] if we had defined one in e.g. OptionalConfiguration.
6 --
7 module Usernames ( Usernames(..) )
8 where
9
10 -- DC is needed only for the DCT.Configured instance of String.
11 import qualified Data.Configurator as DC()
12 import qualified Data.Configurator.Types as DCT
13 import Data.Data ( Data )
14 import Data.Monoid ( Monoid(..) )
15 import Data.Typeable ( Typeable )
16 import System.Console.CmdArgs.Default ( Default(..) )
17
18
19 -- | Wrapper around a list of strings (usernames).
20 --
21 newtype Usernames =
22 Usernames { get_usernames :: [String] }
23 deriving (Data, Show, Typeable)
24
25
26 instance Default Usernames where
27 -- | The default list of usernames is empty.
28 --
29 def = Usernames []
30
31
32 -- | The 'Monoid' instance for 'Usernames' uses an
33 -- 'Monoid' instance for lists.
34 --
35 instance Monoid Usernames where
36 -- | The \"empty\" 'Usernames' simply wraps an empty list.
37 mempty = Usernames []
38
39 -- | This mappend is a little funny; it always chooses the second
40 -- list if that list is nonempty. Otherwise, it chooses the
41 -- first. This is actually associative!
42 u1 `mappend` u2
43 | null (get_usernames u2) = u1
44 | otherwise = u2
45
46
47 instance DCT.Configured Usernames where
48 -- | This allows us to read a 'Usernames' object out of a
49 -- Configurator config file. By default Configurator wouldn't know
50 -- what to do, so we have to tell it that we expect a list, and if
51 -- that list has strings in it, we can apply the Usernames
52 -- constructor to it.
53 convert (DCT.List xs) =
54 fmap Usernames (mapM convert_string xs)
55 where
56 convert_string :: DCT.Value -> Maybe String
57 convert_string = DCT.convert
58
59 convert _ = Nothing