Repository: scala-steward-org/scala-steward
Pull request #2255 (2021-09-29 18:15:43)

Streamline attemptLog* functions


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/SelfCheckAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/application/SelfCheckAlg.scala
index ed680c4a..601789d8 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/SelfCheckAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/SelfCheckAlg.scala
@@ -40,12 +40,12 @@ final class SelfCheckAlg[F[_]](config: Config)(implicit
     } yield ()
 
   private def checkGitBinary: F[Unit] =
-    logger.attemptLogWarn_(execFailedMessage("git")) {
+    logger.attemptWarn.log_(execFailedMessage("git")) {
       gitAlg.version.flatMap(output => logger.info(s"Using $output"))
     }
 
   private def checkScalafmtBinary: F[Unit] =
-    logger.attemptLogWarn_(execFailedMessage(scalafmtBinary)) {
+    logger.attemptWarn.log_(execFailedMessage(scalafmtBinary)) {
       scalafmtAlg.version.flatMap(output => logger.info(s"Using $output"))
     }
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
index 0c3d4a00..064b90af 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
@@ -82,7 +82,7 @@ final class StewardAlg[F[_]](config: Config)(implicit
   private def steward(repo: Repo): F[Either[Throwable, Unit]] = {
     val label = s"Steward ${repo.show}"
     logger.infoTotalTime(label) {
-      logger.attemptLogLabel(util.string.lineLeftRight(label), Some(label)) {
+      logger.attemptError.bracket(util.string.lineLeftRight(label), Some(label)) {
         F.guarantee(
           repoCacheAlg.checkCache(repo).flatMap { case (data, fork) =>
             pruningAlg.needsAttention(data).flatMap { case (attentionNeeded, updates) =>
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
index 00d68d8f..891f250f 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala
@@ -97,7 +97,7 @@ final class EditAlg[F[_]](implicit
   ): F[EditAttempt] =
     for {
       _ <- logger.info(s"Running migration $migration")
-      result <- logger.attemptLogWarn("Scalafix migration failed")(
+      result <- logger.attemptWarn.log("Scalafix migration failed")(
         buildToolDispatcher.runMigration(repo, config, migration)
       )
       verb = if (result.isRight) "Applied" else "Failed"
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
index 8e158575..e9f83b6f 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
@@ -53,7 +53,7 @@ final class HookExecutor[F[_]](implicit
     for {
       _ <- logger.info(s"Executing post-update hook for ${hook.groupId}:${hook.artifactId.name}")
       repoDir <- workspaceAlg.repoDir(repo)
-      result <- logger.attemptLogWarn("Post-update hook failed") {
+      result <- logger.attemptWarn.log("Post-update hook failed") {
         processAlg.execMaybeSandboxed(hook.useSandbox)(hook.command, repoDir)
       }
       maybeCommit <- gitAlg.commitAllIfDirty(repo, hook.commitMessage(update))
diff --git a/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
index ded9acb1..b7408d41 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala
@@ -126,19 +126,20 @@ final class NurtureAlg[F[_]](config: Config)(implicit
       oldNumber: PullRequestNumber,
       newNumber: PullRequestNumber
   ): F[Unit] =
-    logger.attemptLogWarn_(s"Closing PR #$oldNumber failed") {
+    logger.attemptWarn.bracket_(
+      s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}"
+    ) {
       for {
-        _ <- logger.info(s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}")
+        _ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
         comment = s"Superseded by ${vcsApiAlg.referencePullRequest(newNumber)}."
         _ <- vcsApiAlg.commentPullRequest(repo, oldNumber, comment)
         _ <- vcsApiAlg.closePullRequest(repo, oldNumber)
         _ <- removeRemoteBranch(repo, git.branchFor(oldUpdate))
-        _ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
       } yield ()
     }
 
   private def removeRemoteBranch(repo: Repo, branch: Branch): F[Unit] =
-    logger.attemptLogWarn_(s"Removing remote branch ${branch.name} failed") {
+    logger.attemptWarn.log_(s"Removing remote branch ${branch.name} failed") {
       gitAlg.removeBranch(repo, branch)
     }
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala b/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
index a17b2015..eb4b0add 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
@@ -23,34 +23,31 @@ import org.typelevel.log4cats.Logger
 import scala.concurrent.duration.FiniteDuration
 
 object logger {
-  implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
-    def attemptLogLabel[A](label: String, errorLabel: Option[String] = None)(fa: F[A])(implicit
-        F: MonadThrow[F]
-    ): F[Either[Throwable, A]] =
-      logger.info(label) >> attemptLogError(s"${errorLabel.getOrElse(label)} failed")(fa)
-
-    def attemptLogWarn[A](message: String)(fa: F[A])(implicit
-        F: MonadThrow[F]
-    ): F[Either[Throwable, A]] =
-      attemptLogImpl(fa, logger.warn(_)(message))
+  final class AttemptLoggerOps[F[_]](
+      logger: Logger[F],
+      logThrowable: (Throwable, String) => F[Unit]
+  )(implicit F: MonadThrow[F]) {
+    def log[A](msg: String)(fa: F[A]): F[Either[Throwable, A]] =
+      fa.attempt.flatTap(_.fold(t => logThrowable(t, msg), _ => F.unit))
 
-    def attemptLogWarn_[A](message: String)(fa: F[A])(implicit F: MonadThrow[F]): F[Unit] =
-      attemptLogImpl_(fa, logger.warn(_)(message))
+    def log_[A](msg: String)(fa: F[A]): F[Unit] =
+      log(msg)(fa).void
 
-    def attemptLogError[A](message: String)(fa: F[A])(implicit
-        F: MonadThrow[F]
+    def bracket[A](label: String, errorLabel: Option[String] = None)(
+        fa: F[A]
     ): F[Either[Throwable, A]] =
-      attemptLogImpl(fa, logger.error(_)(message))
+      logger.info(label) >> log(s"${errorLabel.getOrElse(label)} failed")(fa)
 
-    private def attemptLogImpl[A](fa: F[A], log: Throwable => F[Unit])(implicit
-        F: MonadThrow[F]
-    ): F[Either[Throwable, A]] =
-      fa.attempt.flatTap(_.fold(log, _ => F.unit))
+    def bracket_[A](label: String, errorLabel: Option[String] = None)(fa: F[A]): F[Unit] =
+      bracket(label, errorLabel)(fa).void
+  }
+
+  implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
+    def attemptWarn(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
+      new AttemptLoggerOps(logger, logger.warn(_)(_))
 
-    private def attemptLogImpl_[A](fa: F[A], log: Throwable => F[Unit])(implicit
-        F: MonadThrow[F]
-    ): F[Unit] =
-      fa.attempt.flatMap(_.fold(log, _ => F.unit))
+    def attemptError(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
+      new AttemptLoggerOps(logger, logger.error(_)(_))
 
     def infoTimed[A](msg: FiniteDuration => String)(fa: F[A])(implicit
         dateTimeAlg: DateTimeAlg[F],
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
index 234c4793..5e3ca851 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
@@ -49,7 +49,7 @@ final class VCSRepoAlg[F[_]](config: Config)(implicit
     }
 
   private def initSubmodules(repo: Repo): F[Unit] =
-    logger.attemptLogWarn_("Initializing and cloning submodules failed") {
+    logger.attemptWarn.log_("Initializing and cloning submodules failed") {
       gitAlg.initSubmodules(repo)
     }
 


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala b/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
index 61d79cc2..23528ca7 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
@@ -7,11 +7,12 @@ import org.scalasteward.core.mock.{MockEff, MockState}
 import org.scalasteward.core.util.logger.LoggerOps
 
 class loggerTest extends CatsEffectSuite {
-  test("attemptLogLabel") {
+  test("attemptError.bracket_") {
     final case class Err(msg: String) extends Throwable(msg)
     val err = Err("hmm?")
-    logger.attemptLogLabel("run")(MockEff.raiseError(err)).runS(MockState.empty).map { state =>
-      assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
+    logger.attemptError.bracket_("run")(MockEff.raiseError(err)).runS(MockState.empty).map {
+      state =>
+        assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
     }
   }