From 6a6aa61b54966544987063df9337dc9d2ff058a5 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 19:58:49 -0700 Subject: document File.containsLines --- src/Propellor/Property/File.hs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index 95fc6f81..b6f1a8d6 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -21,6 +21,12 @@ f `hasContent` newcontent = fileProperty containsLine :: FilePath -> Line -> Property UnixLike f `containsLine` l = f `containsLines` [l] +-- | Ensures that a list of lines are present in a file, adding any that are not +-- to the end of the file. +-- +-- Note that this property does not guarantee that the lines will appear +-- consecutively, nor in the order specified. If you need either of these, use +-- 'File.containsBlock'. containsLines :: FilePath -> [Line] -> Property UnixLike f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f where -- cgit v1.3-2-g0d8e From 4e08fe0a738617160c43c268c99441168aa7bd9b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 19:59:00 -0700 Subject: add File.containsBlock --- src/Propellor/Property/File.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index b6f1a8d6..f57480a3 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -6,6 +6,7 @@ import Propellor.Base import Utility.FileMode import qualified Data.ByteString.Lazy as L +import Data.List (isInfixOf, isPrefixOf) import System.Posix.Files import System.Exit @@ -32,6 +33,22 @@ f `containsLines` ls = fileProperty (f ++ " contains:" ++ show ls) go f where go content = content ++ filter (`notElem` content) ls +-- | Ensures that a block of consecutive lines is present in a file, adding it +-- to the end if not. Revert to ensure that the block is not present (though +-- the lines it contains could be present, non-consecutively). +containsBlock :: FilePath -> [Line] -> RevertableProperty UnixLike UnixLike +f `containsBlock` ls = + fileProperty (f ++ " contains block:" ++ show ls) add f + fileProperty (f ++ " lacks block:" ++ show ls) remove f + where + add content + | ls `isInfixOf` content = content + | otherwise = content ++ ls + remove [] = [] + remove content@(x:xs) + | ls `isPrefixOf` content = x : remove (drop (length ls) xs) + | otherwise = x : remove xs + -- | 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. -- cgit v1.3-2-g0d8e From 7ba44cc7fb3b0897fa396a5b66068a2db7961afb Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 31 Jan 2017 21:29:33 -0700 Subject: fix removal of blocks --- src/Propellor/Property/File.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index f57480a3..9f73b14a 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -46,7 +46,7 @@ f `containsBlock` ls = | otherwise = content ++ ls remove [] = [] remove content@(x:xs) - | ls `isPrefixOf` content = x : remove (drop (length ls) xs) + | ls `isPrefixOf` content = remove (drop (length ls) content) | otherwise = x : remove xs -- | Ensures that a line is not present in a file. -- cgit v1.3-2-g0d8e From b1f0bd05941f5d8c42a28d33316a0a0452a62476 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 2 Feb 2017 15:40:30 -0400 Subject: Added Propellor.Property.File.configFileName and related functions to generate good filenames for config directories. spwhitton has a branch that could use this, and there are several places in propellor that do something ad-hoc that would have been better implemented using this. I was not able to switch any of the existing ad-hoc stuff, but this can be used going forward for new stuff. This commit was sponsored by Anthony DeRobertis on Patreon. --- debian/changelog | 2 ++ src/Propellor/Property/File.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src/Propellor/Property/File.hs') diff --git a/debian/changelog b/debian/changelog index 0e5aa8d5..30af1b88 100644 --- a/debian/changelog +++ b/debian/changelog @@ -10,6 +10,8 @@ propellor (3.2.4) UNRELEASED; urgency=medium Thanks, Sean Whitton. * stack.yaml: Compile with GHC 8.0.1 against lts-7.16. Thanks, Andrew Cowie. + * Added Propellor.Property.File.configFileName and related functions + to generate good filenames for config directories. -- Joey Hess Sat, 24 Dec 2016 15:06:36 -0400 diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index 95fc6f81..fe2b1057 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -8,6 +8,7 @@ import Utility.FileMode import qualified Data.ByteString.Lazy as L import System.Posix.Files import System.Exit +import Data.Char type Line = String @@ -221,3 +222,42 @@ viaStableTmp a f = bracketIO setup cleanup go go tmpfile = do a tmpfile liftIO $ rename tmpfile f + +-- | Generates a base configuration file name from a String, which +-- can be put in a configuration directory, such as +-- /etc/apt/sources.list.d/ +-- +-- The generated file name is limited to using ASCII alphanumerics, +-- '_' and '.', so that programs that only accept a limited set of +-- characters will accept it. Any other characters will be encoded +-- in escaped form. +-- +-- Some file extensions, such as ".dpkg-new" may be filtered out by +-- programs that use configuration directories. To avoid such problems, +-- it's a good idea to add an static prefix and extension to the +-- result of this function. For example: +-- +-- > aptSource foo = "/etc/apt/sources.list.d" "propellor_" ++ configFileName foo <.> ".conf" +configFileName :: String -> FilePath +configFileName = concatMap escape + where + escape c + | isAscii c && isAlphaNum c = [c] + | c == '.' = [c] + | otherwise = '_' : show (ord c) + +-- | Applies configFileName to any value that can be shown. +showConfigFileName :: Show v => v -> FilePath +showConfigFileName = configFileName . show + +-- | Inverse of showConfigFileName. +readConfigFileName :: Read v => FilePath -> Maybe v +readConfigFileName = readish . unescape + where + unescape [] = [] + unescape ('_':cs) = case break (not . isDigit) cs of + ([], _) -> '_' : unescape cs + (ns, cs') -> case readish ns of + Nothing -> '_' : ns ++ unescape cs' + Just n -> chr n : unescape cs' + unescape (c:cs) = c : unescape cs -- cgit v1.3-2-g0d8e From c682c51e828b82b60443b3cf368114f6506b03e9 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 2 Feb 2017 15:51:49 -0400 Subject: haddock formatting --- src/Propellor/Property/File.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index fe2b1057..388bcb6c 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -225,14 +225,14 @@ viaStableTmp a f = bracketIO setup cleanup go -- | Generates a base configuration file name from a String, which -- can be put in a configuration directory, such as --- /etc/apt/sources.list.d/ +-- -- -- The generated file name is limited to using ASCII alphanumerics, --- '_' and '.', so that programs that only accept a limited set of +-- \'_\' and \'.\' , so that programs that only accept a limited set of -- characters will accept it. Any other characters will be encoded -- in escaped form. -- --- Some file extensions, such as ".dpkg-new" may be filtered out by +-- Some file extensions, such as ".old" may be filtered out by -- programs that use configuration directories. To avoid such problems, -- it's a good idea to add an static prefix and extension to the -- result of this function. For example: -- cgit v1.3-2-g0d8e From f861070a3ad159a60961292ccdb53e30524968cd Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 2 Feb 2017 15:52:52 -0400 Subject: better example --- src/Propellor/Property/File.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Propellor/Property/File.hs') diff --git a/src/Propellor/Property/File.hs b/src/Propellor/Property/File.hs index 388bcb6c..9241cb1b 100644 --- a/src/Propellor/Property/File.hs +++ b/src/Propellor/Property/File.hs @@ -237,7 +237,7 @@ viaStableTmp a f = bracketIO setup cleanup go -- it's a good idea to add an static prefix and extension to the -- result of this function. For example: -- --- > aptSource foo = "/etc/apt/sources.list.d" "propellor_" ++ configFileName foo <.> ".conf" +-- > aptConf foo = "/etc/apt/apt.conf.d" "propellor_" ++ configFileName foo <.> ".conf" configFileName :: String -> FilePath configFileName = concatMap escape where -- cgit v1.3-2-g0d8e