diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-11 05:56:10 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-11 05:56:10 +0300 |
commit | 837e60b9d8826168896ad4155ff7046f00665f52 (patch) | |
tree | 18c40efee9f38954079119587d3b60ababf5e555 | |
parent | delenv: ignore missing variables (diff) | |
download | windows-env-837e60b9d8826168896ad4155ff7046f00665f52.tar.gz windows-env-837e60b9d8826168896ad4155ff7046f00665f52.zip |
WindowsEnv: {VarName,VarValue} -> {Name,Value}
Also, fix compiler warnings (I've got too used to building with
`--ghc-options -w`).
-rw-r--r-- | app/AddPath.hs | 8 | ||||
-rw-r--r-- | app/ListPaths.hs | 8 | ||||
-rw-r--r-- | app/RemovePath.hs | 8 | ||||
-rw-r--r-- | app/SetEnv.hs | 4 | ||||
-rw-r--r-- | app/UnsetEnv.hs | 2 | ||||
-rw-r--r-- | app/Utils/PromptMessage.hs | 12 | ||||
-rw-r--r-- | src/WindowsEnv/Environment.hs | 47 |
7 files changed, 43 insertions, 46 deletions
diff --git a/app/AddPath.hs b/app/AddPath.hs index ff80b18..d141270 100644 --- a/app/AddPath.hs +++ b/app/AddPath.hs @@ -20,7 +20,7 @@ import Utils.Prompt import Utils.PromptMessage data Options = Options - { optName :: WindowsEnv.VarName + { optName :: WindowsEnv.Name , optYes :: Bool , optGlobal :: Bool , optPrepend :: Bool @@ -83,15 +83,15 @@ addPath options = runExceptT doAddPath >>= either ioError return defaultValue = do expandedPaths <- mapM WindowsEnv.expand pathsToAdd if pathsToAdd == expandedPaths - then return $ WindowsEnv.VarValue False "" - else return $ WindowsEnv.VarValue True "" + then return $ WindowsEnv.Value False "" + else return $ WindowsEnv.Value True "" doAddPath = do oldValue <- WindowsEnv.query profile varName `catchE` emptyIfMissing let oldPaths = WindowsEnv.pathSplit $ show oldValue let newPaths = pathsToAdd \\ oldPaths unless (null newPaths) $ do - let newValue = WindowsEnv.VarValue (WindowsEnv.varValueExpandable oldValue) $ WindowsEnv.pathJoin (mergePaths oldPaths newPaths) + let newValue = WindowsEnv.Value (WindowsEnv.valueExpandable oldValue) $ WindowsEnv.pathJoin (mergePaths oldPaths newPaths) promptAndEngrave oldValue newValue promptAndEngrave oldValue newValue = do diff --git a/app/ListPaths.hs b/app/ListPaths.hs index f832686..c507d39 100644 --- a/app/ListPaths.hs +++ b/app/ListPaths.hs @@ -30,7 +30,7 @@ data Source = Environment | Registry WindowsEnv.Profile deriving (Eq, Show) data Options = Options - { optName :: WindowsEnv.VarName + { optName :: WindowsEnv.Name , optWhichPaths :: WhichPaths , optSource :: Source } deriving (Eq, Show) @@ -70,12 +70,12 @@ listPaths options = runExceptT doListPaths >>= either ioError return query = queryFrom $ optSource options - queryFrom Environment = lift $ WindowsEnv.VarValue False <$> fromMaybe "" <$> lookupEnv varName + queryFrom Environment = lift $ WindowsEnv.Value False <$> fromMaybe "" <$> lookupEnv varName queryFrom (Registry profile) = WindowsEnv.query profile varName doListPaths = do - value <- query - split <- WindowsEnv.pathSplitAndExpand value + varValue <- query + split <- WindowsEnv.pathSplitAndExpand varValue lift $ do wanted <- filterM (shouldListPath whichPaths) split mapM_ (putStrLn . WindowsEnv.pathOriginal) wanted diff --git a/app/RemovePath.hs b/app/RemovePath.hs index 23cabb7..7157100 100644 --- a/app/RemovePath.hs +++ b/app/RemovePath.hs @@ -19,7 +19,7 @@ import Utils.Prompt import Utils.PromptMessage data Options = Options - { optName :: WindowsEnv.VarName + { optName :: WindowsEnv.Name , optYes :: Bool , optGlobal :: Bool , optPaths :: [String] @@ -69,8 +69,8 @@ removePath options = runExceptT doRemovePath >>= either ioError return defaultValue = do expandedPaths <- mapM WindowsEnv.expand pathsToRemove if pathsToRemove == expandedPaths - then return $ WindowsEnv.VarValue False "" - else return $ WindowsEnv.VarValue True "" + then return $ WindowsEnv.Value False "" + else return $ WindowsEnv.Value True "" doRemovePath = do removePathFrom WindowsEnv.CurrentUser @@ -82,7 +82,7 @@ removePath options = runExceptT doRemovePath >>= either ioError return let oldPaths = WindowsEnv.pathSplit $ show oldValue let newPaths = filter (flip notElem pathsToRemove) oldPaths when (length oldPaths /= length newPaths) $ do - let newValue = WindowsEnv.VarValue (WindowsEnv.varValueExpandable oldValue) (WindowsEnv.pathJoin newPaths) + let newValue = WindowsEnv.Value (WindowsEnv.valueExpandable oldValue) (WindowsEnv.pathJoin newPaths) let promptAnd = if skipPrompt then withoutPrompt else withPrompt $ oldNewMessage profile varName oldValue newValue diff --git a/app/SetEnv.hs b/app/SetEnv.hs index a986226..d590f64 100644 --- a/app/SetEnv.hs +++ b/app/SetEnv.hs @@ -21,7 +21,7 @@ import Utils.PromptMessage data Options = Options { optYes :: Bool , optGlobal :: Bool - , optName :: WindowsEnv.VarName + , optName :: WindowsEnv.Name , optValue :: String } deriving (Eq, Show) @@ -55,7 +55,7 @@ setEnv :: Options -> IO () setEnv options = runExceptT doSetEnv >>= either ioError return where varName = optName options - varValue = WindowsEnv.VarValue False $ optValue options + varValue = WindowsEnv.Value False $ optValue options forAllUsers = optGlobal options profile diff --git a/app/UnsetEnv.hs b/app/UnsetEnv.hs index 98b52e3..02df1a1 100644 --- a/app/UnsetEnv.hs +++ b/app/UnsetEnv.hs @@ -21,7 +21,7 @@ import Utils.PromptMessage data Options = Options { optYes :: Bool , optGlobal :: Bool - , optName :: WindowsEnv.VarName + , optName :: WindowsEnv.Name } deriving (Eq, Show) optionParser :: Parser Options diff --git a/app/Utils/PromptMessage.hs b/app/Utils/PromptMessage.hs index 1315b85..609cb8f 100644 --- a/app/Utils/PromptMessage.hs +++ b/app/Utils/PromptMessage.hs @@ -15,24 +15,24 @@ import Text.Printf (printf) import qualified WindowsEnv -oldNewMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> WindowsEnv.VarValue -> WindowsEnv.VarValue -> String +oldNewMessage :: WindowsEnv.Profile -> WindowsEnv.Name -> WindowsEnv.Value -> WindowsEnv.Value -> String oldNewMessage profile name oldValue newValue = descrMsg ++ oldValueMsg ++ newValueMsg where profileKey = WindowsEnv.profileKeyPath profile descrMsg = printf "Saving variable '%s' to '%s'...\n" name $ show profileKey - oldValueMsg = printf "\tOld value: %s\n" $ WindowsEnv.varValueString oldValue - newValueMsg = printf "\tNew value: %s\n" $ WindowsEnv.varValueString newValue + oldValueMsg = printf "\tOld value: %s\n" $ WindowsEnv.valueString oldValue + newValueMsg = printf "\tNew value: %s\n" $ WindowsEnv.valueString newValue -newMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> WindowsEnv.VarValue -> String +newMessage :: WindowsEnv.Profile -> WindowsEnv.Name -> WindowsEnv.Value -> String newMessage profile name newValue = descrMsg ++ newValueMsg where profileKey = WindowsEnv.profileKeyPath profile descrMsg = printf "Saving variable '%s' to '%s'...\n" name $ show profileKey - newValueMsg = printf "\tNew value: %s\n" $ WindowsEnv.varValueString newValue + newValueMsg = printf "\tNew value: %s\n" $ WindowsEnv.valueString newValue -wipeMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> String +wipeMessage :: WindowsEnv.Profile -> WindowsEnv.Name -> String wipeMessage profile name = printf "Deleting variable '%s' from '%s'...\n" name $ show profileKey where diff --git a/src/WindowsEnv/Environment.hs b/src/WindowsEnv/Environment.hs index c194cbe..5bd5bf6 100644 --- a/src/WindowsEnv/Environment.hs +++ b/src/WindowsEnv/Environment.hs @@ -14,8 +14,8 @@ module WindowsEnv.Environment ( Profile(..) , profileKeyPath - , VarName - , VarValue(..) + , Name + , Value(..) , query , engrave @@ -25,8 +25,8 @@ module WindowsEnv.Environment , pathSplit , expand - , pathSplitAndExpand + , pathSplitAndExpand , ExpandedPath(..) , pathOriginal , pathExpanded @@ -60,37 +60,34 @@ profileKeyPath AllUsers = Registry.KeyPath Registry.LocalMachine , "Environment" ] -type VarName = String - -data VarValue = VarValue - { varValueExpandable :: Bool - , varValueString :: String - } deriving (Eq) +type Name = String -instance Show VarValue where - show = varValueString +data Value = Value + { valueExpandable :: Bool + , valueString :: String + } deriving (Eq, Show) -valueFromRegistry :: Registry.StringValue -> VarValue +valueFromRegistry :: Registry.StringValue -> Value valueFromRegistry (valueType, valueData) - | valueType == Registry.TypeString = VarValue False valueData - | valueType == Registry.TypeExpandableString = VarValue True valueData + | valueType == Registry.TypeString = Value False valueData + | valueType == Registry.TypeExpandableString = Value True valueData | otherwise = error "WindowsEnv.Environment: unexpected" -valueToRegistry :: VarValue -> Registry.StringValue +valueToRegistry :: Value -> Registry.StringValue valueToRegistry value - | varValueExpandable value = (Registry.TypeExpandableString, varValueString value) - | otherwise = (Registry.TypeString, varValueString value) + | valueExpandable value = (Registry.TypeExpandableString, valueString value) + | otherwise = (Registry.TypeString, valueString value) -query :: Profile -> VarName -> ExceptT IOError IO VarValue +query :: Profile -> Name -> ExceptT IOError IO Value query profile name = valueFromRegistry <$> Registry.getStringValue (profileKeyPath profile) name -engrave :: Profile -> VarName -> VarValue -> ExceptT IOError IO () +engrave :: Profile -> Name -> Value -> ExceptT IOError IO () engrave profile name value = do ret <- Registry.setStringValue (profileKeyPath profile) name $ valueToRegistry value lift notifyEnvironmentUpdate return ret -wipe :: Profile -> VarName -> ExceptT IOError IO () +wipe :: Profile -> Name -> ExceptT IOError IO () wipe profile name = do ret <- Registry.deleteValue (profileKeyPath profile) name `catchE` ignoreIfMissing lift notifyEnvironmentUpdate @@ -134,18 +131,18 @@ data ExpandedPath = UnexpandedPath String pathOriginal :: ExpandedPath -> String pathOriginal (UnexpandedPath path) = path -pathOriginal (ExpandedPath original expanded) = original +pathOriginal (ExpandedPath original _) = original pathExpanded :: ExpandedPath -> String pathExpanded (UnexpandedPath path) = path -pathExpanded (ExpandedPath original expanded) = expanded +pathExpanded (ExpandedPath _ expanded) = expanded pathExists :: ExpandedPath -> IO Bool pathExists = doesDirectoryExist . pathExpanded -pathSplitAndExpand :: VarValue -> ExceptT IOError IO [ExpandedPath] +pathSplitAndExpand :: Value -> ExceptT IOError IO [ExpandedPath] pathSplitAndExpand value - | varValueExpandable value = do + | valueExpandable value = do expanded <- expandOnce zipWith ExpandedPath split <$> if length expanded == length split @@ -153,7 +150,7 @@ pathSplitAndExpand value else expandEach | otherwise = return $ map UnexpandedPath $ pathSplit joined where - joined = varValueString value + joined = valueString value split = pathSplit joined expandOnce = pathSplit <$> expand joined expandEach = mapM expand split |