Skip to content

Commit

Permalink
Interfaces: add credentials to public interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Aug 27, 2023
1 parent 8213992 commit 5731480
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.scalafmt.dynamic

import java.io.OutputStreamWriter
import java.net.URL
import java.net.{URI, URL}

import scala.collection.JavaConverters._
import scala.util.Try

import coursierapi.{Dependency => CoursierDependency, _}

class CoursierDependencyDownloader(
private class CoursierDependencyDownloader(
downloadProgressWriter: OutputStreamWriter,
customRepositories: Seq[Repository]
) extends DependencyDownloader {
Expand Down Expand Up @@ -38,7 +38,13 @@ object CoursierDependencyDownloader extends DependencyDownloaderFactory {

override def create(properties: ScalafmtProperties): DependencyDownloader = {
val writer = properties.reporter.downloadOutputStreamWriter()
val repositories = properties.repositories.map(MavenRepository.of)
val repositories = properties.repositories.map { x =>
val host = new URI(x).getHost
val repo = MavenRepository.of(x)
properties.repositoryCredentials.find(_.host == host).fold(repo) { cred =>
repo.withCredentials(Credentials.of(cred.username, cred.password))
}
}
new CoursierDependencyDownloader(writer, repositories)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final case class ScalafmtDynamic(
moduleLoader: ScalafmtModuleLoader,
configLoader: ScalafmtConfigLoader
) extends Scalafmt
with RepositoryCredential.ScalafmtExtension
with ScalafmtSessionFactory {

def this() = this(
Expand Down Expand Up @@ -40,6 +41,11 @@ final case class ScalafmtDynamic(
override def withMavenRepositories(value: String*): Scalafmt =
copy(properties = properties.withMavenRepositories(value))

override def withRepositoryCredentials(
value: RepositoryCredential*
): Scalafmt =
copy(properties = properties.withRepositoryCredentials(value))

override def format(config: Path, file: Path, code: String): String =
createSession(config).format(file, code)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.scalafmt.interfaces._
final case class ScalafmtProperties(
reporter: ScalafmtReporter = ConsoleScalafmtReporter,
repositories: Seq[String] = Nil,
repositoryCredentials: Seq[RepositoryCredential] = Nil,
respectExcludeFilters: Boolean = true
) {

Expand All @@ -20,6 +21,11 @@ final case class ScalafmtProperties(
def withMavenRepositories(value: Seq[String]): ScalafmtProperties =
copy(repositories = value)

def withRepositoryCredentials(
value: Seq[RepositoryCredential]
): ScalafmtProperties =
copy(repositoryCredentials = value)

def reportError(file: Path, error: ScalafmtDynamicError): Unit =
error match {
case _: ConfigMissingVersion =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.scalafmt.interfaces;

public final class RepositoryCredential {
public final String host;
public final String username;
public final String password;
public final String realm;

public RepositoryCredential(String host, String username, String password) {
this(host, username, password, "");
}

public RepositoryCredential(String host, String username, String password, String realm) {
this.host = host;
this.username = username;
this.password = password;
this.realm = realm;
}

public interface ScalafmtExtension {
Scalafmt withRepositoryCredentials(RepositoryCredential... credentials);
}

}

0 comments on commit 5731480

Please sign in to comment.