Skip to content

Commit 53a5a05

Browse files
committed
Refactoring more.
1 parent 19872ee commit 53a5a05

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

datashuttle/datashuttle_class.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -940,10 +940,11 @@ def setup_aws_connection(self) -> None:
940940

941941
def _try_set_rclone_password(self):
942942
""""""
943+
943944
pass_type = {
944945
"Windows": "Windows credential manager",
945946
"Linux": "the `pass` program",
946-
"Darwin": "macOS inbuild `security`."
947+
"Darwin": "macOS inbuild `security`.",
947948
}
948949

949950
input_ = utils.get_user_input(
@@ -957,14 +958,13 @@ def _try_set_rclone_password(self):
957958
try:
958959
self.set_rclone_password()
959960
except Exception as e:
960-
961961
config_path = self.cfg.get_rclone_config_filepath()
962962

963963
utils.log_and_raise_error(
964964
f"Password set up failed. The config at {config_path} contains the private ssh key without a password.\n"
965965
f"Use set_rclone_password()` to attempt to set the password again (see full error message above). ",
966966
RuntimeError,
967-
from_error=e
967+
from_error=e,
968968
)
969969

970970
utils.log_and_message("Password set successfully")
@@ -977,43 +977,26 @@ def set_rclone_password(self):
977977
"First, use `remove_rclone_password` to remove it."
978978
)
979979

980-
connection_method = self.cfg["connection_method"]
981-
982-
rclone_config_path = self.cfg.get_rclone_config_filepath()
980+
rclone_password.run_rclone_config_encrypt(self.cfg)
983981

984-
if not rclone_config_path.exists():
985-
raise RuntimeError(
986-
f"Rclone config file for: {connection_method} was not found. "
987-
f"Make sure you set up the connection first with `setup_{connection_method}_connection()`"
988-
)
989-
rclone_password.run_rclone_config_encrypt(
990-
self.cfg
991-
)
992-
993-
self.cfg.rclone_has_password[connection_method] = True
982+
self.cfg.rclone_has_password[self.cfg["connection_method"]] = True
994983

995984
self.cfg.save_rclone_password_state()
996985

997986
def remove_rclone_password(self):
998987
""""""
999988
if not self.cfg.connection_method_rclone_config_has_password():
1000989
raise RuntimeError(
1001-
f"The config for the current connection method: {self.cfg['connection_method']} does not have a password. Cannot remove."
990+
f"The config for the current connection method: {self.cfg['connection_method']} "
991+
f"does not have a password. Cannot remove."
1002992
)
1003-
config_filepath = rclone_password.get_password_filepath(self.cfg)
1004993

1005-
rclone_password.remove_rclone_password(
1006-
config_filepath, self.cfg.get_rclone_config_filepath()
1007-
)
994+
rclone_password.remove_rclone_password(self.cfg)
1008995

1009996
self.cfg.rclone_has_password[self.cfg["connection_method"]] = False
1010997

1011998
self.cfg.save_rclone_password_state()
1012999

1013-
utils.log_and_message(
1014-
f"Password removed from rclone config file: {}"
1015-
)
1016-
10171000
# -------------------------------------------------------------------------
10181001
# Configs
10191002
# -------------------------------------------------------------------------

datashuttle/utils/rclone_password.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from configs.config_class import Configs
88

99
from datashuttle.configs import canonical_folders
10+
from datashuttle.utils import utils
1011

1112

1213
def get_password_filepath(
@@ -47,6 +48,7 @@ def save_credentials_password(cfg):
4748
)
4849

4950
# run it
51+
# TODO: HANDLE ERRORS
5052
subprocess.run([shell, "-NoProfile", "-Command", ps_cmd], check=True)
5153

5254
elif platform.system() == "Linux":
@@ -58,6 +60,7 @@ def save_credentials_password(cfg):
5860
)
5961

6062
try:
63+
# TODO: HANDLE ERRORS
6164
result = subprocess.run(
6265
["pass", "ls"], capture_output=True, text=True, check=True
6366
)
@@ -66,6 +69,7 @@ def save_credentials_password(cfg):
6669
raise Exception() # re-raise unexpected errors
6770

6871
breakpoint()
72+
# TODO: HANDLE ERRORS
6973
subprocess.run(
7074
f"echo $(openssl rand -base64 40) | pass insert -m {cfg.get_rclone_config_name()}",
7175
shell=True,
@@ -74,6 +78,7 @@ def save_credentials_password(cfg):
7478

7579
# TODO: HANDLE ERRORS
7680
else:
81+
# TODO: HANDLE ERRORS
7782
subprocess.run(
7883
f"security add-generic-password -a datashuttle -s {cfg.get_rclone_config_name()} -w $(openssl rand -base64 40) -U",
7984
shell=True,
@@ -123,28 +128,48 @@ def set_credentials_as_password_command(cfg):
123128

124129
def run_rclone_config_encrypt(cfg: Configs):
125130
""""""
131+
rclone_config_path = cfg.get_rclone_config_filepath()
132+
133+
if not rclone_config_path.exists():
134+
connection_method = cfg["connection_method"]
135+
136+
raise RuntimeError(
137+
f"Rclone config file for: {connection_method} was not found. "
138+
f"Make sure you set up the connection first with `setup_{connection_method}_connection()`"
139+
)
140+
141+
save_credentials_password(cfg)
142+
126143
set_credentials_as_password_command(cfg)
127144

145+
# TODO: HANDLE ERRORS
128146
subprocess.run(
129-
f"rclone config encryption set --config {config_filepath.as_posix()}",
147+
f"rclone config encryption set --config {rclone_config_path.as_posix()}",
130148
shell=True,
131149
)
132150

133151
remove_credentials_as_password_command()
134152

135153

136154
# TODO: HANDLE ERRORS
137-
def remove_rclone_password(password_filepath: Path, config_filepath: Path):
155+
def remove_rclone_password(cfg):
138156
""""""
139-
set_credentials_as_password_command(Path(password_filepath))
157+
set_credentials_as_password_command(Path(cfg))
158+
159+
config_filepath = cfg.get_rclone_config_filepath()
160+
161+
# TODO: HANDLE ERRORS
140162
subprocess.run(
141163
rf"rclone config encryption remove --config {config_filepath.as_posix()}",
142164
shell=True,
143165
)
144166

145-
# TODO: HANDLE ERRORS
146167
remove_credentials_as_password_command()
147168

169+
utils.log_and_message(
170+
f"Password removed from rclone config file: {config_filepath}"
171+
)
172+
148173

149174
def remove_credentials_as_password_command():
150175
if "RCLONE_PASSWORD_COMMAND" in os.environ:

0 commit comments

Comments
 (0)