From 9c9bd3036dba1d046dc80ab39f62805f7a614a6d Mon Sep 17 00:00:00 2001 From: FĂ©lix Sipma Date: Mon, 13 Jun 2016 17:45:44 +0200 Subject: ConfFile: add hasIniSection property --- src/Propellor/Property/ConfFile.hs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/Propellor/Property/ConfFile.hs') diff --git a/src/Propellor/Property/ConfFile.hs b/src/Propellor/Property/ConfFile.hs index 270e04f1..d49a3753 100644 --- a/src/Propellor/Property/ConfFile.hs +++ b/src/Propellor/Property/ConfFile.hs @@ -24,7 +24,7 @@ type SectionStart = Line -> Bool type SectionPast = Line -> Bool -- | run on all lines in the section, including the SectionStart line; -- can add, delete, and modify lines, or even delete entire section -type AdjustSection = [Line] -> [Line] +type AdjustSection = [Line] -> [Line] -- | if SectionStart does not find the section in the file, this is used to -- insert the section somewhere within it type InsertSection = [Line] -> [Line] @@ -92,6 +92,21 @@ containsIniSetting f (header, key, value) = go (l:ls) = if isKeyVal l then confline : ls else l : (go ls) isKeyVal x = (filter (/= ' ') . takeWhile (/= '=')) x `elem` [key, '#':key] +-- | Ensures that a .ini file exists and contains a section +-- with a given key=value list of settings. +hasIniSection :: FilePath -> IniSection -> [(IniKey, String)] -> Property UnixLike +hasIniSection f header keyvalues = + adjustIniSection + ("set " ++ f ++ " section [" ++ header ++ "]") + header + go + (++ [confheader] ++ conflines) -- add missing section at end + f + where + confheader = iniHeader header + conflines = map (\(key, value) -> key ++ "=" ++ value) keyvalues + go _ = conflines + -- | Ensures that a .ini file does not contain the specified section. lacksIniSection :: FilePath -> IniSection -> Property UnixLike lacksIniSection f header = -- cgit v1.2.3