Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Add CLI / config parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeniyk committed Jun 15, 2020
1 parent 7a65091 commit 3cd84b7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ runSteward := Def.taskDyn {
val args = Seq(
Seq("--workspace", s"$projectDir/workspace"),
Seq("--repos-file", s"$projectDir/repos.md"),
Seq("--repos-default-config", s"$projectDir/.scala-steward.conf"),
Seq("--git-author-email", s"me@$projectName.org"),
Seq("--vcs-login", projectName),
Seq("--git-ask-pass", s"$home/.github/askpass/$projectName.sh"),
Expand Down
5 changes: 4 additions & 1 deletion docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ sbt stage
./modules/core/.jvm/target/universal/stage/bin/scala-steward \
--workspace "$STEWARD_DIR/workspace" \
--repos-file "$STEWARD_DIR/repos.md" \
--repos-default-conf "/opt/scala-steward/.scala-steward.conf" \
--git-author-email ${EMAIL} \
--vcs-api-host "https://api.github.com" \
--vcs-login ${LOGIN} \
Expand All @@ -24,6 +25,7 @@ sbt docker:publishLocal
docker run -v $STEWARD_DIR:/opt/scala-steward -it fthomas/scala-steward:latest \
--workspace "/opt/scala-steward/workspace" \
--repos-file "/opt/scala-steward/repos.md" \
--repos-default-conf "/opt/scala-steward/.scala-steward.conf" \
--git-author-email ${EMAIL} \
--vcs-api-host "https://api.github.com" \
--vcs-login ${LOGIN} \
Expand Down Expand Up @@ -71,7 +73,7 @@ The file should contain a single line: `credentials += Credentials("Some Nexus R
```
sbt
project core
run --disable-sandbox --do-not-fork --workspace "/path/workspace" --repos-file "/path/repos.md" --git-ask-pass "/path/pass.sh" --git-author-email "email@example.org" --vcs-type "gitlab" --vcs-api-host "https://gitlab.com/api/v4/" --vcs-login "gitlab.steward"
run --disable-sandbox --do-not-fork --workspace "/path/workspace" --repos-file "/path/repos.md" --repos-default-conf "/opt/scala-steward/.scala-steward.conf" --git-ask-pass "/path/pass.sh" --git-author-email "email@example.org" --vcs-type "gitlab" --vcs-api-host "https://gitlab.com/api/v4/" --vcs-login "gitlab.steward"
```


Expand All @@ -93,6 +95,7 @@ docker run -v $PWD:/opt/scala-steward \
--do-not-fork \
--workspace "/opt/scala-steward/workspace" \
--repos-file "/opt/scala-steward/repos.md" \
--repos-default-conf "/opt/scala-steward/.scala-steward.conf" \
--git-ask-pass "/opt/scala-steward/pass.sh" \
--git-author-email "myemail@company.xyz" \
--vcs-type "bitbucket" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object Cli {
final case class Args(
workspace: String,
reposFile: String,
reposDefaultConf: String,
gitAuthorName: String = "Scala Steward",
gitAuthorEmail: String,
vcsType: SupportedVCS = SupportedVCS.GitHub,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import scala.sys.process.Process
final case class Config(
workspace: File,
reposFile: File,
reposDefaultConfigFile: File,
gitAuthor: Author,
vcsType: SupportedVCS,
vcsApiHost: Uri,
Expand Down Expand Up @@ -85,6 +86,7 @@ object Config {
Config(
workspace = args.workspace.toFile,
reposFile = args.reposFile.toFile,
reposDefaultConfigFile = args.reposDefaultConf.toFile,
gitAuthor = Author(args.gitAuthorName, args.gitAuthorEmail),
vcsType = args.vcsType,
vcsApiHost = args.vcsApiHost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import cats.data.OptionT
import cats.implicits._
import io.chrisdavenport.log4cats.Logger
import io.circe.config.parser
import org.scalasteward.core.application.Config
import org.scalasteward.core.data.Update
import org.scalasteward.core.io.{FileAlg, WorkspaceAlg}
import org.scalasteward.core.repoconfig.RepoConfigAlg._
import org.scalasteward.core.util.MonadThrowable
import org.scalasteward.core.vcs.data.Repo

final class RepoConfigAlg[F[_]](implicit
config: Config,
fileAlg: FileAlg[F],
logger: Logger[F],
workspaceAlg: WorkspaceAlg[F],
Expand All @@ -36,21 +38,21 @@ final class RepoConfigAlg[F[_]](implicit
def readRepoConfigOrDefault(repo: Repo): F[RepoConfig] =
readRepoConfig(repo).flatMap { config =>
if (config.isDefined) F.pure(config.get)
else defaultRepoConfig
else defaultRepoConfig()
}

/**
* Default configuration will try to read .scala-steward.conf in the root
* If not found - fallback to [[RepoConfig.default]]
* Note it's not lazy since we want to get config updates if you decide to change config in the middle of the process
*/
lazy val defaultRepoConfig: F[RepoConfig] =
workspaceAlg.rootDir.flatMap(readConfiguration(_).map(_.getOrElse(RepoConfig.default)))
def defaultRepoConfig(): F[RepoConfig] =
readConfiguration(config.reposDefaultConfigFile).map(_.getOrElse(RepoConfig.default))

def readRepoConfig(repo: Repo): F[Option[RepoConfig]] =
workspaceAlg.repoDir(repo).flatMap(readConfiguration)
workspaceAlg.repoDir(repo).flatMap(projectRootDir => readConfiguration(projectRootDir / repoConfigBasename))

private def readConfiguration(projectRootDir: File): F[Option[RepoConfig]] = {
val configFile = projectRootDir / repoConfigBasename
private def readConfiguration(configFile: File): F[Option[RepoConfig]] = {
val maybeRepoConfig = OptionT(fileAlg.readFile(configFile)).map(parseRepoConfig).flatMapF {
case Right(config) => logger.info(s"Parsed $config").as(config.some)
case Left(errorMsg) => logger.info(errorMsg).as(none[RepoConfig])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class PruningAlg[F[_]](implicit
val depsWithoutResolvers = dependencies.map(_.value).distinct
for {
_ <- logger.info(s"Find updates for ${repo.show}")
defaultConfig <- repoConfigAlg.defaultRepoConfig
defaultConfig <- repoConfigAlg.defaultRepoConfig()
repoConfig = repoCache.maybeRepoConfig.getOrElse(defaultConfig)
updates0 <- updateAlg.findUpdates(dependencies, repoConfig, None)
updateStates0 <- findAllUpdateStates(repo, repoCache, depsWithoutResolvers, updates0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CliTest extends AnyFunSuite with Matchers {
List(
List("--workspace", "a"),
List("--repos-file", "b"),
List("--repos-default-conf", "c"),
List("--git-author-email", "d"),
List("--vcs-type", "gitlab"),
List("--vcs-api-host", "http://example.com"),
Expand All @@ -30,6 +31,7 @@ class CliTest extends AnyFunSuite with Matchers {
Cli.Args(
workspace = "a",
reposFile = "b",
reposDefaultConf = "c",
gitAuthorEmail = "d",
vcsType = SupportedVCS.Gitlab,
vcsApiHost = uri"http://example.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object MockContext {
implicit val config: Config = Config(
workspace = File.temp / "ws",
reposFile = File.temp / "repos.md",
reposDefaultConfigFile = File.temp / ".scala-steward.conf",
gitAuthor = Author("Bot Doe", "bot@example.org"),
vcsType = SupportedVCS.GitHub,
vcsApiHost = Uri(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
val repo = Repo("fthomas", "scalafix-test")
val repoCacheFile =
config.workspace / "store/repo_cache/v1/fthomas/scalafix-test/repo_cache.json"
val repoDefaultConfigFile = config.workspace / ".scala-steward.conf"
val repoCacheContent =
s"""|{
| "sha1": "12def27a837ba6dc9e17406cbbe342fba3527c14",
Expand Down Expand Up @@ -61,7 +60,7 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
state shouldBe initial.copy(
commands = Vector(
List("read", repoCacheFile.toString),
List("read", repoDefaultConfigFile.toString),
List("read", config.reposDefaultConfigFile.toString),
List("read", pullRequestsFile.toString)
),
logs = Vector(
Expand All @@ -76,7 +75,6 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
val repo = Repo("fthomas", "scalafix-test")
val repoCacheFile =
config.workspace / "store/repo_cache/v1/fthomas/scalafix-test/repo_cache.json"
val repoDefaultConfigFile = config.workspace / ".scala-steward.conf"
val repoCacheContent =
s"""|{
| "sha1": "12def27a837ba6dc9e17406cbbe342fba3527c14",
Expand Down Expand Up @@ -170,7 +168,7 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
state shouldBe initial.copy(
commands = Vector(
List("read", repoCacheFile.toString),
List("read", repoDefaultConfigFile.toString),
List("read", config.reposDefaultConfigFile.toString),
List("read", pullRequestsFile.toString)
),
logs = Vector(
Expand All @@ -185,7 +183,6 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
val repo = Repo("fthomas", "scalafix-test")
val repoCacheFile =
config.workspace / "store/repo_cache/v1/fthomas/scalafix-test/repo_cache.json"
val repoDefaultConfigFile = config.workspace / ".scala-steward.conf"
val repoCacheContent =
s"""|{
| "sha1": "12def27a837ba6dc9e17406cbbe342fba3527c14",
Expand Down Expand Up @@ -289,7 +286,7 @@ class PruningAlgTest extends AnyFunSuite with Matchers {
state shouldBe initial.copy(
commands = Vector(
List("read", repoCacheFile.toString),
List("read", repoDefaultConfigFile.toString),
List("read", config.reposDefaultConfigFile.toString),
List("read", versionsFile.toString),
List("read", pullRequestsFile.toString),
List("read", versionsFile.toString),
Expand Down
2 changes: 2 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ LOGIN="scala-steward"

COMMON_ARGS=(
--workspace "$STEWARD_DIR/workspace"
--repos-file "$STEWARD_DIR/repos.md"
--repos-default-conf "$STEWARD_DIR/.scala-steward.conf"
--git-author-email "me@$LOGIN.org"
--vcs-login "$LOGIN"
--ignore-opts-files
Expand Down

0 comments on commit 3cd84b7

Please sign in to comment.