Skip to content

Commit

Permalink
Added Or combinator to TokenSource
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Feb 19, 2020
1 parent 8f2a348 commit 7ba9845
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ You will need to ensure that both `githubActor` and `githubTokenSource` are set
Once this is configured, sbt-github-packages will automatically set your `githubActor` key to its value. That just leaves the `githubTokenSource`. The `TokenSource` ADT has the following possibilities:

```scala
sealed trait TokenSource extends Product with Serializable
sealed trait TokenSource extends Product with Serializable {
def ||(that: TokenSource): TokenSource =
TokenSource.Or(this, that)
}

object TokenSource {
final case class Environment(variable: String) extends TokenSource
final case class GitConfig(key: String) extends TokenSource
final case class Or(primary: TokenSource, secondary: TokenSource) extends TokenSource
}
```

Expand All @@ -62,6 +66,8 @@ Environment variables are a fairly good default. For example, I have a GitHub to
githubTokenSource := TokenSource.Environment("GITHUB_TOKEN")
```

The `||` combinator allows you to configure multiple token sources which will be tried in order on first-read of the setting.

Note that your CI server will need to set the `GITHUB_TOKEN` environment variable as well, as well as any collaborators on your project. The environment-specific nature of these login credentials are a major part of why they are *not* just strings sitting in the `build.sbt` file. As an example, if you're using Travis, you can do something like the following:

```bash
Expand Down
29 changes: 17 additions & 12 deletions src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,26 @@ object GitHubPackagesPlugin extends AutoPlugin {
userDefaults ++
authenticationSettings

def inferredGitHubCredentials(user: String, tokenSource: TokenSource) = {
val tokenM = tokenSource match {
def inferredGitHubCredentials(user: String, tokenSource: TokenSource): Option[Credentials] = {
def make(tokenM: Option[String]) =
tokenM map { token =>
Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
user,
token)
}

tokenSource match {
case TokenSource.Or(primary, secondary) =>
inferredGitHubCredentials(user, primary).orElse(
inferredGitHubCredentials(user, secondary))

case TokenSource.Environment(variable) =>
sys.env.get(variable)
make(sys.env.get(variable))

case TokenSource.GitConfig(key) =>
Try(s"git config $key".!!).map(_.trim).toOption
}

tokenM map { token =>
Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
user,
token)
make(Try(s"git config $key".!!).map(_.trim).toOption)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/sbtghpackages/TokenSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package sbtghpackages

sealed trait TokenSource extends Product with Serializable
sealed trait TokenSource extends Product with Serializable {
def ||(that: TokenSource): TokenSource =
TokenSource.Or(this, that)
}

object TokenSource {
final case class Environment(variable: String) extends TokenSource
final case class GitConfig(key: String) extends TokenSource
final case class Or(primary: TokenSource, secondary: TokenSource) extends TokenSource
}

0 comments on commit 7ba9845

Please sign in to comment.