Skip to content

Commit

Permalink
GitOps: modify exec, don't throw just to catch
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 27, 2021
1 parent 4f5d82c commit 2f86b79
Showing 1 changed file with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.scalafmt.sysops

import scala.sys.process.ProcessLogger
import scala.util.Try
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import java.nio.file.Path

object GitOps {
Expand Down Expand Up @@ -100,24 +99,18 @@ trait GitOps {
private class GitOpsImpl(val workingDirectory: AbsoluteFile) extends GitOps {

private[scalafmt] def exec(cmd: Seq[String]): Try[Seq[String]] = {
val gitRes: Try[String] = Try {
val lastError = new StringBuilder
val swallowStderr = ProcessLogger(_ => (), err => lastError.append(err))
try {
sys.process
.Process(cmd, workingDirectory.jfile)
.!!(swallowStderr)
.trim
} catch {
case NonFatal(e) =>
throw new IllegalStateException(
s"Failed to run command ${cmd.mkString(" ")}. " +
s"Error: ${lastError.toString()}",
e
)
}
}
gitRes.map(_.linesIterator.toSeq)
val errors = Seq.newBuilder[String]
Try {
val swallowStderr = ProcessLogger(_ => (), errors += _)
sys.process.Process(cmd, workingDirectory.jfile).!!(swallowStderr)
}.fold(
e => {
val err = errors.result().mkString("\n> ", "\n> ", "\n")
val msg = s"Failed to run command ${cmd.mkString(" ")}. Error:$err"
Failure(new IllegalStateException(msg, e))
},
x => Success(x.trim.linesIterator.toSeq)
)
}

override def lsTree(dir: AbsoluteFile): Seq[AbsoluteFile] = {
Expand Down

0 comments on commit 2f86b79

Please sign in to comment.