#!/usr/bin/python3

# Copyright 2021 SUSE LLC
#
# This file is part of gceimgutils
#
# gceimgutils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# gceimgutils is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with gceremoveimg. If not, see <http://www.gnu.org/licenses/>.

import argparse
import logging
import os
import sys

import gceimgutils.gceutils as utils
import gceimgutils.gcelistimg as gcelsimg

# Set up command line argument parsing
argparse = argparse.ArgumentParser(description='List images in GCE')
argparse.add_argument(
    '-c', '--credentials-file',
    dest='credentials_file_path',
    help='Path to the service account credentials file',
    metavar='CREDENTIALS_FILE'
)
argparse.add_argument(
    '-p', '--project',
    dest='project_name',
    help='Project to use for image listing',
    metavar='PROJECT_NAME',
    required=True
)
# Note, one of the arguments in the group is required if --version is
# not specified. However setting this behavior through the parser
# also requires one argument to be specified even if --version is specified
# This parser behavior is true even if --version and the group are part of the
# same subgroup
list_image_condition_group = argparse.add_mutually_exclusive_group()
list_image_condition_group.add_argument(
    '--image-name',
    dest='image_name',
    help='The image name of the image to be listed (Optional)',
    metavar='IMAGE_NAME'
)
help_msg = 'An image name fragment to match the image name of the image to be '
help_msg += 'lisetd (Optional)'
list_image_condition_group.add_argument(
    '--image-name-frag',
    dest='image_name_frag',
    help=help_msg,
    metavar='IMAGE_NAME_FRAGMENT'
)
help_msg = 'A regular expression to match the image name of the image to be '
help_msg += 'listed (Optional)'
list_image_condition_group.add_argument(
    '--image-name-match',
    dest='image_name_match',
    help=help_msg,
    metavar='REGEX'
)
argparse.add_argument(
    '--verbose',
    action='store_true',
    default=False,
    dest='verbose',
    help='Enable debug output'
)
argparse.add_argument(
    '--detail',
    action='store_true',
    default=False,
    dest='detail',
    help='Enables detailed image output'
)
argparse.add_argument(
    '--version',
    action='version',
    version=utils.get_version(),
    help='Program version'
)

args = argparse.parse_args()

logger = utils.get_logger(args.verbose)

credentials_file = None
if args.credentials_file_path:
    credentials_file = os.path.expanduser(args.credentials_file_path)

log_level = logging.INFO
if args.verbose:
    logging.DEBUG

lister = gcelsimg.GCEListImage(
    credentials_path=credentials_file,
    image_name=args.image_name,
    image_name_fragment=args.image_name_frag,
    image_name_match=args.image_name_match,
    log_callback=logger,
    log_level=log_level,
    project=args.project_name,
    detail=args.detail
)

try:
    lister.list_images()
except Exception as e:
    if args.verbose:
        logger.exception(e)
    else:
        logger.info(f"Error: Unable to list images {e}")
    sys.exit(1)
