Skip to content

Commit

Permalink
more readable apis in DockerAlias class
Browse files Browse the repository at this point in the history
  • Loading branch information
kimxogus committed Jul 13, 2018
1 parent 484e8c8 commit d646cf0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
16 changes: 12 additions & 4 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerAlias.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ package com.typesafe.sbt.packager.docker
*/
case class DockerAlias(registryHost: Option[String], username: Option[String], name: String, tag: Option[String]) {

/** Untagged image alias */
val untagged: String = registryHost.map(_ + "/").getOrElse("") + username.map(_ + "/").getOrElse("") + name
def withRegistryHost(registryHost: Option[String]): DockerAlias = copy(registryHost = registryHost)

/** Tagged docker image alias */
val tagged: String = untagged + tag.map(":" + _).getOrElse("")
def withUsername(username: Option[String]): DockerAlias = copy(username = username)

def withName(name: String): DockerAlias = copy(name = name)

def withTag(tag: Option[String]): DockerAlias = copy(tag = tag)

override def toString: String =
registryHost.map(_ + "/").getOrElse("") +
username.map(_ + "/").getOrElse("") +
name +
tag.map(":" + _).getOrElse("")
}
42 changes: 19 additions & 23 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,29 @@ object DockerPlugin extends AutoPlugin {
dockerEnvVars := Map(),
dockerRepository := None,
dockerUsername := None,
dockerAdditionalTags := Nil,
dockerAlias := DockerAlias(
(dockerRepository in Docker).value,
(dockerUsername in Docker).value,
(packageName in Docker).value,
Option((version in Docker).value)
),
dockerUpdateLatest := false,
dockerUntaggedImage := false,
dockerAliases := {
val registry = (dockerRepository in Docker).value
val username = (dockerUsername in Docker).value
val name = (packageName in Docker).value

val tags = Seq(Option((version in Docker).value), if (dockerUpdateLatest.value) Some("latest") else None).flatten ++ dockerAdditionalTags.value
val aliases = tags.map { tag =>
DockerAlias(registry, username, name, Option(tag))
}
if (dockerUntaggedImage.value) {
aliases :+ DockerAlias(registry, username, name, None)
val alias = dockerAlias.value
if (dockerUpdateLatest.value) {
Seq(alias, alias.withTag(Option("latest")))
} else {
aliases
Seq(alias)
}
},
dockerAlias := dockerAliases.value.head, // alias tagged with (version in Docker)
dockerEntrypoint := Seq(s"${(defaultLinuxInstallLocation in Docker).value}/bin/${executableScriptName.value}"),
dockerCmd := Seq(),
dockerExecCommand := Seq("docker"),
dockerVersion := Try(Process(dockerExecCommand.value ++ Seq("version", "--format", "'{{.Server.Version}}'")).!!).toOption
.map(_.trim)
.flatMap(DockerVersion.parse),
dockerBuildOptions := Seq("--force-rm") ++ dockerAliases.value.flatMap { alias =>
Seq("-t", alias.tagged)
Seq("-t", alias.toString)
},
dockerRmiCommand := dockerExecCommand.value ++ Seq("rmi"),
dockerBuildCommand := dockerExecCommand.value ++ Seq("build") ++ dockerBuildOptions.value ++ Seq("."),
Expand Down Expand Up @@ -129,7 +125,7 @@ object DockerPlugin extends AutoPlugin {
val log = streams.value.log
publishLocalDocker(stage.value, dockerBuildCommand.value, log)
log.info(
s"Built image ${dockerAlias.value.untagged} with tags [${dockerAliases.value.flatMap(_.tag).mkString(", ")}]"
s"Built image ${dockerAlias.value.withTag(None).toString} with tags [${dockerAliases.value.flatMap(_.tag).mkString(", ")}]"
)
},
publish := {
Expand All @@ -138,7 +134,7 @@ object DockerPlugin extends AutoPlugin {
val log = streams.value.log
val execCommand = dockerExecCommand.value
alias.foreach { aliasValue =>
publishDocker(execCommand, aliasValue.tagged, log)
publishDocker(execCommand, aliasValue.toString, log)
}
},
clean := {
Expand All @@ -147,7 +143,7 @@ object DockerPlugin extends AutoPlugin {
val rmiCommand = dockerRmiCommand.value
// clean up images
alias.foreach { aliasValue =>
rmiDocker(rmiCommand, aliasValue.tagged, log)
rmiDocker(rmiCommand, aliasValue.toString, log)
}
},
sourceDirectory := sourceDirectory.value / "docker",
Expand Down Expand Up @@ -334,12 +330,12 @@ object DockerPlugin extends AutoPlugin {
case s if s.startsWith("Sending build context") =>
log.debug(s) // 1.0
case s if !s.trim.isEmpty => log.error(s)
case s =>
case s =>
}

override def out(inf: => String): Unit = inf match {
case s if !s.trim.isEmpty => log.info(s)
case s =>
case s =>
}

override def buffer[T](f: => T): T = f
Expand All @@ -359,7 +355,7 @@ object DockerPlugin extends AutoPlugin {
def rmiDockerLogger(log: Logger) = new sys.process.ProcessLogger {
override def err(err: => String): Unit = err match {
case s if !s.trim.isEmpty => log.error(s)
case s =>
case s =>
}

override def out(inf: => String): Unit = log.info(inf)
Expand All @@ -386,15 +382,15 @@ object DockerPlugin extends AutoPlugin {

override def err(err: => String): Unit = err match {
case s if !s.trim.isEmpty => log.error(s)
case s =>
case s =>
}

override def out(inf: => String): Unit =
inf match {
case s if s.startsWith("Please login") =>
loginRequired.compareAndSet(false, true)
case s if !loginRequired.get && !s.trim.isEmpty => log.info(s)
case s =>
case s =>
}

override def buffer[T](f: => T): T = f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ object DockerSpotifyClientPlugin extends AutoPlugin {

def publishLocalDocker: Def.Initialize[Task[Unit]] = Def.task {
val context = stage.value
val primaryAlias = dockerAlias.value
val aliases = dockerAliases.value
val log = streams.value.log

Expand All @@ -66,20 +67,16 @@ object DockerSpotifyClientPlugin extends AutoPlugin {

log.info(s"PublishLocal using Docker API ${docker.version().apiVersion()}")

aliases.headOption.foreach { primaryTag =>
docker.build(Paths.get(dockerDirectory), primaryTag.tagged, new ProgressHandler {
override def progress(message: ProgressMessage): Unit =
Option(message.error()) match {
case Some(error) if error.nonEmpty => log.error(message.error())
case _ => Option(message.stream()) foreach (v => log.info(v))
}
}, BuildParam.forceRm())

if (aliases.lengthCompare(1) > 0) {
aliases.drop(1).foreach { tag =>
docker.tag(primaryTag.tagged, tag.tagged, true)
docker.build(Paths.get(dockerDirectory), primaryAlias.toString, new ProgressHandler {
override def progress(message: ProgressMessage): Unit =
Option(message.error()) match {
case Some(error) if error.nonEmpty => log.error(message.error())
case _ => Option(message.stream()) foreach (v => log.info(v))
}
}
}, BuildParam.forceRm())

aliases.foreach { tag =>
docker.tag(primaryAlias.toString, tag.toString, true)
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ trait DockerKeys {
SettingKey[Seq[DockerAlias]]("dockerAliases", "Docker aliases for the built image")
val dockerUpdateLatest =
SettingKey[Boolean]("dockerUpdateLatest", "Set to update latest tag")
val dockerUntaggedImage =
SettingKey[Boolean]("dockerUntaggedImage", "Generate untagged docker image")
val dockerAdditionalTags = SettingKey[Seq[String]]("dockerAdditionalTags", "Docker image tags")
val dockerEntrypoint = SettingKey[Seq[String]]("dockerEntrypoint", "Entrypoint arguments passed in exec form")
val dockerCmd = SettingKey[Seq[String]](
"dockerCmd",
Expand Down

0 comments on commit d646cf0

Please sign in to comment.