Skip to content

Commit d871194

Browse files
authored
Merge pull request #2255 from scala-steward-org/topic/streamline-attemptLog
Streamline attemptLog* functions
2 parents 06f6c27 + da754d1 commit d871194

File tree

8 files changed

+35
-36
lines changed

8 files changed

+35
-36
lines changed

modules/core/src/main/scala/org/scalasteward/core/application/SelfCheckAlg.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ final class SelfCheckAlg[F[_]](config: Config)(implicit
4040
} yield ()
4141

4242
private def checkGitBinary: F[Unit] =
43-
logger.attemptLogWarn_(execFailedMessage("git")) {
43+
logger.attemptWarn.log_(execFailedMessage("git")) {
4444
gitAlg.version.flatMap(output => logger.info(s"Using $output"))
4545
}
4646

4747
private def checkScalafmtBinary: F[Unit] =
48-
logger.attemptLogWarn_(execFailedMessage(scalafmtBinary)) {
48+
logger.attemptWarn.log_(execFailedMessage(scalafmtBinary)) {
4949
scalafmtAlg.version.flatMap(output => logger.info(s"Using $output"))
5050
}
5151

modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ final class StewardAlg[F[_]](config: Config)(implicit
8282
private def steward(repo: Repo): F[Either[Throwable, Unit]] = {
8383
val label = s"Steward ${repo.show}"
8484
logger.infoTotalTime(label) {
85-
logger.attemptLogLabel(util.string.lineLeftRight(label), Some(label)) {
85+
logger.attemptError.bracket(util.string.lineLeftRight(label), Some(label)) {
8686
F.guarantee(
8787
repoCacheAlg.checkCache(repo).flatMap { case (data, fork) =>
8888
pruningAlg.needsAttention(data).flatMap { case (attentionNeeded, updates) =>

modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final class EditAlg[F[_]](implicit
9797
): F[EditAttempt] =
9898
for {
9999
_ <- logger.info(s"Running migration $migration")
100-
result <- logger.attemptLogWarn("Scalafix migration failed")(
100+
result <- logger.attemptWarn.log("Scalafix migration failed")(
101101
buildToolDispatcher.runMigration(repo, config, migration)
102102
)
103103
verb = if (result.isRight) "Applied" else "Failed"

modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class HookExecutor[F[_]](implicit
5353
for {
5454
_ <- logger.info(s"Executing post-update hook for ${hook.groupId}:${hook.artifactId.name}")
5555
repoDir <- workspaceAlg.repoDir(repo)
56-
result <- logger.attemptLogWarn("Post-update hook failed") {
56+
result <- logger.attemptWarn.log("Post-update hook failed") {
5757
processAlg.execMaybeSandboxed(hook.useSandbox)(hook.command, repoDir)
5858
}
5959
maybeCommit <- gitAlg.commitAllIfDirty(repo, hook.commitMessage(update))

modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,20 @@ final class NurtureAlg[F[_]](config: Config)(implicit
126126
oldNumber: PullRequestNumber,
127127
newNumber: PullRequestNumber
128128
): F[Unit] =
129-
logger.attemptLogWarn_(s"Closing PR #$oldNumber failed") {
129+
logger.attemptWarn.bracket_(
130+
s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}"
131+
) {
130132
for {
131-
_ <- logger.info(s"Closing obsolete PR ${oldUrl.renderString} for ${oldUpdate.show}")
133+
_ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
132134
comment = s"Superseded by ${vcsApiAlg.referencePullRequest(newNumber)}."
133135
_ <- vcsApiAlg.commentPullRequest(repo, oldNumber, comment)
134136
_ <- vcsApiAlg.closePullRequest(repo, oldNumber)
135137
_ <- removeRemoteBranch(repo, git.branchFor(oldUpdate))
136-
_ <- pullRequestRepository.changeState(repo, oldUrl, PullRequestState.Closed)
137138
} yield ()
138139
}
139140

140141
private def removeRemoteBranch(repo: Repo, branch: Branch): F[Unit] =
141-
logger.attemptLogWarn_(s"Removing remote branch ${branch.name} failed") {
142+
logger.attemptWarn.log_(s"Removing remote branch ${branch.name} failed") {
142143
gitAlg.removeBranch(repo, branch)
143144
}
144145

modules/core/src/main/scala/org/scalasteward/core/util/logger.scala

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,31 @@ import org.typelevel.log4cats.Logger
2323
import scala.concurrent.duration.FiniteDuration
2424

2525
object logger {
26-
implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
27-
def attemptLogLabel[A](label: String, errorLabel: Option[String] = None)(fa: F[A])(implicit
28-
F: MonadThrow[F]
29-
): F[Either[Throwable, A]] =
30-
logger.info(label) >> attemptLogError(s"${errorLabel.getOrElse(label)} failed")(fa)
31-
32-
def attemptLogWarn[A](message: String)(fa: F[A])(implicit
33-
F: MonadThrow[F]
34-
): F[Either[Throwable, A]] =
35-
attemptLogImpl(fa, logger.warn(_)(message))
26+
final class AttemptLoggerOps[F[_]](
27+
logger: Logger[F],
28+
logThrowable: (Throwable, String) => F[Unit]
29+
)(implicit F: MonadThrow[F]) {
30+
def log[A](msg: String)(fa: F[A]): F[Either[Throwable, A]] =
31+
fa.attempt.flatTap(_.fold(t => logThrowable(t, msg), _ => F.unit))
3632

37-
def attemptLogWarn_[A](message: String)(fa: F[A])(implicit F: MonadThrow[F]): F[Unit] =
38-
attemptLogImpl_(fa, logger.warn(_)(message))
33+
def log_[A](msg: String)(fa: F[A]): F[Unit] =
34+
log(msg)(fa).void
3935

40-
def attemptLogError[A](message: String)(fa: F[A])(implicit
41-
F: MonadThrow[F]
36+
def bracket[A](label: String, errorLabel: Option[String] = None)(
37+
fa: F[A]
4238
): F[Either[Throwable, A]] =
43-
attemptLogImpl(fa, logger.error(_)(message))
39+
logger.info(label) >> log(s"${errorLabel.getOrElse(label)} failed")(fa)
4440

45-
private def attemptLogImpl[A](fa: F[A], log: Throwable => F[Unit])(implicit
46-
F: MonadThrow[F]
47-
): F[Either[Throwable, A]] =
48-
fa.attempt.flatTap(_.fold(log, _ => F.unit))
41+
def bracket_[A](label: String, errorLabel: Option[String] = None)(fa: F[A]): F[Unit] =
42+
bracket(label, errorLabel)(fa).void
43+
}
44+
45+
implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
46+
def attemptWarn(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
47+
new AttemptLoggerOps(logger, logger.warn(_)(_))
4948

50-
private def attemptLogImpl_[A](fa: F[A], log: Throwable => F[Unit])(implicit
51-
F: MonadThrow[F]
52-
): F[Unit] =
53-
fa.attempt.flatMap(_.fold(log, _ => F.unit))
49+
def attemptError(implicit F: MonadThrow[F]): AttemptLoggerOps[F] =
50+
new AttemptLoggerOps(logger, logger.error(_)(_))
5451

5552
def infoTimed[A](msg: FiniteDuration => String)(fa: F[A])(implicit
5653
dateTimeAlg: DateTimeAlg[F],

modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class VCSRepoAlg[F[_]](config: Config)(implicit
4949
}
5050

5151
private def initSubmodules(repo: Repo): F[Unit] =
52-
logger.attemptLogWarn_("Initializing and cloning submodules failed") {
52+
logger.attemptWarn.log_("Initializing and cloning submodules failed") {
5353
gitAlg.initSubmodules(repo)
5454
}
5555

modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import org.scalasteward.core.mock.{MockEff, MockState}
77
import org.scalasteward.core.util.logger.LoggerOps
88

99
class loggerTest extends CatsEffectSuite {
10-
test("attemptLogLabel") {
10+
test("attemptError.bracket_") {
1111
final case class Err(msg: String) extends Throwable(msg)
1212
val err = Err("hmm?")
13-
logger.attemptLogLabel("run")(MockEff.raiseError(err)).runS(MockState.empty).map { state =>
14-
assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
13+
logger.attemptError.bracket_("run")(MockEff.raiseError(err)).runS(MockState.empty).map {
14+
state =>
15+
assertEquals(state.trace, Vector(Log("run"), Log((Some(err), "run failed"))))
1516
}
1617
}
1718

0 commit comments

Comments
 (0)