wasp/waspc/cli/Wasp/Cli/Command.hs
Martijn Faassen b6f738416a
Use send message protocol in cli (#493)
Use cliSendMessage everywhere in CLI

Instead of using waspSays and friends, we use the new cliSendMessage protocol everywhere to
send messages. For convenience we introduce a cliSendMessageC that's lifted to commands.

We handle CommandErrors specially now, and have a special way to display errors (failures) and warnings
with a heading.
2022-03-09 15:43:41 +01:00

28 lines
959 B
Haskell

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Wasp.Cli.Command
( Command,
runCommand,
CommandError (..),
)
where
import Control.Monad.Except (ExceptT, MonadError, runExceptT)
import Control.Monad.IO.Class (MonadIO)
import Wasp.Cli.Message (cliSendMessage)
import qualified Wasp.Message as Msg
newtype Command a = Command {_runCommand :: ExceptT CommandError IO a}
deriving (Functor, Applicative, Monad, MonadIO, MonadError CommandError)
runCommand :: Command a -> IO ()
runCommand cmd = do
errorOrResult <- runExceptT $ _runCommand cmd
case errorOrResult of
Left cmdError -> cliSendMessage $ Msg.Failure (_errorTitle cmdError) (_errorMsg cmdError)
Right _ -> return ()
-- TODO: What if we want to recognize errors in order to handle them?
-- Should we add _commandErrorType? Should CommandError be parametrized by it, is that even possible?
data CommandError = CommandError {_errorTitle :: !String, _errorMsg :: !String}