Skip to content

Commit 091ee93

Browse files
committed
find gpg executables 1st before calling them
1 parent 8dae772 commit 091ee93

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/Stack/Sig/GPG.hs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module Stack.Sig.GPG (signPackage, verifyFile) where
1919
import Control.Applicative ((<$>))
2020
#endif
2121

22-
import Control.Exception (catch, SomeException)
2322
import Control.Monad.Catch (MonadThrow, throwM)
2423
import Control.Monad.IO.Class (MonadIO, liftIO)
2524
import qualified Data.ByteString.Char8 as C
@@ -28,6 +27,7 @@ import Data.Monoid ((<>))
2827
import qualified Data.Text as T
2928
import Path
3029
import Stack.Types
30+
import System.Directory (findExecutable)
3131
import System.Exit (ExitCode(..))
3232
import System.Process (readProcessWithExitCode)
3333

@@ -72,16 +72,14 @@ verifyFile (Signature signature) path = do
7272

7373
-- | Try to execute `gpg2` but fallback to `gpg` (as a backup)
7474
gpg
75-
:: (Monad m, MonadIO m)
75+
:: (Monad m, MonadIO m, MonadThrow m)
7676
=> [String] -> String -> m (ExitCode, String, String)
77-
gpg args stdin =
78-
liftIO
79-
(catch
80-
(readProcessWithExitCode "gpg2" args stdin)
81-
(oops
82-
(catch
83-
(readProcessWithExitCode "gpg" args stdin)
84-
(return . (, [], [])))))
85-
where
86-
oops :: IO a -> SomeException -> IO a
87-
oops = const
77+
gpg args stdin = do
78+
mGpg2Path <- liftIO (findExecutable "gpg2")
79+
case mGpg2Path of
80+
Just _ -> liftIO (readProcessWithExitCode "gpg2" args stdin)
81+
Nothing -> do
82+
mGpgPath <- liftIO (findExecutable "gpg")
83+
case mGpgPath of
84+
Just _ -> liftIO (readProcessWithExitCode "gpg" args stdin)
85+
Nothing -> throwM GPGNotFoundException

src/Stack/Types/Sig.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ newtype Aeson a = Aeson
8282
-- | Exceptions
8383
data SigException
8484
= GPGFingerprintException String
85+
| GPGNotFoundException
8586
| GPGSignException String
8687
| GPGVerifyException String
8788
| SigInvalidSDistTarBall
@@ -94,6 +95,7 @@ instance Exception SigException
9495
instance Show SigException where
9596
show (GPGFingerprintException e) =
9697
"Error extracting a GPG fingerprint " <> e
98+
show GPGNotFoundException = "Unable to find gpg2 or gpg executable"
9799
show (GPGSignException e) = "Error signing with GPG " <> e
98100
show (GPGVerifyException e) = "Error verifying with GPG " <> e
99101
show SigNoProjectRootException = "Missing Project Root"

0 commit comments

Comments
 (0)