55
66from pydantic import BaseModel , Field , field_validator
77
8- from app .utils .lib import FileManager
8+ from app .utils .directory_manager import create_directory
9+ from app .utils .file_manager import (
10+ append_to_file ,
11+ expand_user_path ,
12+ get_directory_path ,
13+ get_public_key_path ,
14+ read_file_content ,
15+ set_permissions ,
16+ )
917from app .utils .logger import create_logger
1018from app .utils .output_formatter import OutputFormatter
1119from app .utils .protocols import LoggerProtocol
@@ -140,7 +148,6 @@ def format_dry_run(self, config: "SSHConfig") -> str:
140148
141149class SSHKeyManager :
142150 def __init__ (self , logger : LoggerProtocol ):
143- self .file_manager = FileManager ()
144151 self .logger = logger
145152
146153 def _check_ssh_keygen_availability (self ) -> tuple [bool , str ]:
@@ -201,15 +208,15 @@ def set_key_permissions(self, private_key_path: str, public_key_path: str) -> tu
201208 self .logger .debug (debug_ssh_permission_setting .format (private_key = private_key_path , public_key = public_key_path ))
202209 try :
203210 self .logger .debug (debug_ssh_private_key_permissions .format (path = private_key_path ))
204- private_success , private_error = self . file_manager . set_permissions (
211+ private_success , private_error = set_permissions (
205212 private_key_path , stat .S_IRUSR | stat .S_IWUSR , self .logger
206213 )
207214 if not private_success :
208215 self .logger .debug (debug_ssh_private_key_permissions_failed .format (error = private_error ))
209216 return False , private_error
210217
211218 self .logger .debug (debug_ssh_public_key_permissions .format (path = public_key_path ))
212- public_success , public_error = self . file_manager . set_permissions (
219+ public_success , public_error = set_permissions (
213220 public_key_path , stat .S_IRUSR | stat .S_IWUSR | stat .S_IRGRP | stat .S_IROTH , self .logger
214221 )
215222 if not public_success :
@@ -227,7 +234,7 @@ def create_ssh_directory(self, ssh_dir: str) -> tuple[bool, str]:
227234 self .logger .debug (debug_ssh_directory_creation .format (directory = ssh_dir , permissions = oct (permissions )))
228235 try :
229236 self .logger .debug (debug_ssh_directory_check .format (directory = ssh_dir ))
230- success , error = self . file_manager . create_directory (ssh_dir , permissions , self .logger )
237+ success , error = create_directory (ssh_dir , permissions , self .logger )
231238 if success :
232239 self .logger .debug (debug_ssh_directory_created .format (directory = ssh_dir ))
233240 else :
@@ -242,12 +249,12 @@ def add_to_authorized_keys(self, public_key_path: str) -> tuple[bool, str]:
242249 self .logger .debug (adding_to_authorized_keys )
243250 self .logger .debug (debug_ssh_authorized_keys_read .format (path = public_key_path ))
244251
245- success , content , error = self . file_manager . read_file_content (public_key_path , self .logger )
252+ success , content , error = read_file_content (public_key_path , self .logger )
246253 if not success :
247254 self .logger .debug (debug_ssh_public_key_read_failed .format (error = error ))
248255 return False , error or failed_to_read_public_key
249256
250- ssh_dir = self . file_manager . expand_user_path ("~/.ssh" )
257+ ssh_dir = expand_user_path ("~/.ssh" )
251258 authorized_keys_path = os .path .join (ssh_dir , "authorized_keys" )
252259 self .logger .debug (debug_ssh_authorized_keys_path .format (path = authorized_keys_path ))
253260
@@ -269,7 +276,7 @@ def add_to_authorized_keys(self, public_key_path: str) -> tuple[bool, str]:
269276 return False , f"Failed to create authorized_keys file: { e } "
270277
271278 self .logger .debug (debug_ssh_authorized_keys_append .format (path = authorized_keys_path ))
272- success , error = self . file_manager . append_to_file (authorized_keys_path , content , self .logger )
279+ success , error = append_to_file (authorized_keys_path , content , logger = self .logger )
273280 if not success :
274281 self .logger .debug (debug_ssh_authorized_keys_append_failed .format (error = error ))
275282 return False , error or failed_to_append_to_authorized_keys
@@ -374,7 +381,6 @@ def __init__(self, config: SSHConfig, logger: LoggerProtocol = None, ssh_manager
374381 self .config = config
375382 self .ssh_manager = ssh_manager or SSHKeyManager (self .logger )
376383 self .formatter = SSHFormatter ()
377- self .file_manager = FileManager ()
378384
379385 def _validate_prerequisites (self ) -> bool :
380386 self .logger .debug (
@@ -383,7 +389,7 @@ def _validate_prerequisites(self) -> bool:
383389 )
384390 )
385391
386- expanded_key_path = self . file_manager . expand_user_path (self .config .path )
392+ expanded_key_path = expand_user_path (self .config .path )
387393 self .logger .debug (debug_ssh_path_expansion .format (original = self .config .path , expanded = expanded_key_path ))
388394
389395 if os .path .exists (expanded_key_path ):
@@ -428,8 +434,8 @@ def generate_ssh_key(self) -> SSHResult:
428434 dry_run_output = self .formatter .format_dry_run (self .config )
429435 return self ._create_result (True , dry_run_output )
430436
431- expanded_path = self . file_manager . expand_user_path (self .config .path )
432- ssh_dir = self . file_manager . get_directory_path (expanded_path )
437+ expanded_path = expand_user_path (self .config .path )
438+ ssh_dir = get_directory_path (expanded_path )
433439 self .logger .debug (debug_ssh_key_directory_info .format (directory = ssh_dir ))
434440
435441 if self .config .create_ssh_directory :
@@ -449,7 +455,7 @@ def generate_ssh_key(self) -> SSHResult:
449455
450456 if self .config .set_permissions :
451457 self .logger .debug (debug_ssh_permissions_enabled )
452- public_key_path = self . file_manager . get_public_key_path (expanded_path )
458+ public_key_path = get_public_key_path (expanded_path )
453459 self .logger .debug (debug_ssh_public_key_path_info .format (path = public_key_path ))
454460 success , error = self .ssh_manager .set_key_permissions (expanded_path , public_key_path )
455461 if not success :
@@ -458,7 +464,7 @@ def generate_ssh_key(self) -> SSHResult:
458464
459465 if self .config .add_to_authorized_keys :
460466 self .logger .debug (debug_ssh_authorized_keys_enabled )
461- public_key_path = self . file_manager . get_public_key_path (expanded_path )
467+ public_key_path = get_public_key_path (expanded_path )
462468 success , error = self .ssh_manager .add_to_authorized_keys (public_key_path )
463469 if not success :
464470 self .logger .debug (debug_ssh_authorized_keys_failed_abort .format (error = error ))
0 commit comments