Skip to content

Commit eb3f098

Browse files
author
shrey
committed
refactor: centralize the get secret function
1 parent c0a7eca commit eb3f098

File tree

3 files changed

+52
-62
lines changed

3 files changed

+52
-62
lines changed

datashuttle/utils/aws.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import getpass
21
import json
3-
import sys
42

53
from datashuttle.configs.config_class import Configs
64
from datashuttle.utils import rclone, utils
@@ -41,34 +39,11 @@ def raise_if_bucket_absent(cfg: Configs) -> None:
4139

4240

4341
def get_aws_secret_access_key(log: bool = True) -> str:
44-
if not sys.stdin.isatty():
45-
proceed = input(
46-
"\nWARNING!\nThe next step is to enter a AWS secret access key, but it is not possible\n"
47-
"to hide your secret access key while entering it in the current terminal.\n"
48-
"This can occur if running the command in an IDE.\n\n"
49-
"Press 'y' to proceed to secret key entry. "
50-
"The characters will not be hidden!\n"
51-
"Alternatively, run AWS S3 setup after starting Python in your "
52-
"system terminal \nrather than through an IDE: "
53-
)
54-
if proceed != "y":
55-
utils.print_message_to_user(
56-
"Quitting AWS S3 setup as 'y' not pressed."
57-
)
58-
utils.log_and_raise_error(
59-
"AWS S3 setup aborted by user.", ConnectionAbortedError
60-
)
61-
62-
aws_secret_access_key = input(
63-
"Please enter your AWS secret access key. Characters will not be hidden: "
64-
)
65-
66-
else:
67-
aws_secret_access_key = getpass.getpass(
68-
"Please enter your AWS secret access key: "
69-
)
70-
71-
if log:
72-
utils.log("AWS secret access key entered by user.")
42+
aws_secret_access_key = utils.get_connection_secret_from_user(
43+
connection_method_name="AWS",
44+
key_name_full="AWS secret access key",
45+
key_name_short="secret key",
46+
log_status=log,
47+
)
7348

7449
return aws_secret_access_key.strip()

datashuttle/utils/gdrive.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import getpass
21
import json
3-
import sys
42

53
from datashuttle.configs.config_class import Configs
64
from datashuttle.utils import rclone, utils
@@ -112,34 +110,11 @@ def prompt_and_get_config_token(
112110

113111

114112
def get_client_secret(log: bool = True) -> str:
115-
if not sys.stdin.isatty():
116-
proceed = input(
117-
"\nWARNING!\nThe next step is to enter a google drive client secret, but it is not possible\n"
118-
"to hide your client secret while entering it in the current terminal.\n"
119-
"This can occur if running the command in an IDE.\n\n"
120-
"Press 'y' to proceed to client secret entry. "
121-
"The characters will not be hidden!\n"
122-
"Alternatively, run google drive setup after starting Python in your "
123-
"system terminal \nrather than through an IDE: "
124-
)
125-
if proceed != "y":
126-
utils.print_message_to_user(
127-
"Quitting google drive setup as 'y' not pressed."
128-
)
129-
utils.log_and_raise_error(
130-
"Google Drive setup aborted by user.", ConnectionAbortedError
131-
)
132-
133-
gdrive_client_secret = input(
134-
"Please enter your google drive client secret. Characters will not be hidden: "
135-
)
136-
137-
else:
138-
gdrive_client_secret = getpass.getpass(
139-
"Please enter your google drive client secret: "
140-
)
141-
142-
if log:
143-
utils.log("Google Drive client secret entered by user.")
113+
gdrive_client_secret = utils.get_connection_secret_from_user(
114+
connection_method_name="Google Drive",
115+
key_name_full="Google Drive client secret",
116+
key_name_short="secret key",
117+
log_status=log,
118+
)
144119

145120
return gdrive_client_secret.strip()

datashuttle/utils/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3+
import getpass
34
import re
5+
import sys
46
import traceback
57
import warnings
68
from typing import TYPE_CHECKING, Any, List, Literal, Union, overload
@@ -87,6 +89,44 @@ def get_user_input(message: str) -> str:
8789
return input_
8890

8991

92+
def get_connection_secret_from_user(
93+
connection_method_name: str,
94+
key_name_full: str,
95+
key_name_short: str,
96+
log_status: bool,
97+
) -> str:
98+
if not sys.stdin.isatty():
99+
proceed = input(
100+
f"\nWARNING!\nThe next step is to enter a {key_name_full}, but it is not possible\n"
101+
f"to hide your {key_name_short} while entering it in the current terminal.\n"
102+
f"This can occur if running the command in an IDE.\n\n"
103+
f"Press 'y' to proceed to {key_name_short} entry. "
104+
f"The characters will not be hidden!\n"
105+
f"Alternatively, run {connection_method_name} setup after starting Python in your "
106+
f"system terminal \nrather than through an IDE: "
107+
)
108+
if proceed != "y":
109+
print_message_to_user(
110+
f"Quitting {connection_method_name} setup as 'y' not pressed."
111+
)
112+
log_and_raise_error(
113+
f"{connection_method_name} setup aborted by user.",
114+
ConnectionAbortedError,
115+
)
116+
117+
input_ = input(
118+
f"Please enter your {key_name_full}. Characters will not be hidden: "
119+
)
120+
121+
else:
122+
input_ = getpass.getpass(f"Please enter your {key_name_full}: ")
123+
124+
if log_status:
125+
log(f"{key_name_full} entered by user.")
126+
127+
return input_
128+
129+
90130
# -----------------------------------------------------------------------------
91131
# Paths
92132
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)