summaryrefslogtreecommitdiff
path: root/Propellor/Property/File.hs
blob: befcc59d940a7a216999bd7827a5d248e3d308e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Propellor.Property.File where

import Propellor

type Line = String

{- | Replaces all the content of a file. -}
hasContent :: FilePath -> [Line] -> Property
f `hasContent` newcontent = fileProperty ("replace " ++ f)
	(\_oldcontent -> newcontent) f

{- | Ensures that a line is present in a file, adding it to the end if not. -}
containsLine :: FilePath -> Line -> Property
f `containsLine` l = fileProperty (f ++ " contains:" ++ l) go f
  where
	go ls
		| l `elem` ls = ls
		| otherwise = ls++[l]

{- | 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. -}
lacksLine :: FilePath -> Line -> Property
f `lacksLine` l = fileProperty (f ++ " remove: " ++ l) (filter (/= l)) f

{- | Removes a file. Does not remove symlinks or non-plain-files. -}
notPresent :: FilePath -> Property
notPresent f = check (doesFileExist f) $ Property (f ++ " not present") $ 
	makeChange $ nukeFile f

fileProperty :: Desc -> ([Line] -> [Line]) -> FilePath -> Property
fileProperty desc a f = Property desc $ go =<< doesFileExist f
  where
	go True = do
		ls <- lines <$> catchDefaultIO [] (readFile f)
		let ls' = a ls
		if ls' == ls
			then noChange
			else makeChange $ viaTmp writeFile f (unlines ls')
	go False = makeChange $ writeFile f (unlines $ a [])