X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=harbl-cli%2Fsrc%2FHosts.hs;fp=harbl-cli%2Fsrc%2FHosts.hs;h=020484dff1dc1872fe6e742d61fd6c2121fcacfb;hb=b55e5db2a68be5d69b970bbe4b5ad447881abd3d;hp=0000000000000000000000000000000000000000;hpb=c4d41b93ec02ff4dc762163441ebefb0324e6f07;p=dead%2Fharbl.git diff --git a/harbl-cli/src/Hosts.hs b/harbl-cli/src/Hosts.hs new file mode 100644 index 0000000..020484d --- /dev/null +++ b/harbl-cli/src/Hosts.hs @@ -0,0 +1,47 @@ +-- | A newtype around a list of Strings which represent hosts to look up. +-- This is all to avoid an orphan instance of 'Configured' for +-- [String] if we had defined one in e.g. 'OptionalConfiguration'. +-- +{-# LANGUAGE DeriveDataTypeable #-} + +module Hosts ( Hosts(..) ) +where + +-- DC is needed only for the DCT.Configured instance of String. +import qualified Data.Configurator as DC() +import qualified Data.Configurator.Types as DCT ( + Configured, + Value( List ), + convert ) +import Data.Data ( Data ) +import System.Console.CmdArgs.Default ( Default(..) ) +import Data.Typeable ( Typeable ) + + +-- | A (wrapper around a) list of hosts. +-- +newtype Hosts = + Hosts { get_hosts :: [String] } + deriving (Data, Show, Typeable) + + +-- | The default list of hosts. It's empty. +-- +instance Default Hosts where def = Hosts [] + +instance DCT.Configured Hosts where + -- | This allows us to read a Hosts object out of a Configurator + -- config file. By default Configurator wouldn't know what to do, + -- so we have to tell it that we expect a list, and if that list + -- has strings in it, we can apply the Hosts constructor to + -- it. + convert (DCT.List xs) = + -- mapM gives us a Maybe [String] here. + fmap Hosts (mapM convert_string xs) + where + convert_string :: DCT.Value -> Maybe String + convert_string = DCT.convert + + -- If we read anything other than a list of values out of the file, + -- fail. + convert _ = Nothing