This commit is contained in:
Arctic 2025-03-08 00:43:48 -06:00
parent 414266eefc
commit ffb1f7e204
5 changed files with 636 additions and 312 deletions

View file

@ -1,15 +1,24 @@
# ssh_manager/config.py
import os
from pathlib import Path
from typing import Final
import subprocess
# Paths
SSH_DIR = os.path.expanduser("~/.ssh")
CONF_DIR = os.path.join(SSH_DIR, "conf")
SOCKET_DIR = os.path.join(SSH_DIR, "s")
MAIN_CONFIG = os.path.join(SSH_DIR, "config")
# Use Path for more efficient path handling
HOME: Final[Path] = Path.home()
SSH_DIR: Final[Path] = HOME / ".ssh"
CONF_DIR: Final[Path] = SSH_DIR / "conf"
SOCKET_DIR: Final[Path] = SSH_DIR / "s"
MAIN_CONFIG: Final[Path] = SSH_DIR / "config"
# Validate paths on import
for path in (SSH_DIR, CONF_DIR, SOCKET_DIR):
if not path.exists():
path.mkdir(mode=0o700, parents=True, exist_ok=True)
# Default SSH config content if ~/.ssh/config is missing
DEFAULT_CONFIG_CONTENT = """###
DEFAULT_CONFIG_CONTENT: Final[str] = """###
#Local ssh
###
@ -30,3 +39,28 @@ Host *
ControlPersist 72000
ControlPath ~/.ssh/s/%C
"""
# Export string versions for backward compatibility
SSH_DIR_STR: Final[str] = str(SSH_DIR)
CONF_DIR_STR: Final[str] = str(CONF_DIR)
SOCKET_DIR_STR: Final[str] = str(SOCKET_DIR)
MAIN_CONFIG_STR: Final[str] = str(MAIN_CONFIG)
def validate_key_path(key_path: Path) -> bool:
if not key_path.exists():
return False
if not key_path.is_dir():
return False
if not os.access(key_path, os.W_OK):
return False
return True
def update_config_with_key(key_path: Path) -> bool:
if not validate_key_path(key_path):
return False # Early return if path validation fails
try:
subprocess.check_call([...])
except subprocess.CalledProcessError as e:
print_error(f"Error generating new SSH key: {e}")
return False