Skip to content

Commit 75350b2

Browse files
committed
remove custom prompting in favor of rich
1 parent 47061b7 commit 75350b2

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

awth/__init__.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# all aws (apple with sauce)
44
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
66
import boto3
77
from botocore.exceptions import ClientError, ParamValidationError
88

@@ -18,7 +18,9 @@
1818
import getpass
1919
import keyring
2020
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
2224
import sys
2325

2426
logger = logging.getLogger('awth')
@@ -121,27 +123,36 @@ def main(device: str,
121123
setup_logger(level)
122124

123125
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?"
128129
)
129130

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}')
137140

138141
config = get_config(AWS_CREDS_PATH)
139142

140143
if setup:
141144
initial_setup(logger, config, AWS_CREDS_PATH, keychain)
142145
return
143146

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)
145156

146157

147158
def get_config(aws_creds_path: str = ""):
@@ -364,9 +375,8 @@ def get_credentials(short_term_name,
364375
logger.debug("Received token as argument")
365376
mfa_token = str(token)
366377
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):')
370380

371381
client = boto3.client(
372382
'sts',

awth/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from awth.util import log_error_and_exit, prompter
1+
from awth.util import log_error_and_exit
2+
3+
from rich.prompt import Prompt
24

35
try:
46
import configparser
@@ -12,9 +14,7 @@
1214

1315

1416
def initial_setup(logger, config, config_path, no_keychain=False):
15-
console_input = prompter()
16-
17-
profile_name = console_input('Profile name to [%s]: ' % ("default"))
17+
profile_name = Prompt('Profile name to [default]: ')
1818
if profile_name is None or profile_name == "":
1919
profile_name = "default"
2020

awth/util.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,3 @@ def log_error_and_exit(logger, message):
77
"""
88
logger.error(message)
99
sys.exit(1)
10-
11-
12-
def prompter():
13-
try:
14-
console_input = raw_input
15-
except NameError:
16-
console_input = input
17-
18-
return console_input

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "awth"
3-
version = "0.1.0a"
3+
version = "0.1.0a1"
44
description = "awth your way into aws, again, with mfa"
55
authors = [
66
"jessebot <[email protected]>",

0 commit comments

Comments
 (0)