@@ -1,11 +1,10 @@
11{-# LANGUAGE LambdaCase #-}
22module Obelisk.Command.CLI where
33
4−import Control.Exception (Exception, SomeException, catch, displayException, handle, throw)
4+import Control.Exception (SomeException, displayException, finally, handle)
55import Data.Text (Text)
66import qualified Data.Text as T
77import qualified Data.Text.IO as T
8−import Data.Typeable (Typeable)
98import System.Exit (ExitCode (ExitFailure), exitWith)
109
1110import System.Console.ANSI
@@ -22,15 +21,8 @@ withSpinner
2221 -> IO a
2322withSpinner s e f = do
2423 spinner <- dots1Spinner (1000 * 200) s
25−
26− -- Upon exception (typically raised by failWith), stop the spinner, log the error and exit.
27− result <- catch f $ \exc -> do
24+ result <- finally f $ do
2825 stopIndicator spinner
29− putError $ T.pack $ displayException (exc :: SomeException)
30− exitWith $ ExitFailure 2
31−
32− -- Upon no exceptions, stop the spinner and log the optional exit message.
33− stopIndicator spinner
3426 case e of
3527 Just exitMsg -> putInfo $ T.pack exitMsg
3628 _ -> return ()
@@ -38,8 +30,6 @@ withSpinner s e f = do
3830
3931data Level = Level_Normal | Level_Warning | Level_Error
4032
41−-- | Prefer this over the alternative ways to exit the program with an error message as it automatically
42−-- takes care of cleaning on spinners.
4333-- TODO: Handle this error cleanly when evaluating outside of `withSpinner` (eg: runCLI)
4434failWith :: Text -> IO a
4535failWith = ioError . userError . T.unpack