converted into multifile for ease

This commit is contained in:
Arctic 2025-03-07 04:08:40 -06:00
parent bfdcf11212
commit 2a7cb7dcb7
9 changed files with 148 additions and 176 deletions

View file

@ -1,27 +1,12 @@
# ssh_manager/edit_host.py
import os
from collections import OrderedDict
class Colors:
GREEN = "\033[0;32m"
RED = "\033[0;31m"
YELLOW = "\033[1;33m"
CYAN = "\033[0;36m"
BOLD = "\033[1m"
RESET = "\033[0m"
def print_error(message):
print(f"{Colors.RED}{Colors.BOLD}[✖] {Colors.RESET}{message}")
def print_warning(message):
print(f"{Colors.YELLOW}{Colors.BOLD}[⚠] {Colors.RESET}{message}")
def print_info(message):
print(f"{Colors.GREEN}{Colors.BOLD}[✔] {Colors.RESET}{message}")
from .utils import print_error, print_warning, print_info
def load_config_file(file_path):
"""
Parse a single SSH config file and return a list of host blocks.
Each block is an OrderedDict with keys like 'Host', 'HostName', etc.
Parse the given config file into a list of host blocks (OrderedDict).
"""
blocks = []
host_data = None
@ -35,60 +20,46 @@ def load_config_file(file_path):
for line in lines:
stripped_line = line.strip()
# Skip empty lines and comments
if not stripped_line or stripped_line.startswith('#'):
continue
# Start of a new Host block
if stripped_line.lower().startswith('host '):
host_labels = stripped_line.split()[1:]
# Pick the first label that isn't a wildcard
for label in host_labels:
if '*' not in label:
# If we already have a host_data in progress, close it out
if host_data:
blocks.append(host_data)
host_data = OrderedDict({'Host': label})
break
elif host_data:
# Split on the first whitespace into key/value
if ' ' in stripped_line:
key, value = stripped_line.split(None, 1)
host_data[key] = value.strip()
# Add the last block if it exists
if host_data:
blocks.append(host_data)
return blocks
def edit_host(CONF_DIR):
def edit_host(conf_dir):
"""
Let the user update fields for an existing host.
1) Ask which host label to edit
2) Locate its subdirectory + config
3) Update (HostName, User, Port, IdentityFile) as needed
4) Rewrite the config
Let the user update fields for an existing host in ~/.ssh/conf/<label>/config.
"""
host_label = input("Enter the Host label to edit: ").strip()
if not host_label:
print_error("Host label cannot be empty.")
return
host_dir = os.path.join(CONF_DIR, host_label)
host_dir = os.path.join(conf_dir, host_label)
config_path = os.path.join(host_dir, "config")
if not os.path.isfile(config_path):
print_warning(f"No config file found at {config_path}; cannot edit this host.")
return
# Load the config file and look for the relevant host block
blocks = load_config_file(config_path)
if not blocks:
print_warning(f"No valid Host blocks found in {config_path}")
return
# We'll assume there's only one block in each config
# (the "Host <label>"). If multiple blocks exist, adapt accordingly.
target_block = None
for b in blocks:
if b.get("Host") == host_label:
@ -110,13 +81,11 @@ def edit_host(CONF_DIR):
new_port = input(f"Enter new Port [{old_port}]: ").strip()
new_ident = input(f"Enter new IdentityFile [{old_identity}]: ").strip()
# If user leaves it blank, keep the old value
final_hostname = new_hostname if new_hostname else old_hostname
final_user = new_user if new_user else old_user
final_port = new_port if new_port else old_port
final_ident = new_ident if new_ident else old_identity
# Rebuild the config lines
new_config_lines = [
f"Host {host_label}",
f" HostName {final_hostname}",
@ -126,7 +95,6 @@ def edit_host(CONF_DIR):
if final_ident:
new_config_lines.append(f" IdentityFile {final_ident}")
# Overwrite the file
try:
with open(config_path, "w") as f:
for line in new_config_lines: