#! /usr/bin/env python

import logging
import os
import sys

# Quickly show version when flag exists.
if '--version' in sys.argv:
    version_file = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'grow', 'VERSION'))
    with open(version_file) as version_file:
        print(version_file.read().strip())
    sys.exit(0)

# We want to inject the use of SecureTransport as early as possible so that any
# references or sessions or what have you are ensured to have it, however we
# only want to do this in the case that we're running on macOS and the linked
# OpenSSL is too old to handle TLSv1.2
try:
    import ssl
except ImportError:
    pass
else:
    if (sys.platform == "darwin" and
            ssl.OPENSSL_VERSION_NUMBER < 0x1000100f):  # OpenSSL 1.0.1
        try:
            from urllib3.contrib import securetransport
        except (ImportError, OSError):
            pass
        else:
            securetransport.inject_into_urllib3()

logging.basicConfig(level=logging.INFO, format='%(message)s')

# TODO: Find a better way to allow importing grow packages.
sys.path.extend(
    [os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')])

from grow import commands
from grow.commands import group

commands.add_subcommands(group.grow)

if __name__ == '__main__':
    # pylint: disable=no-value-for-parameter
    group.grow()
