diff options
Diffstat (limited to 'apps/RemovePath.hs')
-rw-r--r-- | apps/RemovePath.hs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/apps/RemovePath.hs b/apps/RemovePath.hs index e04a67b..2fecda6 100644 --- a/apps/RemovePath.hs +++ b/apps/RemovePath.hs @@ -7,29 +7,31 @@ module Main (main) where import Control.Monad (when) +import Data.List ((\\)) +import Data.Maybe (fromJust, isJust) import Options.Applicative import qualified Environment data Options = Options - { name :: String - , global :: Bool - , paths :: [String] + { optName :: String + , optGlobal :: Bool + , optPaths :: [String] } deriving (Eq, Show) options = Options - <$> nameOption - <*> globalOption - <*> pathArgs + <$> optNameDesc + <*> optGlobalDesc + <*> optPathsDesc where - nameOption = strOption $ + optNameDesc = strOption $ long "name" <> short 'n' <> metavar "NAME" <> value "PATH" <> help "Specify variable name ('PATH' by default)" - globalOption = switch $ + optGlobalDesc = switch $ long "global" <> short 'g' <> help "Whether to remove for all users" - pathArgs = many $ argument str $ + optPathsDesc = many $ argument str $ metavar "PATH" <> help "Directory path(s)" @@ -41,15 +43,18 @@ main = execParser parser >>= removePath removePath :: Options -> IO () removePath options = do - let varName = name options - userVal <- Environment.query Environment.CurrentUser varName - let userValParts = Environment.pathSplit userVal - let newUserValParts = filter (flip notElem $ paths options) userValParts - when (length userValParts /= length newUserValParts) $ do - Environment.engraveWithPrompt Environment.CurrentUser varName $ Environment.pathJoin newUserValParts - when (global options) $ do - globalVal <- Environment.query Environment.AllUsers varName - let globalValParts = Environment.pathSplit globalVal - let newGlobalValParts = filter (flip notElem $ paths options) globalValParts - when (length globalValParts /= length newGlobalValParts) $ do - Environment.engraveWithPrompt Environment.AllUsers varName $ Environment.pathJoin newGlobalValParts + removePathFrom Environment.CurrentUser options + when (optGlobal options) $ do + removePathFrom Environment.AllUsers options + where + varName = optName options + pathsToRemove = optPaths options + + removePathFrom env options = do + oldValue <- Environment.query env varName + when (isJust oldValue) $ do + let oldPaths = Environment.pathSplit $ fromJust oldValue + let newPaths = oldPaths \\ pathsToRemove + when (length oldPaths /= length newPaths) $ do + let newValue = Environment.pathJoin newPaths + Environment.engraveWithPrompt env varName newValue |