Commit 772d96ea authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Merge branch '13-cache-images' into 'main'

Add Image tag cache

See merge request !8
parents 30f7e18a b6c858f9
Loading
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -174,6 +174,8 @@ class Image(Item):
	Docker image items
	"""

	_cache = dict[str, str]()

	T = TypeVar('T', bound='Image')

	def __init__(self, iid: str):
@@ -200,10 +202,23 @@ class Image(Item):
		"""
		Pull an image from a registry
		"""
		try:
			iid = cls._cache[repository]
		except KeyError:
			docker(b"pull", repository)
		iid = Item(repository).inspect().path('$.Id', str)
			iid = cls._process_image(repository)
		return cls(iid)

	@classmethod
	def _process_image(cls, reference: str) -> str:
		report = Item(reference).inspect()
		iid = report.path("$.Id", str)
		cls._cache.update(
			((tag, iid) for tag in report.path("$.RepoTags", list[str])),
			reference=iid,
		)
		return iid

	def get_id(self) -> str:
		"""
		Return an identifier for the Docker Image