Skip to content

Commit

Permalink
Merge pull request #471 from eed3si9n/wip/launchertest
Browse files Browse the repository at this point in the history
Fix SshAgentSessionFactory
  • Loading branch information
eed3si9n authored Jun 26, 2020
2 parents 5b5f2d8 + 6ea6554 commit 28aadad
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- stage: test
env: SBT_VERSION=1.2.8
jdk: openjdk8
script: sbt "-no-colors" "^^ $SBT_VERSION" ";publishLocal;scalafmtCheckAll;plugin/scripted;scaffold/scripted;lib/test;app/compile"
script: sbt "-no-colors" "^^ $SBT_VERSION" ";publishLocal;scalafmtCheckAll;launcher/test;plugin/scripted;scaffold/scripted;lib/test;app/compile"
- name: "Scala 2.13"
script:
- sbt "++ 2.13.2" app/test lib/test
Expand Down
14 changes: 11 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Dependencies._
import CrossVersion.partialVersion

val g8version = "0.13.0-SNAPSHOT"
val g8version = "0.13.1-SNAPSHOT"

val javaVmArgs: List[String] = {
import scala.collection.JavaConverters._
Expand Down Expand Up @@ -142,6 +142,7 @@ lazy val gitsupport = (project in file("cli-git"))
jsch,
jschSshAgent,
jschConnectorFactory,
jgitJsch,
commonsIo,
scalatest % Test,
scalamock % Test
Expand Down Expand Up @@ -187,8 +188,15 @@ lazy val launcher = (project in file("launcher"))
description := "Command line tool to apply templates defined on GitHub",
name := "giter8-launcher",
crossScalaVersions := List(scala212, scala213),
libraryDependencies += coursier,
run / fork := true
libraryDependencies ++= Seq(
coursier,
verify % Test,
sbtIo % Test
),
testFrameworks += new TestFramework("verify.runner.Framework"),
run / fork := true,
Test / fork := true,
Test / javaOptions ++= Seq(s"""-DG8_HOME=${target.value / "home"}""")
// assemblyMergeStrategy in assembly := {
// case "plugin.properties" => MergeStrategy.concat
// case "module-info.class" => MergeStrategy.discard
Expand Down
41 changes: 22 additions & 19 deletions cli-git/src/main/scala/ConsoleCredentialsProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ object ConsoleCredentialsProvider extends CredentialsProvider {
def supports(items: CredentialItem*) = true

def get(uri: URIish, items: CredentialItem*): Boolean = {
items foreach {
case i: CredentialItem.Username =>
val username = System.console.readLine("%s: ", i.getPromptText)
i.setValue(username)

case i: CredentialItem.Password =>
val password = System.console.readPassword("%s: ", i.getPromptText)
i.setValueNoCopy(password)

case i: CredentialItem.InformationalMessage =>
System.console.printf("%s\n", i.getPromptText)

case i: CredentialItem.YesNoType =>
i.setValue(askYesNo(i.getPromptText))

case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
i.setValue(password)
if (System.console == null) false
else {
items foreach {
case i: CredentialItem.Username =>
val username = System.console.readLine("%s: ", i.getPromptText)
i.setValue(username)

case i: CredentialItem.Password =>
val password = System.console.readPassword("%s: ", i.getPromptText)
i.setValueNoCopy(password)

case i: CredentialItem.InformationalMessage =>
System.console.printf("%s\n", i.getPromptText)

case i: CredentialItem.YesNoType =>
i.setValue(askYesNo(i.getPromptText))

case i: CredentialItem.StringType if uri.getScheme == "ssh" =>
val password = String.valueOf(System.console.readPassword("%s: ", i.getPromptText))
i.setValue(password)
}
true
}
true
}

@scala.annotation.tailrec
Expand Down
7 changes: 5 additions & 2 deletions cli-git/src/main/scala/GitInteractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.io.File

import giter8.GitInteractor.TransportError
import org.eclipse.jgit.api.errors.TransportException
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory}
import org.eclipse.jgit.transport.{CredentialsProvider, SshSessionFactory, SshTransport}
import org.eclipse.jgit.api.{Git => JGit}

import scala.util.{Failure, Success, Try}
Expand All @@ -42,14 +42,17 @@ object GitInteractor {

class JGitInteractor(knownHosts: Option[String]) extends GitInteractor {
CredentialsProvider.setDefault(ConsoleCredentialsProvider)
SshSessionFactory.setInstance(new SshAgentSessionFactory(knownHosts))

override def cloneRepository(url: String, dest: File): Try[Unit] = Try {
JGit
.cloneRepository()
.setURI(url)
.setDirectory(dest)
.setCredentialsProvider(ConsoleCredentialsProvider)
.setTransportConfigCallback({
case sshTransport: SshTransport => sshTransport.setSshSessionFactory(new SshAgentSessionFactory(knownHosts))
case x => x
})
.call()
.close()
}
Expand Down
10 changes: 5 additions & 5 deletions launcher/src/main/scala/LauncherMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.net.URLClassLoader
import java.io.{BufferedInputStream, File, FileInputStream}
import java.util.Properties
import java.lang.reflect.InvocationTargetException
import java.nio.file.Files
import java.nio.file.{Files, StandardCopyOption}

object LauncherMain extends Runner with App {
java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.SEVERE)
Expand Down Expand Up @@ -35,7 +35,7 @@ class LauncherProcessor extends Processor {
(if (forceOverwrite) Vector("-f") else Vector()) ++
(outputDirectory match { case Some(out) => Vector("-o", out.toString); case _ => Vector() })
val giter8Files = giter8Artifacts(g8v)
virtuallyRun(giter8Files, virtualArgument)
virtuallyRun(giter8Files, virtualArgument, workingDirectory)
Right("")
}

Expand Down Expand Up @@ -67,7 +67,7 @@ class LauncherProcessor extends Processor {
}
downloadedJars map { downloaded =>
val t = bootDir / downloaded.getName
Files.copy(downloaded.toPath, t.toPath).toFile
Files.copy(downloaded.toPath, t.toPath, StandardCopyOption.REPLACE_EXISTING).toFile
}
}
// push launcher JAR to the end of classpath to avoid Scala version clash
Expand All @@ -83,9 +83,9 @@ class LauncherProcessor extends Processor {
}

// uses classloader trick to run
def virtuallyRun(files: Seq[File], args: Seq[String]): Unit = {
def virtuallyRun(files: Seq[File], args: Seq[String], workingDirectory: File): Unit = {
val cl = new URLClassLoader(files.map(_.toURL).toArray, null)
call("giter8.Giter8", "run", cl)(classOf[Array[String]])(args.toArray)
call("giter8.Giter8", "run", cl)(classOf[Array[String]], classOf[File])(args.toArray, workingDirectory)
}

def giter8Version(templateDir: File): Option[String] = {
Expand Down
33 changes: 33 additions & 0 deletions launcher/src/test/scala/LauncherTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testpkg

import giter8._
import verify._
import java.io.File
import sbt.io.IO

object LauncherTest extends BasicTestSuite {
lazy val launcher = new Runner {
def run(args: Array[String], workingDirectory: File): Int = {
run(args, workingDirectory, new LauncherProcessor)
}
}
implicit private class RichFile(file: File) {
def /(child: String): File = new File(file, child)
}

test("runs scala/scala-seed.g8") {
IO.withTemporaryDirectory { dir =>
launcher.run(Array("scala/scala-seed.g8", "--name=hello"), dir)
assert((dir / "hello" / "build.sbt").exists)
}
}

/*
test("runs git@github.com:scala/scala-seed.g8.git") {
IO.withTemporaryDirectory { dir =>
launcher.run(Array("git@github.com:scala/scala-seed.g8.git", "--name=hello"), dir)
assert((dir / "hello" / "build.sbt").exists)
}
}
*/
}
2 changes: 2 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ object Dependencies {
ExclusionRule("com.sun.jdmk", "jmxtools"),
ExclusionRule("com.sun.jmx", "jmxri")
)
val jgitJsch = "org.eclipse.jgit" % "org.eclipse.jgit.ssh.jsch" % "5.8.0.202006091008-r"
val jsch = "com.jcraft" % "jsch.agentproxy.jsch" % "0.0.9"
val jschSshAgent = "com.jcraft" % "jsch.agentproxy.sshagent" % "0.0.9"
val jschConnectorFactory = "com.jcraft" % "jsch.agentproxy.connector-factory" % "0.0.9"
val scopt = "com.github.scopt" %% "scopt" % "3.7.1"
val scalacheck = "org.scalacheck" %% "scalacheck" % "1.14.3"
val scalatest = "org.scalatest" %% "scalatest" % "3.2.0"
val scalamock = "org.scalamock" %% "scalamock" % "4.4.0"
val verify = "com.eed3si9n.verify" %% "verify" % "0.2.0"
val sbtIo = "org.scala-sbt" %% "io" % "1.3.4"
val scala212 = "2.12.11"
val scala213 = "2.13.2"
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.3.10
sbt.version=1.2.8

0 comments on commit 28aadad

Please sign in to comment.