summaryrefslogtreecommitdiff
path: root/src/Propellor/Property/ConfFile.hs
diff options
context:
space:
mode:
authorFélix Sipma2016-06-13 17:45:44 +0200
committerFélix Sipma2016-06-13 17:45:44 +0200
commit9c9bd3036dba1d046dc80ab39f62805f7a614a6d (patch)
tree05732d1d0875d5db7eda9c62db70d5bf8dbcfb8b /src/Propellor/Property/ConfFile.hs
parent02b003ab910a9c697b011430cc0c09fa014eae68 (diff)
ConfFile: add hasIniSection property
Diffstat (limited to 'src/Propellor/Property/ConfFile.hs')
-rw-r--r--src/Propellor/Property/ConfFile.hs17
1 files changed, 16 insertions, 1 deletions
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 =