diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-07-18 00:43:21 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-07-18 00:43:21 +0300 |
commit | be9299d2dc8e0a9e76fe6f07cabb15c144999710 (patch) | |
tree | 91a68cd35e78c612eb358181f24e91fda904e766 | |
parent | put modules to 'Windows' (diff) | |
download | windows-env-be9299d2dc8e0a9e76fe6f07cabb15c144999710.tar.gz windows-env-be9299d2dc8e0a9e76fe6f07cabb15c144999710.zip |
refactoring
Diffstat (limited to '')
-rw-r--r-- | apps/AddPath.hs | 20 | ||||
-rw-r--r-- | apps/Banner.hs | 31 | ||||
-rw-r--r-- | apps/FixNtSymbolPath.hs | 21 | ||||
-rw-r--r-- | apps/Prompt.hs (renamed from apps/Utils.hs) | 22 | ||||
-rw-r--r-- | apps/RemovePath.hs | 13 | ||||
-rw-r--r-- | apps/SetEnv.hs | 19 | ||||
-rw-r--r-- | apps/UnsetEnv.hs | 19 | ||||
-rw-r--r-- | windows-env.cabal | 6 |
8 files changed, 86 insertions, 65 deletions
diff --git a/apps/AddPath.hs b/apps/AddPath.hs index db0dcc2..343207c 100644 --- a/apps/AddPath.hs +++ b/apps/AddPath.hs @@ -9,12 +9,12 @@ module Main (main) where import Control.Monad (void, when) import Data.List (union) import Data.Maybe (fromMaybe) -import Text.Printf (printf) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optName :: Env.VarName @@ -56,18 +56,18 @@ addPath options = do let newPaths = union oldPaths pathsToAdd when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - void $ prompt promptBanner $ Env.engrave profile varName newValue + let banner = engraveBanner profile varName oldValue newValue + void $ prompt banner $ Env.engrave profile varName newValue where varName = optName options pathsToAdd = optPaths options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/Banner.hs b/apps/Banner.hs new file mode 100644 index 0000000..c25741b --- /dev/null +++ b/apps/Banner.hs @@ -0,0 +1,31 @@ +{- + - Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> + - This file is licensed under the terms of the MIT License. + - See LICENSE.txt for details. +-} + +module Banner + ( engraveBanner + , wipeBanner + ) where + +import Data.Maybe (fromJust, isJust) +import Text.Printf (printf) + +import qualified Windows.Environment as Env + +engraveBanner :: Env.Profile -> Env.VarName -> Maybe Env.VarValue -> Env.VarValue -> String +engraveBanner profile name oldValue newValue = + warning ++ valuesStr + where + warning = printf "Saving variable '%s' to '%s'...\n" name (Env.profileKeyPath profile) + valuesStr + | isJust oldValue = oldValueStr ++ newValueStr + | otherwise = theValueStr + oldValueStr = printf "\tOld value: %s\n" $ fromJust oldValue + newValueStr = printf "\tNew value: %s\n" newValue + theValueStr = printf "\tValue: %s\n" newValue + +wipeBanner :: Env.Profile -> Env.VarName -> String +wipeBanner profile name = + printf "Deleting variable '%s' from '%s'...\n" name (Env.profileKeyPath profile) diff --git a/apps/FixNtSymbolPath.hs b/apps/FixNtSymbolPath.hs index 8c999a8..219ceb2 100644 --- a/apps/FixNtSymbolPath.hs +++ b/apps/FixNtSymbolPath.hs @@ -15,7 +15,8 @@ import System.FilePath (combine) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -72,22 +73,22 @@ fixNtSymbolPath options = do let newPaths = union oldPaths $ dirPaths remoteDirs when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - confirmed <- prompt promptBanner $ Env.engrave profile varName newValue - when confirmed $ + let banner = engraveBanner profile varName oldValue newValue + agreed <- prompt banner $ Env.engrave profile varName newValue + when agreed $ createDirs localDirs where varName = "_NT_SYMBOL_PATH" forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt main :: IO () main = execParser parser >>= fixNtSymbolPath diff --git a/apps/Utils.hs b/apps/Prompt.hs index e34950f..97c23fa 100644 --- a/apps/Utils.hs +++ b/apps/Prompt.hs @@ -4,21 +4,14 @@ - See LICENSE.txt for details. -} -module Utils +module Prompt ( withPrompt , withoutPrompt - - , engraveBanner - , wipeBanner ) where import Control.Monad (liftM, void, when) -import Data.Maybe (fromJust, isJust) import Data.Char (toLower) import System.IO (hFlush, stdout) -import Text.Printf (printf) - -import Windows.Environment (Profile, profileKeyPath, VarName, VarValue) prompt :: String -> IO String prompt banner = do @@ -51,16 +44,3 @@ withPrompt banner m = do withoutPrompt :: IO a -> IO Bool withoutPrompt m = m >> return True - -engraveBanner :: Profile -> VarName -> Maybe VarValue -> VarValue -> String -engraveBanner profile name oldValue newValue = - header ++ values - where - header = printf "Saving variable '%s' to '%s'...\n" name (profileKeyPath profile) - values = if isJust oldValue - then printf "\tOld value: %s\n\tNew value: %s\n" (fromJust oldValue) newValue - else printf "\tValue: %s\n" newValue - -wipeBanner :: Profile -> VarName -> String -wipeBanner profile name = - printf "Deleting variable '%s' from '%s'...\n" name (profileKeyPath profile) diff --git a/apps/RemovePath.hs b/apps/RemovePath.hs index ecc56c0..871ebef 100644 --- a/apps/RemovePath.hs +++ b/apps/RemovePath.hs @@ -13,7 +13,8 @@ import Data.Maybe (fromJust, isJust) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optName :: Env.VarName @@ -65,10 +66,10 @@ removePath options = do let newPaths = oldPaths \\ pathsToRemove when (length oldPaths /= length newPaths) $ do let newValue = Env.pathJoin newPaths - let promptBanner = Utils.engraveBanner profile varName oldValue newValue - void $ prompt promptBanner $ Env.engrave profile varName newValue + let banner = engraveBanner profile varName oldValue newValue + void $ prompt banner $ Env.engrave profile varName newValue skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/SetEnv.hs b/apps/SetEnv.hs index a48fbe6..e94e350 100644 --- a/apps/SetEnv.hs +++ b/apps/SetEnv.hs @@ -11,7 +11,8 @@ import Control.Monad (void) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -47,19 +48,19 @@ main = execParser parser >>= setEnv fullDesc <> progDesc "Set environment variable" setEnv :: Options -> IO () -setEnv options = void $ prompt confirmationBanner $ Env.engrave profile varName varValue +setEnv options = void $ prompt banner $ Env.engrave profile varName varValue where - confirmationBanner = Utils.engraveBanner profile varName Nothing varValue + banner = engraveBanner profile varName Nothing varValue varName = optName options varValue = optValue options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/apps/UnsetEnv.hs b/apps/UnsetEnv.hs index 88101d9..eebef00 100644 --- a/apps/UnsetEnv.hs +++ b/apps/UnsetEnv.hs @@ -11,7 +11,8 @@ import Control.Monad (void) import Options.Applicative import qualified Windows.Environment as Env -import qualified Utils +import Banner +import Prompt data Options = Options { optYes :: Bool @@ -42,18 +43,18 @@ main = execParser parser >>= unsetEnv fullDesc <> progDesc "Unset environment variable" unsetEnv :: Options -> IO () -unsetEnv options = void $ prompt confirmationBanner $ Env.wipe profile varName +unsetEnv options = void $ prompt banner $ Env.wipe profile varName where - confirmationBanner = Utils.wipeBanner profile varName + banner = wipeBanner profile varName varName = optName options forAllUsers = optGlobal options - profile = if forAllUsers - then Env.AllUsers - else Env.CurrentUser + profile + | forAllUsers = Env.AllUsers + | otherwise = Env.CurrentUser skipPrompt = optYes options - prompt = if skipPrompt - then const Utils.withoutPrompt - else Utils.withPrompt + prompt + | skipPrompt = const withoutPrompt + | otherwise = withPrompt diff --git a/windows-env.cabal b/windows-env.cabal index d34684f..5a2857d 100644 --- a/windows-env.cabal +++ b/windows-env.cabal @@ -24,6 +24,7 @@ library executable add_path hs-source-dirs: apps main-is: AddPath.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , optparse-applicative @@ -33,6 +34,7 @@ executable add_path executable fix_nt_symbol_path hs-source-dirs: apps main-is: FixNtSymbolPath.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base, directory, filepath , optparse-applicative @@ -42,6 +44,7 @@ executable fix_nt_symbol_path executable list_path hs-source-dirs: apps main-is: ListPath.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base, directory , optparse-applicative @@ -51,6 +54,7 @@ executable list_path executable remove_path hs-source-dirs: apps main-is: RemovePath.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , optparse-applicative @@ -60,6 +64,7 @@ executable remove_path executable set_env hs-source-dirs: apps main-is: SetEnv.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , optparse-applicative @@ -69,6 +74,7 @@ executable set_env executable unset_env hs-source-dirs: apps main-is: UnsetEnv.hs + other-modules: Banner, Prompt ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , optparse-applicative |