In [1]:
import scala.sys.process._


Out[1]:
import scala.sys.process._

In [2]:
"docker images".!!


Out[2]:
res1: String = """
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mosaico/abuild        3                   3af917b24969        4 hours ago         285 MB
oraclelinux           7                   4fa40ffcfccd        5 weeks ago         225 MB
ubuntubuilder         latest              ab327d93e9ad        7 weeks ago         309 MB
alpine                edge                a1a3cae7a75e        2 months ago        3.98 MB
alpine                latest              baa5d63471ea        2 months ago      ...

In [8]:
val s1 = "docker images".!!
      .split("\n").toList.tail


Out[8]:
s1: List[String] = List(
  "mosaico/abuild        3                   3af917b24969        4 hours ago         285 MB",
  "oraclelinux           7                   4fa40ffcfccd        5 weeks ago         225 MB",
  "ubuntubuilder         latest              ab327d93e9ad        7 weeks ago         309 MB",
  "alpine                edge                a1a3cae7a75e        2 months ago        3.98 MB",
  "alpine                latest              baa5d63471ea        2 months ago        4.8 MB",
  "ubuntu                14.04.5             f2d8ce9fa988        2 months ago   ...

In [14]:
val s2 = s1.map(_.split("\\s+").toList)


Out[14]:
s2: List[List[String]] = List(
  List("mosaico/abuild", "3", "3af917b24969", "4", "hours", "ago", "285", "MB"),
  List("oraclelinux", "7", "4fa40ffcfccd", "5", "weeks", "ago", "225", "MB"),
  List(
    "ubuntubuilder",
    "latest",
    "ab327d93e9ad",
    "7",
    "weeks",
    "ago",
    "309",
    "MB"
...

In [16]:
val s3 = s2.map(x=> x(0)+":"+x(1) -> x(2))


Out[16]:
s3: List[(String, String)] = List(
  ("mosaico/abuild:3", "3af917b24969"),
  ("oraclelinux:7", "4fa40ffcfccd"),
  ("ubuntubuilder:latest", "ab327d93e9ad"),
  ("alpine:edge", "a1a3cae7a75e"),
  ("alpine:latest", "baa5d63471ea"),
  ("ubuntu:14.04.5", "f2d8ce9fa988"),
  ("java:latest", "96cddf5ae9f1"),
  ("cloudera/quickstart:latest", "4239cd2958c6")
)

In [24]:
def findImagesByName(name: String) = {
    val s1 = "docker images".!!
      .split("\n").toList.tail
    //s1=List("mosaico/abuild   3   3af9 ...."...)
    val s2 = s1.map(_.split("\\s+").toList)
    //s2=List(List("mosaico/abuild","3","3af9",...)...)
    val s3 = s2.map(x=> x(0)+":"+x(1) -> x(2))
    //List( "mosaico/abuild:3" -> "3af9",....)
    s3.filter(_._1.indexOf(name)!= -1)
    // filter with first and extract the second
  }


Out[24]:
defined function findImagesByName

In [25]:
findImagesByName("alpine")


Out[25]:
res24: List[(String, String)] = List(("alpine:edge", "a1a3cae7a75e"), ("alpine:latest", "baa5d63471ea"))

In [26]:
val args=Array("alpine", ":latest")


Out[26]:
args: Array[String] = Array("alpine", ":latest")

In [32]:
args.flatMap(findImagesByName(_)).distinct.map(_._1)


Out[32]:
res31: Array[String] = Array(
  "alpine:edge",
  "alpine:latest",
  "ubuntubuilder:latest",
  "java:latest",
  "cloudera/quickstart:latest"
)

In [ ]: