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 28, 2023
1 parent 8213992 commit 49306b7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
43 changes: 43 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,49 @@ By default, `scalafmt` only formats files that match the
val scalafmtThatIgnoresProjectSettings = scalafmt.withRespectProjectFilters(false)
```

### Alternate repositories and credentials

`scalafmt` uses some default repositories to download the version specified in
`.scalafmt.conf`; these repositories could be hardcoded or potentially specified
via the environment variables.

In order to specify explicit repositories as well, one could use

```scala mdoc:silent
val scalafmtWithRepos = scalafmt.withMavenRepositories(
"https://repo-1/snapshots",
"https://repo-2/public"
)
```

In addition, if some of the default or custom repositories require access credentials,
they could be specified via

```scala mdoc:silent
import org.scalafmt.interfaces._

val scalafmtWithCreds = scalafmtWithRepos match {
case x: RepositoryCredential.ScalafmtExtension =>
x.withRepositoryCredentials(
new org.scalafmt.interfaces.RepositoryCredential(
host = "repo-1",
username = "...",
password = "..."
),
new org.scalafmt.interfaces.RepositoryCredential(
host = "repo-2",
username = "...",
password = "...",
realm = "..."
)
)
case x => x
}
```

This capability was added to the public interface as an extension method, accessible
via a separate sub-interface.

### Clearing resources

Use the `clear()` method to clear up resources of the `scalafmt` instance.
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public interface Scalafmt {
*/
Scalafmt withMavenRepositories(String ... repositories);

/**
* Builder method.
*
* @param credentials repository credentials to use when resolving
* @return an updated interface instance.
*/
Scalafmt withRepositoryCredentials(RepositoryCredential... credentials);

/**
* Clear internal caches such as classloaded Scalafmt instances.
*/
Expand Down

0 comments on commit 49306b7

Please sign in to comment.