From 8653780e04df0ad0d24b5be15a98bb0dcb01ca30 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 17 Jul 2016 15:47:51 +0300 Subject: add command line options to skip prompts --- apps/AddPath.hs | 24 ++++++++++++++++++------ apps/FixNtSymbolPath.hs | 40 +++++++++++++++++++++++++++++++++++----- apps/ListPath.hs | 3 ++- apps/RemovePath.hs | 28 ++++++++++++++++++++-------- apps/SetEnv.hs | 26 +++++++++++++++++++------- apps/UnsetEnv.hs | 26 +++++++++++++++++++------- 6 files changed, 113 insertions(+), 34 deletions(-) (limited to 'apps') diff --git a/apps/AddPath.hs b/apps/AddPath.hs index aeb5ee5..40fa785 100644 --- a/apps/AddPath.hs +++ b/apps/AddPath.hs @@ -16,6 +16,7 @@ import qualified Environment data Options = Options { optName :: String + , optYes :: Bool , optGlobal :: Bool , optPaths :: [String] } deriving (Eq, Show) @@ -23,18 +24,22 @@ data Options = Options options :: Parser Options options = Options <$> optNameDesc + <*> optYesDesc <*> optGlobalDesc <*> optPathsDesc where optNameDesc = strOption $ long "name" <> short 'n' <> metavar "NAME" <> value "PATH" <> - help "Specify variable name ('PATH' by default)" + help "Variable name ('PATH' by default)" + optYesDesc = switch $ + long "yes" <> short 'y' <> + help "Skip confirmation prompt" optGlobalDesc = switch $ long "global" <> short 'g' <> - help "Whether to add for all users" + help "Add for all users" optPathsDesc = many $ argument str $ metavar "PATH" <> - help "Directory path(s)" + help "Directories to add" main :: IO () main = execParser parser >>= addPath @@ -49,9 +54,16 @@ addPath options = do let newPaths = union oldPaths pathsToAdd when (length oldPaths /= length newPaths) $ do let newValue = Environment.pathJoin newPaths - Environment.engraveWithPrompt env varName newValue + engrave env varName newValue where - env | optGlobal options = Environment.AllUsers - | otherwise = Environment.CurrentUser varName = optName options pathsToAdd = optPaths options + + forAllUsers = optGlobal options + env | forAllUsers = Environment.AllUsers + | otherwise = Environment.CurrentUser + + skipPrompt = optYes options + engrave + | skipPrompt = Environment.engrave + | otherwise = Environment.engraveWithPrompt diff --git a/apps/FixNtSymbolPath.hs b/apps/FixNtSymbolPath.hs index f3d465e..636e93b 100644 --- a/apps/FixNtSymbolPath.hs +++ b/apps/FixNtSymbolPath.hs @@ -12,8 +12,27 @@ import Data.Maybe (fromMaybe) import System.Directory (createDirectoryIfMissing, getCurrentDirectory) import System.FilePath (combine) +import Options.Applicative + import qualified Environment +data Options = Options + { optYes :: Bool + , optGlobal :: Bool + } deriving (Eq, Show) + +options :: Parser Options +options = Options + <$> optYesDesc + <*> optGlobalDesc + where + optYesDesc = switch $ + long "yes" <> short 'y' <> + help "Skip confirmation prompt" + optGlobalDesc = switch $ + long "global" <> short 'g' <> + help "Set up for all users" + getRemoteSymbolsDirectoryPath :: IO String getRemoteSymbolsDirectoryPath = do localPath <- getLocalPath @@ -32,19 +51,30 @@ getPdbsDirectoryPath = do createDirectoryIfMissing True path return path -fixNtSymbolPath :: IO () -fixNtSymbolPath = do +fixNtSymbolPath :: Options -> IO () +fixNtSymbolPath options = do oldValue <- Environment.query env varName let oldPaths = Environment.pathSplit $ fromMaybe "" oldValue pathsToAdd <- addPaths let newPaths = union oldPaths pathsToAdd when (length oldPaths /= length newPaths) $ do let newValue = Environment.pathJoin newPaths - Environment.engrave env varName newValue + engrave env varName newValue where - env = Environment.CurrentUser varName = "_NT_SYMBOL_PATH" addPaths = sequence [getRemoteSymbolsDirectoryPath, getPdbsDirectoryPath] + forAllUsers = optGlobal options + env | forAllUsers = Environment.AllUsers + | otherwise = Environment.CurrentUser + + skipPrompt = optYes options + engrave + | skipPrompt = Environment.engrave + | otherwise = Environment.engraveWithPrompt + main :: IO () -main = fixNtSymbolPath +main = execParser parser >>= fixNtSymbolPath + where + parser = info (helper <*> options) $ + fullDesc <> progDesc "Set up your _NT_SYMBOL_PATH" diff --git a/apps/ListPath.hs b/apps/ListPath.hs index 469fbba..e0cbefe 100644 --- a/apps/ListPath.hs +++ b/apps/ListPath.hs @@ -24,7 +24,7 @@ options = Options <$> optNameDesc where optNameDesc = strOption $ long "name" <> short 'n' <> metavar "NAME" <> value "PATH" <> - help "Specify variable name ('PATH' by default)" + help "Variable name ('PATH' by default)" main :: IO () main = execParser parser >>= listPath @@ -40,6 +40,7 @@ listPath options = do where varName = optName options getEnv = liftM (fromMaybe "") . lookupEnv + printPath p = do exists <- doesDirectoryExist p putStrLn $ (if exists then "+" else "-") ++ " " ++ p diff --git a/apps/RemovePath.hs b/apps/RemovePath.hs index 87c60a4..8c2bcef 100644 --- a/apps/RemovePath.hs +++ b/apps/RemovePath.hs @@ -16,24 +16,29 @@ import qualified Environment data Options = Options { optName :: String + , optYes :: Bool , optGlobal :: Bool , optPaths :: [String] } deriving (Eq, Show) options = Options <$> optNameDesc + <*> optYesDesc <*> optGlobalDesc <*> optPathsDesc where optNameDesc = strOption $ long "name" <> short 'n' <> metavar "NAME" <> value "PATH" <> - help "Specify variable name ('PATH' by default)" + help "Variable name ('PATH' by default)" + optYesDesc = switch $ + long "yes" <> short 'y' <> + help "Skip confirmation prompt" optGlobalDesc = switch $ long "global" <> short 'g' <> - help "Whether to remove for all users" + help "Remove for all users" optPathsDesc = many $ argument str $ metavar "PATH" <> - help "Directory path(s)" + help "Directories to remove" main :: IO () main = execParser parser >>= removePath @@ -43,18 +48,25 @@ main = execParser parser >>= removePath removePath :: Options -> IO () removePath options = do - removePathFrom Environment.CurrentUser options - when (optGlobal options) $ do - removePathFrom Environment.AllUsers options + removePathFrom Environment.CurrentUser + when forAllUsers $ do + removePathFrom Environment.AllUsers where varName = optName options pathsToRemove = optPaths options - removePathFrom env options = do + forAllUsers = optGlobal options + + removePathFrom env = 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 + engrave env varName newValue + + skipPrompt = optYes options + engrave + | skipPrompt = Environment.engrave + | otherwise = Environment.engraveWithPrompt diff --git a/apps/SetEnv.hs b/apps/SetEnv.hs index 293b062..18e369f 100644 --- a/apps/SetEnv.hs +++ b/apps/SetEnv.hs @@ -11,20 +11,25 @@ import Options.Applicative hiding (value) import qualified Environment data Options = Options - { optGlobal :: Bool + { optYes :: Bool + , optGlobal :: Bool , optName :: String , optValue :: String } deriving (Eq, Show) options :: Parser Options options = Options - <$> optGlobalDesc + <$> optYesDesc + <*> optGlobalDesc <*> optNameDesc <*> optValueDesc where + optYesDesc = switch $ + long "yes" <> short 'y' <> + help "Skip confirmation prompt" optGlobalDesc = switch $ long "global" <> short 'g' <> - help "Whether to set for all users" + help "Set for all users" optNameDesc = argument str $ metavar "NAME" <> help "Variable name" @@ -36,12 +41,19 @@ main :: IO () main = execParser parser >>= setEnv where parser = info (helper <*> options) $ - fullDesc <> progDesc "Set environment variables" + fullDesc <> progDesc "Set environment variable" setEnv :: Options -> IO () -setEnv options = Environment.engraveWithPrompt env varName varValue +setEnv options = engrave env varName varValue where - env | optGlobal options = Environment.AllUsers - | otherwise = Environment.CurrentUser varName = optName options varValue = optValue options + + forAllUsers = optGlobal options + env | forAllUsers = Environment.AllUsers + | otherwise = Environment.CurrentUser + + skipPrompt = optYes options + engrave + | skipPrompt = Environment.engrave + | otherwise = Environment.engraveWithPrompt diff --git a/apps/UnsetEnv.hs b/apps/UnsetEnv.hs index f7bf0b2..14111d2 100644 --- a/apps/UnsetEnv.hs +++ b/apps/UnsetEnv.hs @@ -11,18 +11,23 @@ import Options.Applicative import qualified Environment data Options = Options - { optGlobal :: Bool + { optYes :: Bool + , optGlobal :: Bool , optName :: String } deriving (Eq, Show) options :: Parser Options options = Options - <$> optGlobalDesc + <$> optYes + <*> optGlobalDesc <*> optNameDesc where + optYes = switch $ + long "yes" <> short 'y' <> + help "Skip confirmation prompt" optGlobalDesc = switch $ long "global" <> short 'g' <> - help "Whether to unset for all users" + help "Unset for all users" optNameDesc = argument str $ metavar "NAME" <> help "Variable name" @@ -31,11 +36,18 @@ main :: IO () main = execParser parser >>= unsetEnv where parser = info (helper <*> options) $ - fullDesc <> progDesc "Unset environment variables" + fullDesc <> progDesc "Unset environment variable" unsetEnv :: Options -> IO () -unsetEnv options = Environment.wipeWithPrompt env varName +unsetEnv options = wipe env varName where - env | optGlobal options = Environment.AllUsers - | otherwise = Environment.CurrentUser varName = optName options + + forAllUsers = optGlobal options + env | forAllUsers = Environment.AllUsers + | otherwise = Environment.CurrentUser + + skipPrompt = optYes options + wipe + | skipPrompt = Environment.wipe + | otherwise = Environment.wipeWithPrompt -- cgit v1.2.3