Skip to content

Commit

Permalink
DockerPlugin code cleanup (#1436)
Browse files Browse the repository at this point in the history
* Use new slash syntax

* Cleanup imports

* Tried to add type annotation

* Fix redundant calls

* nonEmpty

* Use length, not size

* Generic code cleanup

* Apply provided change by @muuki88

Co-authored-by: Nepomuk Seiler <muuki88@users.noreply.github.com>

* Apply provided change by @muuki88

Co-authored-by: Nepomuk Seiler <muuki88@users.noreply.github.com>

* apply scalafmt

* Fix compile error

* Revert to old style "in"

Co-authored-by: Frank Oosterhuis <frank.oosterhuis@getthere.nl>
Co-authored-by: Nepomuk Seiler <muuki88@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 18, 2021
1 parent d2a41e0 commit 7ff7028
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.typesafe.sbt.packager.docker

import java.io.File
import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean

import sbt._
import sbt.Keys.{clean, mappings, name, organization, publish, publishLocal, sourceDirectory, streams, target, version}
import com.typesafe.sbt.SbtNativePackager.Universal
import com.typesafe.sbt.packager.Keys._
import com.typesafe.sbt.packager.archetypes.jar.ClasspathJarPlugin
import com.typesafe.sbt.packager.linux.LinuxPlugin.autoImport.{daemonUser, defaultLinuxInstallLocation}
import com.typesafe.sbt.packager.universal.UniversalPlugin
import com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stage
import com.typesafe.sbt.SbtNativePackager.Universal
import com.typesafe.sbt.packager.Compat._
import com.typesafe.sbt.packager.validation._
import com.typesafe.sbt.packager.{MappingsHelper, Stager}
import sbt.Keys._
import sbt._

import java.io.File
import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean
import scala.sys.process.Process
import scala.util.Try

Expand Down Expand Up @@ -53,6 +51,7 @@ object DockerPlugin extends AutoPlugin {
object autoImport extends DockerKeysEx {
val Docker: Configuration = config("docker")

// IntelliJ dies with the proper type annotation ¯\_(ツ)_/¯
val DockerAlias = com.typesafe.sbt.packager.docker.DockerAlias
}

Expand Down Expand Up @@ -98,7 +97,7 @@ object DockerPlugin extends AutoPlugin {
(dockerRepository in Docker).value,
(dockerUsername in Docker).value,
(packageName in Docker).value,
Option((version in Docker).value)
Option((version).value)
),
dockerLayerGrouping := { _: String =>
None
Expand All @@ -115,7 +114,7 @@ object DockerPlugin extends AutoPlugin {
case _ =>
}

val oldFunction = (dockerLayerGrouping in Docker).value
val oldFunction = dockerLayerGrouping.value

// By default we set this to a function that always returns None.
val oldPartialFunction = Function.unlift((tuple: (File, String)) => oldFunction(tuple._2))
Expand Down Expand Up @@ -372,7 +371,7 @@ object DockerPlugin extends AutoPlugin {
*/
private final def makeLabel(label: (String, String)): CmdLike = {
val (variable, value) = label
Cmd("LABEL", variable + "=\"" + value.toString + "\"")
Cmd("LABEL", variable + "=\"" + value + "\"")
}

/**
Expand All @@ -381,7 +380,7 @@ object DockerPlugin extends AutoPlugin {
*/
private final def makeEnvVar(envVar: (String, String)): CmdLike = {
val (variable, value) = envVar
Cmd("ENV", variable + "=\"" + value.toString + "\"")
Cmd("ENV", variable + "=\"" + value + "\"")
}

/**
Expand Down Expand Up @@ -607,13 +606,13 @@ object DockerPlugin extends AutoPlugin {
log.debug(s) // pre-1.0
case s if s.startsWith("Sending build context") =>
log.debug(s) // 1.0
case s if !s.trim.isEmpty => log.error(s)
case s if s.trim.nonEmpty => log.error(s)
case _ =>
}

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

Expand All @@ -625,13 +624,13 @@ object DockerPlugin extends AutoPlugin {
new sys.process.ProcessLogger {
override def err(err: => String): Unit =
err match {
case s if !s.trim.isEmpty => log.info(s)
case s if s.trim.nonEmpty => log.info(s)
case _ =>
}

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

Expand Down Expand Up @@ -663,7 +662,7 @@ object DockerPlugin extends AutoPlugin {
case DockerPermissionStrategy.MultiStage =>
IO.readLines(context / "Dockerfile")
.find(_.startsWith(labelCmd))
.map(_.substring(labelCmd.size).stripPrefix("\"").stripSuffix("\"")) match {
.map(_.substring(labelCmd.length).stripPrefix("\"").stripSuffix("\"")) match {
// No matter if the build process succeeded or failed, we try to remove the intermediate images
case Some(id) =>
val label = s"${labelKey}=${id}"
Expand All @@ -672,7 +671,7 @@ object DockerPlugin extends AutoPlugin {
sys.process.Process(execCommand ++ s"image prune -f --filter label=${label}".split(" ")) ! logger
// FYI: "docker image prune" returns 0 (success) no matter if images were removed or not
if (retImageClean != 0)
log.err(
log.error(
"Something went wrong while removing multi-stage intermediate image(s)"
) // no exception, just let the user know
case None =>
Expand All @@ -693,7 +692,7 @@ object DockerPlugin extends AutoPlugin {
new sys.process.ProcessLogger {
override def err(err: => String): Unit =
err match {
case s if !s.trim.isEmpty => log.error(s)
case s if s.trim.nonEmpty => log.error(s)
case s =>
}

Expand Down Expand Up @@ -721,15 +720,15 @@ object DockerPlugin extends AutoPlugin {

override def err(err: => String): Unit =
err match {
case s if !s.trim.isEmpty => log.error(s)
case s if s.trim.nonEmpty => log.error(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 if !loginRequired.get && s.trim.nonEmpty => log.info(s)
case s =>
}

Expand Down

0 comments on commit 7ff7028

Please sign in to comment.