{-# LANGUAGE OverloadedStrings #-} module System.Taffybar.Auth where import Control.Arrow import Control.Monad.IO.Class import Data.Maybe import System.Taffybar.Util import Text.Regex fieldRegex :: Regex fieldRegex :: Regex fieldRegex = String -> Bool -> Bool -> Regex mkRegexWithOpts String "^(.*?): *(.*?)$" Bool True Bool True passGet :: MonadIO m => String -> m (Either String (String, [(String, String)])) passGet :: forall (m :: * -> *). MonadIO m => String -> m (Either String (String, [(String, String)])) passGet String credentialName = (String -> (String, [(String, String)])) -> Either String String -> Either String (String, [(String, String)]) forall b c d. (b -> c) -> Either d b -> Either d c forall (a :: * -> * -> *) b c d. ArrowChoice a => a b c -> a (Either d b) (Either d c) right ([String] -> (String, [(String, String)]) getPassComponents ([String] -> (String, [(String, String)])) -> (String -> [String]) -> String -> (String, [(String, String)]) forall b c a. (b -> c) -> (a -> b) -> a -> c . String -> [String] lines) (Either String String -> Either String (String, [(String, String)])) -> m (Either String String) -> m (Either String (String, [(String, String)])) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> [String] -> m (Either String String) forall (m :: * -> *). MonadIO m => [String] -> m (Either String String) runCommandFromPath [String "pass", String "show", String credentialName] where getPassComponents :: [String] -> (String, [(String, String)]) getPassComponents [String] passLines = let entries :: [(String, String)] entries = ([String] -> (String, String)) -> [[String]] -> [(String, String)] forall a b. (a -> b) -> [a] -> [b] map [String] -> (String, String) forall {b}. IsString b => [b] -> (b, b) buildEntry ([[String]] -> [(String, String)]) -> [[String]] -> [(String, String)] forall a b. (a -> b) -> a -> b $ [Maybe [String]] -> [[String]] forall a. [Maybe a] -> [a] catMaybes ([Maybe [String]] -> [[String]]) -> [Maybe [String]] -> [[String]] forall a b. (a -> b) -> a -> b $ Regex -> String -> Maybe [String] matchRegex Regex fieldRegex (String -> Maybe [String]) -> [String] -> [Maybe [String]] forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> [String] -> [String] forall a. HasCallStack => [a] -> [a] tail [String] passLines buildEntry :: [b] -> (b, b) buildEntry [b fieldName, b fieldValue] = (b fieldName, b fieldValue) buildEntry [b] _ = (b "", b "") in ([String] -> String forall a. HasCallStack => [a] -> a head [String] passLines, [(String, String)] entries)