020484dff1dc1872fe6e742d61fd6c2121fcacfb
[dead/harbl.git] / harbl-cli / src / Hosts.hs
1 -- | A newtype around a list of Strings which represent hosts to look up.
2 -- This is all to avoid an orphan instance of 'Configured' for
3 -- [String] if we had defined one in e.g. 'OptionalConfiguration'.
4 --
5 {-# LANGUAGE DeriveDataTypeable #-}
6
7 module Hosts ( Hosts(..) )
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 Configured,
14 Value( List ),
15 convert )
16 import Data.Data ( Data )
17 import System.Console.CmdArgs.Default ( Default(..) )
18 import Data.Typeable ( Typeable )
19
20
21 -- | A (wrapper around a) list of hosts.
22 --
23 newtype Hosts =
24 Hosts { get_hosts :: [String] }
25 deriving (Data, Show, Typeable)
26
27
28 -- | The default list of hosts. It's empty.
29 --
30 instance Default Hosts where def = Hosts []
31
32 instance DCT.Configured Hosts where
33 -- | This allows us to read a Hosts object out of a Configurator
34 -- config file. By default Configurator wouldn't know what to do,
35 -- so we have to tell it that we expect a list, and if that list
36 -- has strings in it, we can apply the Hosts constructor to
37 -- it.
38 convert (DCT.List xs) =
39 -- mapM gives us a Maybe [String] here.
40 fmap Hosts (mapM convert_string xs)
41 where
42 convert_string :: DCT.Value -> Maybe String
43 convert_string = DCT.convert
44
45 -- If we read anything other than a list of values out of the file,
46 -- fail.
47 convert _ = Nothing