Handle failures properly

Attoparsec error are discarded since they are basically useless.
This commit is contained in:
rnhmjoj 2015-07-07 20:59:53 +02:00
parent 75d640063f
commit 20ef16e3f2
2 changed files with 12 additions and 8 deletions

View File

@ -32,15 +32,19 @@ fuzzy x = filter (any (isInfixOf x) . strings)
-- Keychain access -- -- Keychain access --
keychainList :: IO [FilePath] keychainList :: IO [FilePath]
keychainList = keychainList = do
runParser parseKeychainList <$> raw <- readProcess "security" ["list-keychains"] ""
readProcess "security" ["list-keychains"] "" case runParser parseKeychainList raw of
Just list -> return list
Nothing -> error "failed to parse active keychains list"
getKeychain :: IO Keychain getKeychain :: IO Keychain
getKeychain = do getKeychain = do
paths <- filter ((/="System") . takeBaseName) <$> keychainList paths <- filter ((/="System") . takeBaseName) <$> keychainList
dump <- readProcess "security" ("dump-keychain" : "-d" : paths) "" raw <- readProcess "security" ("dump-keychain" : "-d" : paths) ""
return (runParser parseKeychain dump) case runParser parseKeychain raw of
Just items -> return items
Nothing -> error "failed to parse keychain"
sendClipboard :: String -> IO () sendClipboard :: String -> IO ()
sendClipboard text = sendClipboard text =

View File

@ -14,11 +14,11 @@ import Data.Hex (unhex)
import Data.Time import Data.Time
runParser :: Monoid a => Parser a -> String -> a runParser :: Parser a -> String -> Maybe a
runParser parser = handle . parseOnly parser . pack runParser parser = handle . parseOnly parser . pack
where where
handle (Left _) = mempty handle (Left _) = Nothing
handle (Right x) = x handle (Right x) = Just x
-- Parsers -- -- Parsers --