aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/AddPath.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-06-11 03:05:02 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-06-11 03:05:02 +0300
commitf5f056d79087f4ee3038643d02a82c1ad574553f (patch)
tree7d0313a1cddfe7be70378844d9b6bc487982c65d /app/AddPath.hs
parentcode style (diff)
downloadwindows-env-f5f056d79087f4ee3038643d02a82c1ad574553f.tar.gz
windows-env-f5f056d79087f4ee3038643d02a82c1ad574553f.zip
refactoring
The fact whether the registry value was a regular or an expandable string is now propagated up to the `Environment` module (and even further to the apps). This was done to get rid of these weird `setString*` functions (and the like). I don't feel like I've came up with the right abstractions yet though, so there's more work on this to come.
Diffstat (limited to '')
-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