aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/AddPath.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/AddPath.hs')
-rw-r--r--app/AddPath.hs39
1 files changed, 24 insertions, 15 deletions
diff --git a/app/AddPath.hs b/app/AddPath.hs
index fc8d5de..1993b4a 100644
--- a/app/AddPath.hs
+++ b/app/AddPath.hs
@@ -20,11 +20,11 @@ import Utils.Prompt
import Utils.PromptMessage
data Options = Options
- { optName :: WindowsEnv.VarName
+ { optName :: WindowsEnv.VarName
, optYes :: Bool
, optGlobal :: Bool
, optPrepend :: Bool
- , optPaths :: [WindowsEnv.VarValue]
+ , optPaths :: [String]
} deriving (Eq, Show)
optionParser :: Parser Options
@@ -62,7 +62,7 @@ addPath :: Options -> IO ()
addPath options = runExceptT doAddPath >>= either ioError return
where
varName = optName options
- pathsToAdd = optPaths options
+ pathsToAdd = nub $ optPaths options
forAllUsers = optGlobal options
profile
@@ -72,22 +72,31 @@ addPath options = runExceptT doAddPath >>= either ioError return
skipPrompt = optYes options
prepend = optPrepend options
- append xs ys
- | prepend = ys ++ xs
- | otherwise = xs ++ ys
+ mergePaths old new
+ | prepend = new ++ old
+ | otherwise = old ++ new
emptyIfMissing e
- | isDoesNotExistError e = return ""
+ | isDoesNotExistError e = defaultValue
| otherwise = throwE e
+ defaultValue = do
+ expandedPaths <- mapM WindowsEnv.expand pathsToAdd
+ if pathsToAdd == expandedPaths
+ then return $ WindowsEnv.VarValue False ""
+ else return $ WindowsEnv.VarValue True ""
+
doAddPath = do
oldValue <- WindowsEnv.query profile varName `catchE` emptyIfMissing
- let oldPaths = WindowsEnv.pathSplit oldValue
- let newPaths = (nub pathsToAdd) \\ oldPaths
+ let oldPaths = WindowsEnv.pathSplit $ show oldValue
+ let newPaths = pathsToAdd \\ oldPaths
unless (null newPaths) $ do
- let newValue = WindowsEnv.pathJoin $ append oldPaths newPaths
- let promptAnd = if skipPrompt
- then withoutPrompt
- else withPrompt $ oldNewMessage profile varName oldValue newValue
- let engrave = WindowsEnv.engrave profile varName newValue
- void $ promptAnd engrave
+ let newValue = WindowsEnv.VarValue (WindowsEnv.varValueExpandable oldValue) $ WindowsEnv.pathJoin (mergePaths oldPaths newPaths)
+ promptAndEngrave oldValue newValue
+
+ promptAndEngrave oldValue newValue = do
+ let promptAnd = if skipPrompt
+ then withoutPrompt
+ else withPrompt $ oldNewMessage profile varName (show oldValue) (show newValue)
+ let engrave = WindowsEnv.engrave profile varName newValue
+ void $ promptAnd engrave