implemented service renaming

This commit is contained in:
Jonathan Lamothe 2018-12-31 15:27:36 -05:00
parent 8b71454445
commit f686fb7412
2 changed files with 43 additions and 7 deletions

View File

@ -70,20 +70,19 @@ mainMenu =
addPassword :: S.StateT Status IO () addPassword :: S.StateT Status IO ()
addPassword = do addPassword = do
svc <- req $ prompt "service name: " reqResp svc <- req $ prompt "service name: " reqResp
db <- S.gets $ view database ifServExists svc
if pwHasService svc db (do
then do
edit <- req (confirm $ edit <- req (confirm $
"The service already exists in the database.\n" ++ "The service already exists in the database.\n" ++
"Would you like to edit it?") "Would you like to edit it?")
if edit if edit
then servMenu svc then servMenu svc
else mainMenu else mainMenu)
else do (do
d <- buildData d <- buildData
setService svc d setService svc d
showPass svc showPass svc
servMenu svc servMenu svc)
viewEditMenu :: S.StateT Status IO () viewEditMenu :: S.StateT Status IO ()
viewEditMenu = menu "View/Edit Password" viewEditMenu = menu "View/Edit Password"
@ -145,6 +144,7 @@ servMenu x = menu x
[ ( "show password", showPass x >> servMenu x ) [ ( "show password", showPass x >> servMenu x )
, ( "edit password", editPassMenu x ) , ( "edit password", editPassMenu x )
, ( "remove service", removeServ x ) , ( "remove service", removeServ x )
, ( "rename service", renameServ x )
, ( "back", mainMenu ) , ( "back", mainMenu )
] ]
@ -161,10 +161,34 @@ removeServ x = do
"Are you sure you want to delete the password for " ++ x ++ "?" "Are you sure you want to delete the password for " ++ x ++ "?"
if go if go
then do then do
S.modify $ over database $ pwRemoveService x removeServ' x
mainMenu mainMenu
else servMenu x else servMenu x
removeServ' :: String -> S.StateT Status IO ()
removeServ' = S.modify . over database . pwRemoveService
renameServ :: String -> S.StateT Status IO ()
renameServ x = do
y <- req $ prompt "new service name: " reqResp
if x == y
then servMenu x
else ifServExists y
(do
overwrite <- req $ confirm $
y ++ " already exists.\n" ++
"Would you like to overwrite it?"
if overwrite
then renameServ' x y
else servMenu x)
(renameServ' x y)
renameServ' :: String -> String -> S.StateT Status IO ()
renameServ' x y = withService x mainMenu $ \d -> do
removeServ' x
setService y d
servMenu y
changeSalt :: String -> S.StateT Status IO () changeSalt :: String -> S.StateT Status IO ()
changeSalt x = withService x mainMenu $ \d -> do changeSalt x = withService x mainMenu $ \d -> do
salt <- run newPWSalt salt <- run newPWSalt

View File

@ -24,6 +24,7 @@ module Util
( menu ( menu
, run , run
, withService , withService
, ifServExists
, setService , setService
, req , req
, tryReq , tryReq
@ -82,6 +83,17 @@ withService srv fb act = do
Nothing -> fb Nothing -> fb
Just x -> act x Just x -> act x
ifServExists
:: String
-> S.StateT Status IO a
-> S.StateT Status IO a
-> S.StateT Status IO a
ifServExists s x y = do
db <- S.gets $ view database
if pwHasService s db
then x
else y
setService :: String -> PWData -> S.StateT Status IO () setService :: String -> PWData -> S.StateT Status IO ()
setService k = S.modify . over database . pwSetService k setService k = S.modify . over database . pwSetService k