|
2 | 2 |
|
3 | 3 | # all aws (apple with sauce) |
4 | 4 | from awth.config import initial_setup |
5 | | -from awth.util import log_error_and_exit, prompter |
| 5 | +from awth.util import log_error_and_exit |
6 | 6 | import boto3 |
7 | 7 | from botocore.exceptions import ClientError, ParamValidationError |
8 | 8 |
|
|
18 | 18 | import getpass |
19 | 19 | import keyring |
20 | 20 | import logging |
21 | | -from os import path, environ |
| 21 | +from os import path, environ, makedirs |
| 22 | +from pathlib import Path |
| 23 | +from rich.prompt import Confirm, Prompt |
22 | 24 | import sys |
23 | 25 |
|
24 | 26 | logger = logging.getLogger('awth') |
@@ -121,27 +123,36 @@ def main(device: str, |
121 | 123 | setup_logger(level) |
122 | 124 |
|
123 | 125 | if not path.isfile(AWS_CREDS_PATH): |
124 | | - console_input = prompter() |
125 | | - create = console_input( |
126 | | - f"Could not locate credentials file at {AWS_CREDS_PATH}, " |
127 | | - "would you like to create one? [y/n]" |
| 126 | + create_credentials_file = Confirm.ask( |
| 127 | + "Could not locate credentials file at " |
| 128 | + f"[green]{AWS_CREDS_PATH}[/green]. Would you like to create one?" |
128 | 129 | ) |
129 | 130 |
|
130 | | - if create.lower() == "y": |
131 | | - with open(AWS_CREDS_PATH, 'a'): |
132 | | - pass |
133 | | - else: |
134 | | - log_error_and_exit(logger, |
135 | | - 'Could not locate credentials file at ' |
136 | | - f'{AWS_CREDS_PATH}') |
| 131 | + if create_credentials_file: |
| 132 | + # try creating directory and file |
| 133 | + try: |
| 134 | + makedirs(path.expanduser("~/.aws"), exist_ok=True) |
| 135 | + Path(AWS_CREDS_PATH).touch() |
| 136 | + except Exception as e: |
| 137 | + log_error_and_exit(logger, |
| 138 | + f'{e}. Could not locate credentials file at ' |
| 139 | + f'{AWS_CREDS_PATH}') |
137 | 140 |
|
138 | 141 | config = get_config(AWS_CREDS_PATH) |
139 | 142 |
|
140 | 143 | if setup: |
141 | 144 | initial_setup(logger, config, AWS_CREDS_PATH, keychain) |
142 | 145 | return |
143 | 146 |
|
144 | | - validate(args, config) |
| 147 | + validate(config, |
| 148 | + profile, |
| 149 | + long_term_suffix, |
| 150 | + short_term_suffix, |
| 151 | + assume_role, |
| 152 | + keychain, |
| 153 | + device, |
| 154 | + duration, |
| 155 | + force) |
145 | 156 |
|
146 | 157 |
|
147 | 158 | def get_config(aws_creds_path: str = ""): |
@@ -364,9 +375,8 @@ def get_credentials(short_term_name, |
364 | 375 | logger.debug("Received token as argument") |
365 | 376 | mfa_token = str(token) |
366 | 377 | else: |
367 | | - console_input = prompter() |
368 | | - mfa_token = console_input(f'Enter AWS MFA code for device [{device}] ' |
369 | | - f'(renewing for {duration} seconds):') |
| 378 | + mfa_token = Prompt(f'Enter AWS MFA code for device [{device}] ' |
| 379 | + f'(renewing for {duration} seconds):') |
370 | 380 |
|
371 | 381 | client = boto3.client( |
372 | 382 | 'sts', |
|
0 commit comments