diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-03-25 05:56:02 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-03-25 05:56:02 +0300 |
commit | 80f6847645459dfd21e531946e1eeaf2384a2dff (patch) | |
tree | 8a93e818fe647974bd724be08092e9475d03425f /apps | |
parent | add README to the package (diff) | |
download | windows-env-80f6847645459dfd21e531946e1eeaf2384a2dff.tar.gz windows-env-80f6847645459dfd21e531946e1eeaf2384a2dff.zip |
rename directories
Diffstat (limited to 'apps')
-rw-r--r-- | apps/AddPath.hs | 83 | ||||
-rw-r--r-- | apps/ListPaths.hs | 84 | ||||
-rw-r--r-- | apps/Prompt.hs | 50 | ||||
-rw-r--r-- | apps/PromptMessage.hs | 39 | ||||
-rw-r--r-- | apps/RemovePath.hs | 85 | ||||
-rw-r--r-- | apps/SetEnv.hs | 72 | ||||
-rw-r--r-- | apps/UnsetEnv.hs | 66 |
7 files changed, 0 insertions, 479 deletions
diff --git a/apps/AddPath.hs b/apps/AddPath.hs deleted file mode 100644 index 73d86d0..0000000 --- a/apps/AddPath.hs +++ /dev/null @@ -1,83 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module Main (main) where - -import Control.Monad (void, when) -import Control.Monad.Trans.Except (catchE, runExceptT, throwE) -import Data.List (union) -import System.IO.Error (ioError, isDoesNotExistError) - -import Options.Applicative - -import qualified WindowsEnv - -import Prompt -import PromptMessage - -data Options = Options - { optName :: WindowsEnv.VarName - , optYes :: Bool - , optGlobal :: Bool - , optPaths :: [WindowsEnv.VarValue] - } deriving (Eq, Show) - -optionParser :: Parser Options -optionParser = Options - <$> optNameDesc - <*> optYesDesc - <*> optGlobalDesc - <*> optPathsDesc - where - optNameDesc = strOption - $ long "name" <> short 'n' - <> metavar "NAME" <> value "PATH" - <> help "Variable name ('PATH' by default)" - optYesDesc = switch - $ long "yes" <> short 'y' - <> help "Skip confirmation prompt" - optGlobalDesc = switch - $ long "global" <> short 'g' - <> help "Add for all users" - optPathsDesc = many $ argument str - $ metavar "PATH" - <> help "Directories to add" - -main :: IO () -main = execParser parser >>= addPath - where - parser = info (helper <*> optionParser) $ - fullDesc <> progDesc "Add directories to your PATH" - -addPath :: Options -> IO () -addPath options = runExceptT doAddPath >>= either ioError return - where - varName = optName options - pathsToAdd = optPaths options - - forAllUsers = optGlobal options - profile - | forAllUsers = WindowsEnv.AllUsers - | otherwise = WindowsEnv.CurrentUser - - skipPrompt = optYes options - - emptyIfMissing e - | isDoesNotExistError e = return "" - | otherwise = throwE e - - doAddPath = do - oldValue <- WindowsEnv.query profile varName `catchE` emptyIfMissing - let oldPaths = WindowsEnv.pathSplit oldValue - let newPaths = oldPaths `union` pathsToAdd - when (length oldPaths /= length newPaths) $ do - let newValue = WindowsEnv.pathJoin newPaths - let promptAnd = if skipPrompt - then withoutPrompt - else withPrompt $ oldNewMessage profile varName oldValue newValue - let engrave = WindowsEnv.engrave profile varName newValue - void $ promptAnd engrave diff --git a/apps/ListPaths.hs b/apps/ListPaths.hs deleted file mode 100644 index 666423f..0000000 --- a/apps/ListPaths.hs +++ /dev/null @@ -1,84 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module Main (main) where - -import Control.Monad (filterM) -import Control.Monad.Trans.Class (lift) -import Control.Monad.Trans.Except (runExceptT) -import Data.Maybe (fromMaybe) -import System.Directory (doesDirectoryExist) -import System.Environment (lookupEnv) -import System.IO.Error (ioError) - -import Options.Applicative - -import qualified WindowsEnv - -data WhichPaths = All | ExistingOnly | MissingOnly - deriving (Eq, Show) - -shouldListPath :: WhichPaths -> WindowsEnv.VarValue -> IO Bool -shouldListPath All = return . const True -shouldListPath ExistingOnly = doesDirectoryExist -shouldListPath MissingOnly = fmap not . doesDirectoryExist - -data Source = Environment | Registry WindowsEnv.Profile - deriving (Eq, Show) - -data Options = Options - { optName :: WindowsEnv.VarName - , optWhichPaths :: WhichPaths - , optSource :: Source - } deriving (Eq, Show) - -optionParser :: Parser Options -optionParser = Options - <$> optNameDesc - <*> optWhichPathsDesc - <*> optSourceDesc - where - optNameDesc = strOption - $ long "name" <> short 'n' - <> metavar "NAME" <> value "PATH" - <> help "Variable name ('PATH' by default)" - optWhichPathsDesc = pure All - <|> flag' ExistingOnly (long "existing" <> short 'e' - <> help "List existing paths only") - <|> flag' MissingOnly (long "missing" <> short 'm' - <> help "List missing paths only") - optSourceDesc = pure Environment - <|> flag' (Registry WindowsEnv.CurrentUser) (long "user" <> short 'u' - <> help "List current user's paths only") - <|> flag' (Registry WindowsEnv.AllUsers) (long "global" <> short 'g' - <> help "List global (all users') paths only") - -main :: IO () -main = execParser parser >>= listPaths - where - parser = info (helper <*> optionParser) $ - fullDesc <> progDesc "List directories in your PATH" - -listPaths :: Options -> IO () -listPaths options = runExceptT doListPaths >>= either ioError return - where - varName = optName options - whichPaths = optWhichPaths options - source = optSource options - - query = queryFrom source - - queryFrom Environment = lift $ fromMaybe "" <$> lookupEnv varName - queryFrom (Registry profile) = WindowsEnv.query profile varName - - filterPaths = filterM $ shouldListPath whichPaths - - doListPaths = do - paths <- WindowsEnv.pathSplit <$> query - lift $ do - pathsToPrint <- filterPaths paths - mapM_ putStrLn pathsToPrint diff --git a/apps/Prompt.hs b/apps/Prompt.hs deleted file mode 100644 index 404c582..0000000 --- a/apps/Prompt.hs +++ /dev/null @@ -1,50 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : portable - -module Prompt - ( withPrompt - , withoutPrompt - ) where - -import Control.Monad (void, when) -import Control.Monad.Trans.Class (lift) -import Control.Monad.Trans.Except (ExceptT) -import Data.Char (toLower) -import System.IO (hFlush, stdout) - -prompt :: String -> IO String -prompt msg = do - putStr msg - hFlush stdout - getLine - -promptYesNo :: String -> IO Bool -promptYesNo msg = do - response <- map toLower <$> prompt msg - if response `elem` yeses - then return True - else if response `elem` noes - then return False - else promptToContinue - where - yeses = ["y", "yes"] - noes = ["n", "no"] - -promptToContinue :: IO Bool -promptToContinue = promptYesNo "Continue? (y/n) " - -withPrompt :: String -> ExceptT IOError IO a -> ExceptT IOError IO Bool -withPrompt msg m = do - lift $ do - putStr msg - hFlush stdout - agreed <- lift promptToContinue - when agreed $ void m - return agreed - -withoutPrompt :: ExceptT IOError IO a -> ExceptT IOError IO Bool -withoutPrompt m = m >> return True diff --git a/apps/PromptMessage.hs b/apps/PromptMessage.hs deleted file mode 100644 index a3ff776..0000000 --- a/apps/PromptMessage.hs +++ /dev/null @@ -1,39 +0,0 @@ --- | --- Copyright : (c) 2016 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module PromptMessage - ( oldNewMessage - , newMessage - , wipeMessage - ) where - -import Text.Printf (printf) - -import qualified WindowsEnv - -oldNewMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> WindowsEnv.VarValue -> WindowsEnv.VarValue -> 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" oldValue - newValueMsg = printf "\tNew value: %s\n" newValue - -newMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> WindowsEnv.VarValue -> 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" newValue - -wipeMessage :: WindowsEnv.Profile -> WindowsEnv.VarName -> String -wipeMessage profile name = - printf "Deleting variable '%s' from '%s'...\n" name (show profileKey) - where - profileKey = WindowsEnv.profileKeyPath profile diff --git a/apps/RemovePath.hs b/apps/RemovePath.hs deleted file mode 100644 index 7d0c104..0000000 --- a/apps/RemovePath.hs +++ /dev/null @@ -1,85 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module Main (main) where - -import Control.Monad (void, when) -import Control.Monad.Trans.Except (catchE, runExceptT, throwE) -import Data.List ((\\)) -import System.IO.Error (ioError, isDoesNotExistError) - -import Options.Applicative - -import qualified WindowsEnv - -import Prompt -import PromptMessage - -data Options = Options - { optName :: WindowsEnv.VarName - , optYes :: Bool - , optGlobal :: Bool - , optPaths :: [WindowsEnv.VarValue] - } deriving (Eq, Show) - -optionParser :: Parser Options -optionParser = Options - <$> optNameDesc - <*> optYesDesc - <*> optGlobalDesc - <*> optPathsDesc - where - optNameDesc = strOption - $ long "name" <> short 'n' - <> metavar "NAME" <> value "PATH" - <> help "Variable name ('PATH' by default)" - optYesDesc = switch - $ long "yes" <> short 'y' - <> help "Skip confirmation prompt" - optGlobalDesc = switch - $ long "global" <> short 'g' - <> help "Remove for all users" - optPathsDesc = many $ argument str - $ metavar "PATH" - <> help "Directories to remove" - -main :: IO () -main = execParser parser >>= removePath - where - parser = info (helper <*> optionParser) $ - fullDesc <> progDesc "Remove directories from your PATH" - -removePath :: Options -> IO () -removePath options = runExceptT doRemovePath >>= either ioError return - where - varName = optName options - pathsToRemove = optPaths options - - forAllUsers = optGlobal options - - skipPrompt = optYes options - - emptyIfMissing e - | isDoesNotExistError e = return "" - | otherwise = throwE e - - doRemovePath = do - removePathFrom WindowsEnv.CurrentUser - when forAllUsers $ - removePathFrom WindowsEnv.AllUsers - - removePathFrom profile = do - oldValue <- WindowsEnv.query profile varName `catchE` emptyIfMissing - let oldPaths = WindowsEnv.pathSplit oldValue - let newPaths = oldPaths \\ pathsToRemove - when (length oldPaths /= length newPaths) $ do - let newValue = WindowsEnv.pathJoin newPaths - let promptAnd = if skipPrompt - then withoutPrompt - else withPrompt $ oldNewMessage profile varName oldValue newValue - let engrave = WindowsEnv.engrave profile varName newValue - void $ promptAnd engrave diff --git a/apps/SetEnv.hs b/apps/SetEnv.hs deleted file mode 100644 index 87d7812..0000000 --- a/apps/SetEnv.hs +++ /dev/null @@ -1,72 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module Main (main) where - -import Control.Monad (void) -import Control.Monad.Trans.Except (runExceptT) -import System.IO.Error (ioError) - -import Options.Applicative - -import qualified WindowsEnv - -import Prompt -import PromptMessage - -data Options = Options - { optYes :: Bool - , optGlobal :: Bool - , optName :: WindowsEnv.VarName - , optValue :: WindowsEnv.VarValue - } deriving (Eq, Show) - -optionParser :: Parser Options -optionParser = Options - <$> optYesDesc - <*> optGlobalDesc - <*> optNameDesc - <*> optValueDesc - where - optYesDesc = switch - $ long "yes" <> short 'y' - <> help "Skip confirmation prompt" - optGlobalDesc = switch - $ long "global" <> short 'g' - <> help "Set for all users" - optNameDesc = argument str - $ metavar "NAME" - <> help "Variable name" - optValueDesc = argument str - $ metavar "VALUE" - <> help "Variable value" - -main :: IO () -main = execParser parser >>= setEnv - where - parser = info (helper <*> optionParser) $ - fullDesc <> progDesc "Define environment variables" - -setEnv :: Options -> IO () -setEnv options = runExceptT doSetEnv >>= either ioError return - where - varName = optName options - varValue = optValue options - - forAllUsers = optGlobal options - profile - | forAllUsers = WindowsEnv.AllUsers - | otherwise = WindowsEnv.CurrentUser - - skipPrompt = optYes options - promptAnd - | skipPrompt = withoutPrompt - | otherwise = withPrompt $ newMessage profile varName varValue - - engrave = WindowsEnv.engraveForce profile varName varValue - - doSetEnv = void $ promptAnd engrave diff --git a/apps/UnsetEnv.hs b/apps/UnsetEnv.hs deleted file mode 100644 index f60503b..0000000 --- a/apps/UnsetEnv.hs +++ /dev/null @@ -1,66 +0,0 @@ --- | --- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com> --- License : MIT --- Maintainer : Egor.Tensin@gmail.com --- Stability : experimental --- Portability : Windows-only - -module Main (main) where - -import Control.Monad (void) -import Control.Monad.Trans.Except (runExceptT) -import System.IO.Error (ioError) - -import Options.Applicative - -import qualified WindowsEnv - -import Prompt -import PromptMessage - -data Options = Options - { optYes :: Bool - , optGlobal :: Bool - , optName :: WindowsEnv.VarName - } deriving (Eq, Show) - -optionParser :: Parser Options -optionParser = Options - <$> optYesDesc - <*> optGlobalDesc - <*> optNameDesc - where - optYesDesc = switch - $ long "yes" <> short 'y' - <> help "Skip confirmation prompt" - optGlobalDesc = switch - $ long "global" <> short 'g' - <> help "Unset for all users" - optNameDesc = argument str - $ metavar "NAME" - <> help "Variable name" - -main :: IO () -main = execParser parser >>= unsetEnv - where - parser = info (helper <*> optionParser) $ - fullDesc <> progDesc "Delete environment variables" - -unsetEnv :: Options -> IO () -unsetEnv options = runExceptT doUnsetEnv >>= either ioError return - where - varName = optName options - - forAllUsers = optGlobal options - profile - | forAllUsers = WindowsEnv.AllUsers - | otherwise = WindowsEnv.CurrentUser - - skipPrompt = optYes options - promptAnd - | skipPrompt = withoutPrompt - | otherwise = withPrompt $ wipeMessage profile varName - - wipe = WindowsEnv.wipe profile varName - - doUnsetEnv = void $ promptAnd wipe |