summaryrefslogtreecommitdiff
path: root/Property
diff options
context:
space:
mode:
authorJoey Hess2014-03-30 13:12:33 -0400
committerJoey Hess2014-03-30 13:12:33 -0400
commit8e7b296e820e7513c7846ceeb3fbd87d60bc95f4 (patch)
tree4eb80ad32b42ee871f5704cd1af99baf9e8cacc6 /Property
parente741b7b82e0b5be2809d1da7f54d8b0fcd0449e3 (diff)
split out Property.FIle
Diffstat (limited to 'Property')
-rw-r--r--Property/Apt.hs3
-rw-r--r--Property/File.hs22
-rw-r--r--Property/Hostname.hs6
-rw-r--r--Property/Ssh.hs5
-rw-r--r--Property/Tor.hs3
5 files changed, 32 insertions, 7 deletions
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"