|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DataKinds #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | +{-# LANGUAGE FlexibleContexts #-} |
| 5 | +{-# LANGUAGE OverloadedStrings #-} |
| 6 | +{-# LANGUAGE TemplateHaskell #-} |
| 7 | +{-# LANGUAGE QuasiQuotes #-} |
| 8 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 9 | +{-# LANGUAGE RankNTypes #-} |
| 10 | + |
| 11 | +module GHCup.OptParse.HealthCheck where |
| 12 | + |
| 13 | + |
| 14 | +import GHCup |
| 15 | +import GHCup.Errors |
| 16 | +import GHCup.Types |
| 17 | +import GHCup.Prelude.Logger |
| 18 | +import GHCup.Prelude.String.QQ |
| 19 | + |
| 20 | +#if !MIN_VERSION_base(4,13,0) |
| 21 | +import Control.Monad.Fail ( MonadFail ) |
| 22 | +#endif |
| 23 | +import Control.Monad.Reader |
| 24 | +import Control.Monad.Trans.Resource |
| 25 | +import Data.Functor |
| 26 | +import Haskus.Utils.Variant.Excepts |
| 27 | +import Options.Applicative hiding ( style ) |
| 28 | +import Prelude hiding ( appendFile ) |
| 29 | +import System.Exit |
| 30 | + |
| 31 | +import qualified Data.Text as T |
| 32 | +import Control.Exception.Safe (MonadMask) |
| 33 | +import Text.PrettyPrint.Annotated.HughesPJClass (prettyShow) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + --------------- |
| 40 | + --[ Options ]-- |
| 41 | + --------------- |
| 42 | + |
| 43 | + |
| 44 | +data HealtCheckOptions = HealtCheckOptions |
| 45 | + { hcOffline :: Bool |
| 46 | + } deriving (Eq, Show) |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + --------------- |
| 51 | + --[ Parsers ]-- |
| 52 | + --------------- |
| 53 | + |
| 54 | + |
| 55 | +hcP :: Parser HealtCheckOptions |
| 56 | +hcP = |
| 57 | + HealtCheckOptions |
| 58 | + <$> |
| 59 | + switch |
| 60 | + (short 'o' <> long "offline" <> help "Only do checks that don't require internet") |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + -------------- |
| 65 | + --[ Footer ]-- |
| 66 | + -------------- |
| 67 | + |
| 68 | + |
| 69 | +hcFooter :: String |
| 70 | +hcFooter = [s|Discussion: |
| 71 | + Performs various health checks. Good for attaching to bug reports.|] |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + --------------------------- |
| 77 | + --[ Effect interpreters ]-- |
| 78 | + --------------------------- |
| 79 | + |
| 80 | + |
| 81 | +type HCEffects = '[ DigestError |
| 82 | + , ContentLengthError |
| 83 | + , GPGError |
| 84 | + , DownloadFailed |
| 85 | + , NoDownload |
| 86 | + ] |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +runHC :: MonadUnliftIO m |
| 91 | + => (ReaderT LeanAppState m (VEither HCEffects a) -> m (VEither HCEffects a)) |
| 92 | + -> Excepts HCEffects (ResourceT (ReaderT LeanAppState m)) a |
| 93 | + -> m (VEither HCEffects a) |
| 94 | +runHC runLeanAppState = |
| 95 | + runLeanAppState |
| 96 | + . runResourceT |
| 97 | + . runE |
| 98 | + @HCEffects |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + ------------------ |
| 103 | + --[ Entrypoint ]-- |
| 104 | + ------------------ |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +hc :: ( Monad m |
| 109 | + , MonadMask m |
| 110 | + , MonadUnliftIO m |
| 111 | + , MonadFail m |
| 112 | + ) |
| 113 | + => HealtCheckOptions |
| 114 | + -> (forall a. ReaderT LeanAppState m (VEither HCEffects a) -> m (VEither HCEffects a)) |
| 115 | + -> (ReaderT LeanAppState m () -> m ()) |
| 116 | + -> m ExitCode |
| 117 | +hc HealtCheckOptions{..} runAppState runLogger = runHC runAppState (do |
| 118 | + runHealthCheck hcOffline |
| 119 | + ) >>= \case |
| 120 | + VRight r -> do |
| 121 | + liftIO $ print $ prettyShow r |
| 122 | + pure ExitSuccess |
| 123 | + VLeft e -> do |
| 124 | + runLogger $ logError $ T.pack $ prettyHFError e |
| 125 | + pure $ ExitFailure 27 |
| 126 | + |
0 commit comments