installKernel :: InstallUser -- ^ Whether the kernel should be installed for only the current user (with @--user@) or globally
              -> Kernelspec  -- ^ The kernelspec to install
              -> IO InstallResult -- ^ Installation result, potentially with a friendly error message
installKernel installUser kernelspec = tryInstall `catch` handleInstallFailure
  where
    tryInstall :: IO InstallResult
    tryInstall = do
      jupyterPath <- which "jupyter"
      verifyJupyterCommand jupyterPath
      installKernelspec installUser jupyterPath kernelspec
      return InstallSuccessful

    handleInstallFailure :: JupyterKernelspecException -> IO InstallResult
    handleInstallFailure (JupyterKernelspecException message) = return $ InstallFailed message

-- | Throw a 'JupyterKernelspecException' with a given error message.
installKernel :: InstallUser -- ^ Whether the kernel should be installed for only the current user (with @--user@) or globally
              -> Kernelspec  -- ^ The kernelspec to install
              -> IO InstallResult
installKernel installUser kernelspec = tryInstall `catch` handleInstallFailure
  where
    tryInstall :: IO InstallResult
    tryInstall = do
      jupyterPath <- which "jupyter"
      verifyJupyterCommand jupyterPath
      installKernelspec installUser jupyterPath kernelspec
      return InstallSuccessful

    handleInstallFailure :: JupyterKernelspecException -> IO InstallResult
    handleInstallFailure (JupyterKernelspecException message) = return $ InstallFailed message

-- | Throw a 'JupyterKernelspecException' with a given error message.
installKernel installUser kernelspec = tryInstall `catch` handleInstallFailure
  where
    tryInstall :: IO InstallResult
    tryInstall = do
      jupyterPath <- which "jupyter"
      verifyJupyterCommand jupyterPath
      installKernelspec installUser jupyterPath kernelspec
      return InstallSuccessful

    handleInstallFailure :: JupyterKernelspecException -> IO InstallResult
    handleInstallFailure (JupyterKernelspecException message) = return $ InstallFailed message

-- | Throw a 'JupyterKernelspecException' with a given error message.