diff options
author | Joey Hess <joey@kitenet.net> | 2013-04-23 12:36:37 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-04-23 12:36:37 -0400 |
commit | fb02f030fa02e80f11ac01bb49afdf0dcb6d8f76 (patch) | |
tree | 0d57a6272db5dfeddad1cb9df1495a37762e389f | |
parent | 94530a0ed9f7fbbb0c604cff6be68f9fd04862b6 (diff) |
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.
-rw-r--r-- | Config/Files.hs | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/Config/Files.hs b/Config/Files.hs index e51e8a747..45f478eeb 100644 --- a/Config/Files.hs +++ b/Config/Files.hs @@ -24,26 +24,26 @@ autoStartFile = userConfigFile "autostart" readAutoStartFile :: IO [FilePath] readAutoStartFile = do f <- autoStartFile - nub . lines <$> catchDefaultIO "" (readFile f) + nub . map dropTrailingPathSeparator . lines + <$> catchDefaultIO "" (readFile f) -{- Adds a directory to the autostart file. -} -addAutoStartFile :: FilePath -> IO () -addAutoStartFile path = do +modifyAutoStartFile :: ([FilePath] -> [FilePath]) -> IO () +modifyAutoStartFile func = do dirs <- readAutoStartFile - when (path `notElem` dirs) $ do + let dirs' = nubBy equalFilePath $ func dirs + when (dirs' /= dirs) $ do f <- autoStartFile createDirectoryIfMissing True (parentDir f) - viaTmp writeFile f $ unlines $ dirs ++ [path] + viaTmp writeFile f $ unlines $ dirs' + +{- Adds a directory to the autostart file. -} +addAutoStartFile :: FilePath -> IO () +addAutoStartFile path = modifyAutoStartFile $ (:) path {- Removes a directory from the autostart file. -} removeAutoStartFile :: FilePath -> IO () -removeAutoStartFile path = do - dirs <- readAutoStartFile - when (path `elem` dirs) $ do - f <- autoStartFile - createDirectoryIfMissing True (parentDir f) - viaTmp writeFile f $ unlines $ - filter (not . equalFilePath path) dirs +removeAutoStartFile path = modifyAutoStartFile $ + filter (not . equalFilePath path) {- The path to git-annex is written here; which is useful when cabal - has installed it to some aweful non-PATH location. -} |