better autostart file modification

As well as just being nicer, and less code, this uses nubBy equalFilePath
to ensure that the autostart file never gets dups.

Also, removing from the file no longer needs to be a perfect string match;
it also uses equalFilePath.
This commit is contained in:
Joey Hess 2013-04-23 12:36:37 -04:00
parent f3ef6fe6a7
commit 834ce10ced

View file

@ -24,26 +24,26 @@ autoStartFile = userConfigFile "autostart"
readAutoStartFile :: IO [FilePath] readAutoStartFile :: IO [FilePath]
readAutoStartFile = do readAutoStartFile = do
f <- autoStartFile f <- autoStartFile
nub . lines <$> catchDefaultIO "" (readFile f) nub . map dropTrailingPathSeparator . lines
<$> catchDefaultIO "" (readFile f)
modifyAutoStartFile :: ([FilePath] -> [FilePath]) -> IO ()
modifyAutoStartFile func = do
dirs <- readAutoStartFile
let dirs' = nubBy equalFilePath $ func dirs
when (dirs' /= dirs) $ do
f <- autoStartFile
createDirectoryIfMissing True (parentDir f)
viaTmp writeFile f $ unlines $ dirs'
{- Adds a directory to the autostart file. -} {- Adds a directory to the autostart file. -}
addAutoStartFile :: FilePath -> IO () addAutoStartFile :: FilePath -> IO ()
addAutoStartFile path = do addAutoStartFile path = modifyAutoStartFile $ (:) path
dirs <- readAutoStartFile
when (path `notElem` dirs) $ do
f <- autoStartFile
createDirectoryIfMissing True (parentDir f)
viaTmp writeFile f $ unlines $ dirs ++ [path]
{- Removes a directory from the autostart file. -} {- Removes a directory from the autostart file. -}
removeAutoStartFile :: FilePath -> IO () removeAutoStartFile :: FilePath -> IO ()
removeAutoStartFile path = do removeAutoStartFile path = modifyAutoStartFile $
dirs <- readAutoStartFile filter (not . equalFilePath path)
when (path `elem` dirs) $ do
f <- autoStartFile
createDirectoryIfMissing True (parentDir f)
viaTmp writeFile f $ unlines $
filter (not . equalFilePath path) dirs
{- The path to git-annex is written here; which is useful when cabal {- The path to git-annex is written here; which is useful when cabal
- has installed it to some aweful non-PATH location. -} - has installed it to some aweful non-PATH location. -}