#!python

from __future__ import print_function
import click
import sys
from kwrappers.util import Session

@click.command(context_settings=dict(max_content_width=120))
@click.option('--profile', '-p', default='default', help="A profile defined in `~/.aws/credentials`.  If it requires an MFA token a prompt will be given")
def main(profile):
    """Gets temporary AWS session token from a profile.  Allows you to export into your shell and run tools expecting the AWS standard environment variables. Namely:

        - AWS_ACCESS_KEY_ID
        - AWS_SECRET_ACCESS_KEY
        - AWS_SESSION_TOKEN

        Returns: commands to copy to your shell terminal
    """
    try:
        session = Session(profile=profile)
        creds = session.get_session_creds()
    except Exception as e:
        print("{}: {}".format(type(e).__name__, e))
        sys.exit(1)

    print("Keys and token for profile: '{profile}'".format(profile=profile))
    print("Paste the following into your shell:\n")
    print("export AWS_ACCESS_KEY_ID={}".format(creds.access_key))
    print("export AWS_SECRET_ACCESS_KEY={}".format(creds.secret_key))
    print("export AWS_SESSION_TOKEN={}".format(creds.token))

if __name__ == '__main__':
    main()
