diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-02-14 03:11:33 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-02-14 03:11:33 +0300 |
commit | 16fca4f16f23364a1441c17ea023bc9744b286c0 (patch) | |
tree | f0b2dc1c5504451a46475578f233d01a5e01315c | |
parent | strip extra whitespace (diff) | |
download | windows-env-16fca4f16f23364a1441c17ea023bc9744b286c0.tar.gz windows-env-16fca4f16f23364a1441c17ea023bc9744b286c0.zip |
allow adding/removing multiple paths at once
-rw-r--r-- | AddPath.hs | 24 | ||||
-rw-r--r-- | RemovePath.hs | 30 |
2 files changed, 26 insertions, 28 deletions
@@ -6,7 +6,7 @@ module Main ( main ) where -import Control.Monad ( when ) +import Control.Monad ( mapM_, when ) import System.Console.GetOpt import System.Environment ( getArgs, getProgName ) import System.Exit ( exitFailure, exitSuccess ) @@ -20,17 +20,19 @@ main = do case getOpt Permute optionDescription rawArgs of (actions, args, []) -> do options <- foldl (>>=) (return defaultOptions) actions - case args of - [path] -> addPath path options - _ -> invalidNumberOfArguments + addPath args options (_, _, errorMessages) -> exitWithUsageErrors errorMessages -addPath :: String -> Options -> IO () -addPath path options = do - oldVal <- EnvUtils.getEnv $ name options - when (notElem path $ EnvUtils.splitPaths oldVal) $ do - oldValFromReg <- EnvUtils.queryFromRegistry (env options) (name options) - EnvUtils.saveToRegistryWithPrompt (env options) (name options) $ EnvUtils.joinPaths [path,oldValFromReg] +addPath :: [String] -> Options -> IO () +addPath paths options = do + missingPaths <- dropIncludedPaths paths + when (not $ null missingPaths) $ do + oldPath <- EnvUtils.queryFromRegistry (env options) (name options) + EnvUtils.saveToRegistryWithPrompt (env options) (name options) $ EnvUtils.joinPaths $ missingPaths ++ [oldPath] + where + dropIncludedPaths paths = do + currentPath <- EnvUtils.getEnv $ name options + return $ filter (flip notElem $ EnvUtils.splitPaths currentPath) paths data Options = Options { name :: String , env :: EnvUtils.RegistryBasedEnvironment } @@ -48,7 +50,7 @@ buildHelpMessage = do buildHeader :: IO String buildHeader = do progName <- getProgName - return $ "Usage: " ++ progName ++ " [OPTIONS...] PATH\nOptions:" + return $ "Usage: " ++ progName ++ " [OPTIONS...] [PATH...]\nOptions:" exitWithHelpMessage :: a -> IO b exitWithHelpMessage _ = do diff --git a/RemovePath.hs b/RemovePath.hs index 5fd6d6d..2e8fd01 100644 --- a/RemovePath.hs +++ b/RemovePath.hs @@ -20,27 +20,23 @@ main = do case getOpt Permute optionDescription rawArgs of (actions, args, []) -> do options <- foldl (>>=) (return defaultOptions) actions - case args of - [path] -> removePath path options - _ -> invalidNumberOfArguments + removePath args options (_, _, errorMessages) -> exitWithUsageErrors errorMessages -removePath :: String -> Options -> IO () -removePath path options = do +removePath :: [String] -> Options -> IO () +removePath paths options = do let varName = name options userVal <- EnvUtils.queryFromRegistry EnvUtils.CurrentUserEnvironment varName let userValParts = EnvUtils.splitPaths userVal - if path `elem` userValParts - then do - let newUserValParts = filter (/= path) userValParts - EnvUtils.saveToRegistryWithPrompt EnvUtils.CurrentUserEnvironment varName $ EnvUtils.joinPaths newUserValParts - else do - when (global options) $ do - globalVal <- EnvUtils.queryFromRegistry EnvUtils.AllUsersEnvironment varName - let globalValParts = EnvUtils.splitPaths globalVal - when (path `elem` globalValParts) $ do - let newGlobalValParts = filter (/= path) globalValParts - EnvUtils.saveToRegistryWithPrompt EnvUtils.AllUsersEnvironment varName $ EnvUtils.joinPaths newGlobalValParts + let newUserValParts = filter (`notElem` paths) userValParts + when (length userValParts /= length newUserValParts) $ do + EnvUtils.saveToRegistryWithPrompt EnvUtils.CurrentUserEnvironment varName $ EnvUtils.joinPaths newUserValParts + when (global options) $ do + globalVal <- EnvUtils.queryFromRegistry EnvUtils.AllUsersEnvironment varName + let globalValParts = EnvUtils.splitPaths globalVal + let newGlobalValParts = filter (`notElem` paths) globalValParts + when (length globalValParts /= length newGlobalValParts) $ do + EnvUtils.saveToRegistryWithPrompt EnvUtils.AllUsersEnvironment varName $ EnvUtils.joinPaths newGlobalValParts data Options = Options { name :: String , global :: Bool } @@ -58,7 +54,7 @@ buildHelpMessage = do buildHeader :: IO String buildHeader = do progName <- getProgName - return $ "Usage: " ++ progName ++ " [OPTIONS...] PATH\nOptions:" + return $ "Usage: " ++ progName ++ " [OPTIONS...] [PATH...]\nOptions:" exitWithHelpMessage :: a -> IO b exitWithHelpMessage _ = do |