#!/usr/bin/env python3

import botocore
from aws_cloudwatch_logs_utils.cloudwatch_logs import valid_time, parse_common_params, logs_cat, \
    LogStreamNotFoundError, TooManyLogStreamsMatched
import argparse


def main():
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument("--startTime", type=valid_time,
                            help="Start time in format \"YYYY-MM-DDTHH:MM:SS+HHMM\" or timestamp")
        parser.add_argument("--endTime", type=valid_time,
                            help="End time time in format \"YYYY-MM-DDTHH:MM:SS+HHMM\" or timestamp")

        params = parse_common_params(parser)

        args = parser.parse_args()

        params['start_time'] = args.startTime
        params['end_time'] = args.endTime

        logs_cat(**params, no_timestamp=args.no_timestamp)

    except (botocore.exceptions.ClientError, botocore.exceptions.ParamValidationError,
            LogStreamNotFoundError, TooManyLogStreamsMatched) as e:
        print("Error: {}".format(e))
        exit(1)

    except BrokenPipeError:
        exit(0)

    except KeyboardInterrupt:
        exit(2)


if __name__ == '__main__':
    main()
