Skip to content

Commit

Permalink
Use File in Cli.Args (#1653)
Browse files Browse the repository at this point in the history
* Use File in Cli.Args

This uses `File` in `Cli.Args` so that we don't need `Sync` when converting
from `Args` to `Config`.

* Add test
  • Loading branch information
fthomas authored Oct 11, 2020
1 parent f9b4d80 commit fd46985
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.scalasteward.core.application

import better.files.File
import caseapp._
import caseapp.core.Error.MalformedValue
import caseapp.core.argparser.{ArgParser, SimpleArgParser}
Expand All @@ -26,15 +27,15 @@ import scala.concurrent.duration._

object Cli {
final case class Args(
workspace: String,
reposFile: String,
defaultRepoConf: Option[String] = None,
workspace: File,
reposFile: File,
defaultRepoConf: Option[File] = None,
gitAuthorName: String = "Scala Steward",
gitAuthorEmail: String,
vcsType: SupportedVCS = SupportedVCS.GitHub,
vcsApiHost: Uri = uri"https://api.github.com",
vcsLogin: String,
gitAskPass: String,
gitAskPass: File,
signCommits: Boolean = false,
whitelist: List[String] = Nil,
readOnly: List[String] = Nil,
Expand All @@ -45,7 +46,7 @@ object Cli {
processTimeout: FiniteDuration = 10.minutes,
scalafixMigrations: List[Uri] = Nil,
disableDefaultScalafixMigrations: Boolean = false,
groupMigrations: Option[String] = None,
groupMigrations: Option[File] = None,
cacheTtl: FiniteDuration = 2.hours,
cacheMissDelay: FiniteDuration = 0.milliseconds,
bitbucketServerUseDefaultReviewers: Boolean = false,
Expand Down Expand Up @@ -86,9 +87,15 @@ object Cli {
}
}

implicit val fileArgParser: ArgParser[File] =
ArgParser[String].xmapError(
_.toString,
s => Either.catchNonFatal(File(s)).leftMap(t => MalformedValue("File", t.getMessage))
)

implicit val finiteDurationArgParser: ArgParser[FiniteDuration] =
ArgParser[String].xmapError(
_.toString(),
_.toString,
s =>
parseFiniteDuration(s).leftMap { throwable =>
val error = s"The value is expected in the following format: <length><unit>. ($throwable)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,29 @@ object Config {
disableDefaults: Boolean
)

def create[F[_]](args: Cli.Args)(implicit F: Sync[F]): F[Config] =
F.delay {
Config(
workspace = args.workspace.toFile,
reposFile = args.reposFile.toFile,
defaultRepoConfigFile = args.defaultRepoConf.map(_.toFile),
gitAuthor = Author(args.gitAuthorName, args.gitAuthorEmail),
vcsType = args.vcsType,
vcsApiHost = args.vcsApiHost,
vcsLogin = args.vcsLogin,
gitAskPass = args.gitAskPass.toFile,
signCommits = args.signCommits,
whitelistedDirectories = args.whitelist,
readOnlyDirectories = args.readOnly,
disableSandbox = args.disableSandbox,
doNotFork = args.doNotFork,
ignoreOptsFiles = args.ignoreOptsFiles,
envVars = args.envVar,
processTimeout = args.processTimeout,
scalafix = Scalafix(args.scalafixMigrations, args.disableDefaultScalafixMigrations),
groupMigrations = args.groupMigrations.map(_.toFile),
cacheTtl = args.cacheTtl,
cacheMissDelay = args.cacheMissDelay,
bitbucketServerUseDefaultReviewers = args.bitbucketServerUseDefaultReviewers,
gitlabMergeWhenPipelineSucceeds = args.gitlabMergeWhenPipelineSucceeds
)
}
def from(args: Cli.Args): Config =
Config(
workspace = args.workspace,
reposFile = args.reposFile,
defaultRepoConfigFile = args.defaultRepoConf,
gitAuthor = Author(args.gitAuthorName, args.gitAuthorEmail),
vcsType = args.vcsType,
vcsApiHost = args.vcsApiHost,
vcsLogin = args.vcsLogin,
gitAskPass = args.gitAskPass,
signCommits = args.signCommits,
whitelistedDirectories = args.whitelist,
readOnlyDirectories = args.readOnly,
disableSandbox = args.disableSandbox,
doNotFork = args.doNotFork,
ignoreOptsFiles = args.ignoreOptsFiles,
envVars = args.envVar,
processTimeout = args.processTimeout,
scalafix = Scalafix(args.scalafixMigrations, args.disableDefaultScalafixMigrations),
groupMigrations = args.groupMigrations,
cacheTtl = args.cacheTtl,
cacheMissDelay = args.cacheMissDelay,
bitbucketServerUseDefaultReviewers = args.bitbucketServerUseDefaultReviewers,
gitlabMergeWhenPipelineSucceeds = args.gitlabMergeWhenPipelineSucceeds
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object Context {
blocker <- Blocker[F]
implicit0(logger: Logger[F]) <- Resource.liftF(Slf4jLogger.create[F])
_ <- Resource.liftF(printBanner[F])
implicit0(config: Config) <- Resource.liftF(Config.create[F](args))
implicit0(config: Config) <- Resource.pure(Config.from(args))
implicit0(client: Client[F]) <- AsyncHttpClient.resource[F]()
implicit0(httpExistenceClient: HttpExistenceClient[F]) <- HttpExistenceClient.create[F]
implicit0(user: AuthenticatedUser) <- Resource.liftF(config.vcsUser[F])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.scalasteward.core.application

import better.files.File
import org.http4s.syntax.literals._
import org.scalasteward.core.application.Cli.EnvVar
import org.scalasteward.core.application.Cli.ParseResult._
Expand Down Expand Up @@ -27,14 +28,14 @@ class CliTest extends AnyFunSuite with Matchers with EitherValues {
).flatten
) shouldBe Success(
Cli.Args(
workspace = "a",
reposFile = "b",
defaultRepoConf = Some("c"),
workspace = File("a"),
reposFile = File("b"),
defaultRepoConf = Some(File("c")),
gitAuthorEmail = "d",
vcsType = SupportedVCS.Gitlab,
vcsApiHost = uri"http://example.com",
vcsLogin = "e",
gitAskPass = "f",
gitAskPass = File("f"),
ignoreOptsFiles = true,
envVar = List(EnvVar("g", "h"), EnvVar("i", "j")),
processTimeout = 30.minutes
Expand All @@ -53,11 +54,11 @@ class CliTest extends AnyFunSuite with Matchers with EitherValues {
).flatten
) shouldBe Success(
Cli.Args(
workspace = "a",
reposFile = "b",
workspace = File("a"),
reposFile = File("b"),
gitAuthorEmail = "d",
vcsLogin = "e",
gitAskPass = "f"
gitAskPass = File("f")
)
)
}
Expand Down Expand Up @@ -102,4 +103,8 @@ class CliTest extends AnyFunSuite with Matchers with EitherValues {
test("finiteDurationArgParser: previous value") {
Cli.finiteDurationArgParser(Some(10.seconds), "20seconds").isLeft shouldBe true
}

test("fileArgParser: previous value") {
Cli.fileArgParser(Some(File("/tmp")), "/opt").isLeft shouldBe true
}
}

0 comments on commit fd46985

Please sign in to comment.