Skip to content

Commit 6ad3c3d

Browse files
committed
Remove custom Command dataclass, misc. edits to eliminate TypeErrors
1 parent f0a88c4 commit 6ad3c3d

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

Diff for: plugins/connection/aws_ssm.py

+24-35
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,6 @@ def filter_ansi(line: str, is_windows: bool) -> str:
436436
return line
437437

438438

439-
@dataclass
440-
class Command:
441-
"""
442-
Custom dataclass for the generated command dictionaries.
443-
"""
444-
445-
command: str
446-
method: str # 'get' or 'put'
447-
headers: Dict[str, str]
448-
449-
450439
@dataclass
451440
class CommandResult:
452441
"""
@@ -985,7 +974,7 @@ def _generate_commands(
985974
s3_path: str,
986975
in_path: str,
987976
out_path: str,
988-
) -> Tuple[List[Command], Optional[Dict]]:
977+
) -> Tuple[List[Dict], dict]:
989978
"""
990979
Generate commands for the specified bucket, S3 path, input path, and output path.
991980
@@ -995,7 +984,7 @@ def _generate_commands(
995984
:param out_path: Output path
996985
:param method: The request method to use for the command (can be "get" or "put").
997986
998-
:returns: List of Command dictionaries containing the command string and metadata.
987+
:returns: A tuple containing a list of command dictionaries along with any ``put_args`` dictionaries.
999988
"""
1000989

1001990
put_args, put_headers = self._generate_encryption_settings()
@@ -1029,7 +1018,7 @@ def _generate_commands(
10291018
),
10301019
# The "method" key indicates to _file_transport_command which commands are put_commands
10311020
"method": "put",
1032-
"headers": put_headers
1021+
"headers": put_headers,
10331022
}) # fmt: skip
10341023
else:
10351024
put_command_headers = " ".join([f"-H '{h}: {v}'" for h, v in put_headers.items()])
@@ -1055,6 +1044,8 @@ def _generate_commands(
10551044
"touch "
10561045
f"'{out_path}'"
10571046
),
1047+
"method": "get",
1048+
"headers": {},
10581049
}) # fmt: skip
10591050
commands.append({
10601051
"command":
@@ -1066,12 +1057,12 @@ def _generate_commands(
10661057
),
10671058
# The "method" key indicates to _file_transport_command which commands are put_commands
10681059
"method": "put",
1069-
"headers": put_headers
1060+
"headers": put_headers,
10701061
}) # fmt: skip
10711062

10721063
return commands, put_args
10731064

1074-
def _exec_transport_commands(self, in_path: str, out_path: str, commands: List[Command]) -> CommandResult:
1065+
def _exec_transport_commands(self, in_path: str, out_path: str, commands: List[dict]) -> Tuple[int, str, str]:
10751066
"""
10761067
Execute the provided transport commands.
10771068
@@ -1084,7 +1075,10 @@ def _exec_transport_commands(self, in_path: str, out_path: str, commands: List[C
10841075

10851076
stdout_combined, stderr_combined = "", ""
10861077
for command in commands:
1087-
(returncode, stdout, stderr) = self.exec_command(command["command"], in_data=None, sudoable=False)
1078+
result = self.exec_command(command["command"], in_data=None, sudoable=False)
1079+
1080+
returncode = result[0]
1081+
stdout, stderr = result[1], result[2]
10881082

10891083
# Check the return code
10901084
if returncode != 0:
@@ -1101,7 +1095,7 @@ def _file_transport_command(
11011095
in_path: str,
11021096
out_path: str,
11031097
ssm_action: str,
1104-
) -> CommandResult:
1098+
) -> Tuple[int, str, str]:
11051099
"""
11061100
Transfer file(s) to/from host using an intermediate S3 bucket and then delete the file(s).
11071101
@@ -1117,30 +1111,25 @@ def _file_transport_command(
11171111

11181112
client = self._s3_client
11191113

1114+
commands, put_args = self._generate_commands(
1115+
bucket_name,
1116+
s3_path,
1117+
in_path,
1118+
out_path,
1119+
)
1120+
11201121
try:
11211122
if ssm_action == "get":
1122-
put_commands, put_args = self._generate_commands(
1123-
bucket_name,
1124-
s3_path,
1125-
in_path,
1126-
out_path,
1127-
)
1128-
put_commands = [cmd["command"] for cmd in put_commands if cmd.get("method") == "put"]
1129-
(returncode, stdout, stderr) = self._exec_transport_commands(in_path, out_path, put_commands)
1123+
put_commands = [cmd for cmd in commands if cmd.get("method") == "put"]
1124+
result = self._exec_transport_commands(in_path, out_path, put_commands)
11301125
with open(to_bytes(out_path, errors="surrogate_or_strict"), "wb") as data:
11311126
client.download_fileobj(bucket_name, s3_path, data)
11321127
else:
1133-
get_commands, put_args = self._generate_commands(
1134-
bucket_name,
1135-
s3_path,
1136-
in_path,
1137-
out_path,
1138-
)
1139-
get_commands = [cmd["command"] for cmd in get_commands if cmd.get("method") == "get"]
1128+
get_commands = [cmd for cmd in commands if cmd.get("method") == "get"]
11401129
with open(to_bytes(in_path, errors="surrogate_or_strict"), "rb") as data:
11411130
client.upload_fileobj(data, bucket_name, s3_path, ExtraArgs=put_args)
1142-
(returncode, stdout, stderr) = self._exec_transport_commands(in_path, out_path, get_commands)
1143-
return CommandResult(returncode, stdout, stderr)
1131+
result = self._exec_transport_commands(in_path, out_path, get_commands)
1132+
return CommandResult(result)
11441133
finally:
11451134
# Remove the files from the bucket after they've been transferred
11461135
client.delete_object(Bucket=bucket_name, Key=s3_path)

0 commit comments

Comments
 (0)