|
|
|
@ -7,9 +7,13 @@ from ssh_config_utils.parser import parse_config_text |
|
|
|
class ConfigFile: |
|
|
|
def __init__(self, global_host, hosts): |
|
|
|
self.hosts = {} |
|
|
|
global_host = global_host |
|
|
|
self.global_host = global_host |
|
|
|
for host in hosts: |
|
|
|
self.hosts.setdefault(host.name, host) |
|
|
|
if isinstance(host.name, list): |
|
|
|
name = host.name[0] |
|
|
|
else: |
|
|
|
name = host.name |
|
|
|
self.hosts.setdefault(name, host) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def read_file(cls, fp): |
|
|
|
@ -27,7 +31,7 @@ class ConfigFile: |
|
|
|
for key, values in host_data.items(): |
|
|
|
if len(values) == 1: |
|
|
|
host_data[key] = values[0] |
|
|
|
if host_data["host"] == "*": |
|
|
|
if "*" in host_data["host"]: |
|
|
|
if global_host is None: |
|
|
|
del host_data["host"] |
|
|
|
global_host = GlobalHost(host_data) |
|
|
|
@ -40,7 +44,14 @@ class ConfigFile: |
|
|
|
return self.format(indent=" " * 2, host_seperator="\n" * 2) |
|
|
|
|
|
|
|
def format(self, **format_data): |
|
|
|
hosts = sorted(self.hosts.values(), key=lambda host: host.name) |
|
|
|
hosts = sorted( |
|
|
|
self.hosts.values(), |
|
|
|
key=lambda host: host.name |
|
|
|
if isinstance(host.name, str) |
|
|
|
else " ".join(host.name), |
|
|
|
) |
|
|
|
if self.global_host is not None: |
|
|
|
hosts.insert(0, self.global_host) |
|
|
|
return format_data["host_seperator"].join( |
|
|
|
map(lambda host: host.format(**format_data), hosts) |
|
|
|
) |
|
|
|
|