From 8e7b296e820e7513c7846ceeb3fbd87d60bc95f4 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 30 Mar 2014 13:12:33 -0400 Subject: split out Property.FIle --- Property/Apt.hs | 3 ++- Property/File.hs | 22 ++++++++++++++++++++++ Property/Hostname.hs | 6 +++--- Property/Ssh.hs | 5 +++-- Property/Tor.hs | 3 ++- 5 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 Property/File.hs (limited to 'Property') diff --git a/Property/Apt.hs b/Property/Apt.hs index 4d58574f..653c0fca 100644 --- a/Property/Apt.hs +++ b/Property/Apt.hs @@ -7,6 +7,7 @@ import System.IO import Control.Monad import Property +import qualified Property.File as File import Utility.SafeCommand import Utility.Process @@ -47,7 +48,7 @@ stdSourcesList :: Suite -> Property stdSourcesList = setSourcesList . debCdn setSourcesList :: [Line] -> Property -setSourcesList ls = fileHasContent sourcesList ls `onChange` update +setSourcesList ls = sourcesList `File.hasContent` ls `onChange` update runApt :: [CommandParam] -> Property runApt ps = cmdProperty' "apt-get" ps env diff --git a/Property/File.hs b/Property/File.hs new file mode 100644 index 00000000..a4b21961 --- /dev/null +++ b/Property/File.hs @@ -0,0 +1,22 @@ +module Property.File where + +import Property + +{- Replaces all the content of a file. -} +hasContent :: FilePath -> [Line] -> Property +f `hasContent` newcontent = FileProperty ("replace " ++ f) + f (\_oldcontent -> newcontent) + +{- 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) f go + 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) f (filter (/= l)) diff --git a/Property/Hostname.hs b/Property/Hostname.hs index 7baf7178..38e9dbe8 100644 --- a/Property/Hostname.hs +++ b/Property/Hostname.hs @@ -1,11 +1,11 @@ module Property.Hostname where import Property +import qualified Property.File as File import Utility.SafeCommand type HostName = String set :: HostName -> Property -set hostname = - fileHasContent "/etc/hostname" [hostname] - `onChange` cmdProperty "hostname" [Param hostname] +set hostname = "/etc/hostname" `File.hasContent` [hostname] + `onChange` cmdProperty "hostname" [Param hostname] diff --git a/Property/Ssh.hs b/Property/Ssh.hs index 141e3495..98149bcb 100644 --- a/Property/Ssh.hs +++ b/Property/Ssh.hs @@ -6,6 +6,7 @@ import System.FilePath import Property import Property.User +import qualified Property.File as File import Utility.SafeCommand import Utility.Exception @@ -18,8 +19,8 @@ sshdConfig = "/etc/ssh/sshd_config" setSshdConfig :: String -> Bool -> Property setSshdConfig setting allowed = combineProperties desc - [ lineNotInFile sshdConfig $ sshline (not allowed) - , lineInFile sshdConfig $ sshline allowed + [ sshdConfig `File.lacksLine` (sshline $ not allowed) + , sshdConfig `File.containsLine` (sshline allowed) ] `onChange` restartSshd where desc = unwords [ "ssh config:", setting, sshBool allowed ] diff --git a/Property/Tor.hs b/Property/Tor.hs index b26ba68e..7f7e7245 100644 --- a/Property/Tor.hs +++ b/Property/Tor.hs @@ -2,12 +2,13 @@ module Property.Tor where import Property import Utility.SafeCommand +import qualified Property.File as File import qualified Property.Apt as Apt isBridge :: Property isBridge = setup `requires` Apt.installed ["tor"] where - setup = fileHasContent "/etc/tor/torrc" + setup = "/etc/tor/torrc" `File.hasContent` [ "SocksPort 0" , "ORPort 443" , "BridgeRelay 1" -- cgit v1.2.3