aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apps/AddPath.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-12-12 20:43:42 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-12-12 20:43:42 +0300
commitf1a4c2b98f1707c09e17ddd07cb25d6e1cfe4022 (patch)
tree55877ee2e87e89b552b194966292f6f46eb0982f /apps/AddPath.hs
parentfix export lists (diff)
downloadwindows-env-f1a4c2b98f1707c09e17ddd07cb25d6e1cfe4022.tar.gz
windows-env-f1a4c2b98f1707c09e17ddd07cb25d6e1cfe4022.zip
use monad transformers
Diffstat (limited to 'apps/AddPath.hs')
-rw-r--r--apps/AddPath.hs32
1 files changed, 18 insertions, 14 deletions
diff --git a/apps/AddPath.hs b/apps/AddPath.hs
index a32244c..2f1870c 100644
--- a/apps/AddPath.hs
+++ b/apps/AddPath.hs
@@ -7,6 +7,8 @@
module Main (main) where
import Control.Monad (void, when)
+import Control.Monad.Trans.Class
+import Control.Monad.Trans.Except
import Data.List (union)
import System.IO.Error (ioError, isDoesNotExistError)
@@ -52,16 +54,8 @@ main = execParser parser >>= addPath
addPath :: Options -> IO ()
addPath options = do
- oldValue <- Env.query profile varName >>= emptyIfMissing
- let oldPaths = Env.pathSplit oldValue
- let newPaths = oldPaths `union` pathsToAdd
- when (length oldPaths /= length newPaths) $ do
- let newValue = Env.pathJoin newPaths
- let promptAnd = if skipPrompt
- then withoutPrompt
- else withPrompt $ engraveMessage profile varName oldValue newValue
- let engrave = Env.engrave profile varName newValue
- void $ promptAnd engrave
+ ret <- runExceptT $ doAddPath
+ either ioError return ret
where
varName = optName options
pathsToAdd = optPaths options
@@ -73,7 +67,17 @@ addPath options = do
skipPrompt = optYes options
- emptyIfMissing (Left e)
- | isDoesNotExistError e = return ""
- | otherwise = ioError e
- emptyIfMissing (Right s) = return s
+ emptyIfMissing e | isDoesNotExistError e = return ""
+ | otherwise = throwE e
+
+ doAddPath = do
+ oldValue <- Env.query profile varName `catchE` emptyIfMissing
+ let oldPaths = Env.pathSplit oldValue
+ let newPaths = oldPaths `union` pathsToAdd
+ when (length oldPaths /= length newPaths) $ do
+ let newValue = Env.pathJoin newPaths
+ let promptAnd = if skipPrompt
+ then withoutPrompt
+ else withPrompt $ engraveMessage profile varName oldValue newValue
+ let engrave = Env.engrave profile varName newValue
+ lift $ void $ promptAnd $ runExceptT engrave