summaryrefslogtreecommitdiff
path: root/Propellor/Property.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Propellor/Property.hs')
-rw-r--r--Propellor/Property.hs38
1 files changed, 33 insertions, 5 deletions
diff --git a/Propellor/Property.hs b/Propellor/Property.hs
index 3a3c1cb1..5b1800ef 100644
--- a/Propellor/Property.hs
+++ b/Propellor/Property.hs
@@ -10,8 +10,10 @@ import "mtl" Control.Monad.Reader
import Propellor.Types
import Propellor.Types.Attr
+import Propellor.Attr
import Propellor.Engine
import Utility.Monad
+import System.FilePath
makeChange :: IO () -> Propellor Result
makeChange a = liftIO a >> return MadeChange
@@ -52,14 +54,19 @@ p1 `before` p2 = Property (propertyDesc p1) $ do
-- file to indicate whether it has run before.
-- Use with caution.
flagFile :: Property -> FilePath -> Property
-flagFile property flagfile = Property (propertyDesc property) $
- go =<< liftIO (doesFileExist flagfile)
+flagFile property = flagFile' property . return
+
+flagFile' :: Property -> IO FilePath -> Property
+flagFile' property getflagfile = Property (propertyDesc property) $ do
+ flagfile <- liftIO getflagfile
+ go flagfile =<< liftIO (doesFileExist flagfile)
where
- go True = return NoChange
- go False = do
+ go _ True = return NoChange
+ go flagfile False = do
r <- ensureProperty property
when (r == MadeChange) $ liftIO $
- unlessM (doesFileExist flagfile) $
+ unlessM (doesFileExist flagfile) $ do
+ createDirectoryIfMissing True (takeDirectory flagfile)
writeFile flagfile ""
return r
@@ -85,6 +92,26 @@ check c property = Property (propertyDesc property) $ ifM (liftIO c)
, return NoChange
)
+-- | Marks a Property as trivial. It can only return FailedChange or
+-- NoChange.
+--
+-- Useful when it's just as expensive to check if a change needs
+-- to be made as it is to just idempotently assure the property is
+-- satisfied. For example, chmodding a file.
+trivial :: Property -> Property
+trivial p = Property (propertyDesc p) $ do
+ r <- ensureProperty p
+ if r == MadeChange
+ then return NoChange
+ else return r
+
+-- | Makes a property that is satisfied differently depending on the host's
+-- operating system.
+--
+-- Note that the operating system may not be declared for some hosts.
+withOS :: Desc -> (Maybe System -> Propellor Result) -> Property
+withOS desc a = Property desc $ a =<< getOS
+
boolProperty :: Desc -> IO Bool -> Property
boolProperty desc a = Property desc $ ifM (liftIO a)
( return MadeChange
@@ -105,6 +132,7 @@ 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]) (getAttr p . as)