aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/AddPath.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-02-14 03:11:33 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-02-14 03:11:33 +0300
commit16fca4f16f23364a1441c17ea023bc9744b286c0 (patch)
treef0b2dc1c5504451a46475578f233d01a5e01315c /AddPath.hs
parentstrip extra whitespace (diff)
downloadwindows-env-16fca4f16f23364a1441c17ea023bc9744b286c0.tar.gz
windows-env-16fca4f16f23364a1441c17ea023bc9744b286c0.zip
allow adding/removing multiple paths at once
Diffstat (limited to 'AddPath.hs')
-rw-r--r--AddPath.hs24
1 files changed, 13 insertions, 11 deletions
diff --git a/AddPath.hs b/AddPath.hs
index 8ef00c5..e17adc9 100644
--- a/AddPath.hs
+++ b/AddPath.hs
@@ -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