From 50cd59cb3e6d20afe48a50fa9dc0c3a9cf9d9960 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 10 Apr 2014 21:09:20 -0400 Subject: new more expressive config.hs WIP --- Propellor/Property.hs | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'Propellor/Property.hs') diff --git a/Propellor/Property.hs b/Propellor/Property.hs index 7af69ea8..ccc060ff 100644 --- a/Propellor/Property.hs +++ b/Propellor/Property.hs @@ -9,6 +9,8 @@ import Control.Monad.IfElse import "mtl" Control.Monad.Reader import Propellor.Types +import Propellor.Types.Attr +import Propellor.Attr import Propellor.Engine import Utility.Monad @@ -94,17 +96,46 @@ boolProperty desc a = Property desc $ ifM (liftIO a) revert :: RevertableProperty -> RevertableProperty revert (RevertableProperty p1 p2) = RevertableProperty p2 p1 --- | Starts a list of Properties -props :: [Property] -props = [] +-- | Starts accumulating the properties of a Host. +-- +-- > host "example.com" +-- > & someproperty +-- > ! oldproperty +-- > & otherproperty +host :: HostName -> Host +host hn = Host [] (\_ -> newAttr hn) + +-- | Adds a property to a Host +-- Can add Properties, RevertableProperties, and AttrProperties +(&) :: IsProp p => Host -> p -> Host +(Host ps as) & p = Host (ps ++ [toProp p]) (as . getAttr p) --- | Adds a property to the list. --- Can add both Properties and RevertableProperties. -(&) :: IsProp p => [Property] -> p -> [Property] -ps & p = ps ++ [toProp p] infixl 1 & --- | Adds a property to the list in reverted form. -(!) :: [Property] -> RevertableProperty -> [Property] -ps ! p = ps ++ [toProp $ revert p] +-- | Adds a property to the Host in reverted form. +(!) :: Host -> RevertableProperty -> Host +(Host ps as) ! p = Host (ps ++ [toProp q]) (as . getAttr q) + where + q = revert p + infixl 1 ! + +-- | Makes a propertyList of a set of properties, using the same syntax +-- used by `host`. +-- +-- > template "my template" $ props +-- & someproperty +-- ! oldproperty +-- +-- Note that none of the properties can define Attrs, because +-- they will not propigate out to the host that this is added to. +-- +-- Unfortunately, this is not currently enforced at the type level, so +-- attempting to set an Attr in here will be run time error. +template :: Desc -> Host -> Property +template desc h@(Host ps _) + | hostAttr h == hostAttr props = propertyList desc ps + | otherwise = error $ desc ++ ": template contains Attr" + +props :: Host +props = Host [] (\_ -> hostnameless) -- cgit v1.2.3