From 4e08fe0a738617160c43c268c99441168aa7bd9b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 19:59:00 -0700 Subject: add File.containsBlock --- src/Propellor/Property/File.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index b6f1a8d6..f57480a3 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -6,6 +6,7 @@ import Propellor.Base import Utility.FileMode import qualified Data.ByteString.Lazy as L +import Data.List (isInfixOf, isPrefixOf) import System.Posix.Files import System.Exit @@ -32,6 +33,22 @@ f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f where go content = content ++ filter (`notElem` content) ls +-- | Ensures that a block of consecutive lines is present in a file, adding it +-- to the end if not. Revert to ensure that the block is not present (though +-- the lines it contains could be present, non-consecutively). +containsBlock :: FilePath -> [Line] -> RevertableProperty UnixLike UnixLike +f `containsBlock` ls = + fileProperty (f ++ " contains block:" ++ show ls) add f + fileProperty (f ++ " lacks block:" ++ show ls) remove f + where + add content + | ls `isInfixOf` content = content + | otherwise = content ++ ls + remove [] = [] + remove content@(x:xs) + | ls `isPrefixOf` content = x : remove (drop (length ls) xs) + | otherwise = x : remove xs + -- | Ensures that a line is not present in a file. -- Note that the file is ensured to exist, so if it doesn't, an empty -- file will be written. -- cgit v1.2.3