module Proteome.System.Path where

import Path (Abs, File, Path, parseRelFile)
import Path.IO (findExecutable)

import Proteome.Data.GrepError (GrepError)
import qualified Proteome.Data.GrepError as GrepError (GrepError (NoSuchExecutable, NotInPath))

findExe ::
  Members [Stop GrepError, Embed IO] r =>
  Text ->
  Sem r (Path Abs File)
findExe :: forall (r :: EffectRow).
Members '[Stop GrepError, Embed IO] r =>
Text -> Sem r (Path Abs File)
findExe Text
exe = do
  Path Rel File
path <- GrepError
-> Either SomeException (Path Rel File) -> Sem r (Path Rel File)
forall err' (r :: EffectRow) err a.
Member (Stop err') r =>
err' -> Either err a -> Sem r a
stopEitherAs GrepError
parseError (FilePath -> Either SomeException (Path Rel File)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel File)
parseRelFile (Text -> FilePath
forall a. ToString a => a -> FilePath
toString Text
exe))
  GrepError -> Maybe (Path Abs File) -> Sem r (Path Abs File)
forall err (r :: EffectRow) a.
Member (Stop err) r =>
err -> Maybe a -> Sem r a
stopNote GrepError
notInPath (Maybe (Path Abs File) -> Sem r (Path Abs File))
-> Sem r (Maybe (Path Abs File)) -> Sem r (Path Abs File)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Path Rel File -> Sem r (Maybe (Path Abs File))
forall (m :: * -> *).
MonadIO m =>
Path Rel File -> m (Maybe (Path Abs File))
findExecutable Path Rel File
path
  where
    parseError :: GrepError
parseError =
      Text -> GrepError
GrepError.NoSuchExecutable Text
exe
    notInPath :: GrepError
notInPath =
      Text -> GrepError
GrepError.NotInPath Text
exe