Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance tags command to show image digest #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/commands/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dateutil.parser

from ..consts import PER_PAGE
from ..libs.utils import readable_memory_format, print_result
from ..libs.utils import readable_memory_format, print_result, digest_to_short
from ..libs.config import Config


Expand Down Expand Up @@ -37,16 +37,39 @@ def get_tags(docker_hub_client, orgname, args, per_page=PER_PAGE):
formatted_date = ''
if repo['last_updated']:
formatted_date = dateutil.parser \
.parse(repo['last_updated'])
.parse(repo['last_updated'])
formatted_date = formatted_date.strftime("%Y-%m-%d %H:%M")

# digest
if 'digest' in repo:
digest = repo['digest']
else:
digest = 'N/A'

# images
images_platform = []
images_size = []
images_digest = []
if 'images' in repo:
for image in repo['images']:
images_platform.append("os:%s-(%s) arch:%s" % (
image['os'], image['os_version'] or 'N/A',
image['architecture']))
images_size.append(readable_memory_format(
image['size'] / 1024))
images_digest.append(digest_to_short(image['digest']))

# Convert full_size in bytes to KB
size_in_kb = repo['full_size'] / 1024
formatted_size = readable_memory_format(size_in_kb)
rows.append([repo['name'], formatted_size, formatted_date])
header = ['Name', 'Size', 'Last updated']
rows.append([repo['name'], formatted_size, formatted_date, digest_to_short(digest),
"\n".join(images_platform), "\n".join(images_size),
"\n".join(images_digest)])
header = ['Name', 'Size', 'Last updated', 'Digest', "Images Platform",
"Image Size", "Images Digest"]
print_result(args.format, rows, header, resp['content']['count'],
args.page)
total_pages = int(((resp['content']['count'] - 1)/per_page) + 1)
total_pages = int(((resp['content']['count'] - 1) / per_page) + 1)
return total_pages
print('This repo has no tags')
return None
Expand Down
12 changes: 9 additions & 3 deletions src/libs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def print_result(fmt, rows=None, header=None, count=0, page=1, heading=False,
if count == 0:
print(zero_result_msg)
else:
total_pages = int(((count - 1)/PER_PAGE) + 1)
total_pages = int(((count - 1) / PER_PAGE) + 1)
if heading:
print_header(heading)
else:
Expand All @@ -74,13 +74,14 @@ def readable_memory_format(size):
size_name = ("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size, 1024)))
power = math.pow(1024, i)
formatted_size = round(size/power, 2)
formatted_size = round(size / power, 2)
return f'{formatted_size} {size_name[i]}'


#pylint: disable=too-few-public-methods
class CondAction(argparse.Action):
""" A custom argparse action to support required arguments """

def __init__(self, option_strings, dest, nargs=None, **kwargs):
arg = kwargs.pop('to_be_required', [])
super().__init__(option_strings, dest, **kwargs)
Expand All @@ -97,6 +98,11 @@ def __call__(self, parser, namespace, values, option_string=None):
try:
setattr(namespace, self.dest, values)
return super().__call__(parser, namespace, values,
option_string)
option_string)
except NotImplementedError:
pass


def digest_to_short(digest):
""" Convert long digest to short """
return digest.split(':')[1][:12] if digest.startswith('sha256:') else digest