Skip to content

Commit

Permalink
Support termius groups expansion from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
lejmr committed Jun 24, 2018
1 parent 822eb9f commit 7897308
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions termius/porting/providers/ssh/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib2 import Path

from termius.core.commands.mixins import SshConfigMergerMixin
from termius.core.models.terminal import Host, SshConfig, Identity, SshKey
from termius.core.models.terminal import Host, SshConfig, Identity, SshKey, Group


class SSHConfigHostAdapter(SshConfigMergerMixin):
Expand Down Expand Up @@ -56,7 +56,7 @@ def adapt_instance_to_ssh_config_host(self, host_instance):

return adapted

def adapt_ssh_config_host_to_instance(self, alias, parsed_host):
def adapt_ssh_config_host_to_instance(self, alias, parsed_host, parsed_group=None):
"""Convert parsed host to application host."""
app_host = Host(
label=alias,
Expand All @@ -80,5 +80,7 @@ def adapt_ssh_config_host_to_instance(self, alias, parsed_host):
)

app_host.ssh_config = ssh_config
if parsed_group:
app_host.group = parsed_group

return app_host
6 changes: 6 additions & 0 deletions termius/porting/providers/ssh/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def parse(self, file_obj): # noqa
:param file_obj: a file-like object to read the config file from
"""
TERMIUS_GROUPS = "# termius:group"
termius_ignore_regexp = re.compile(r'# termius:ignore')
termius_groups_regexp = re.compile(r'{}'.format(TERMIUS_GROUPS))

host = {'host': ['*'], 'config': {}}

Expand All @@ -30,6 +32,10 @@ def parse(self, file_obj): # noqa
if ignore_comment:
host['config']['ignore'] = ''

groups_comment = termius_groups_regexp.match(line)
if groups_comment:
host['group'] = line.replace(TERMIUS_GROUPS, "").strip()

continue

match = re.match(self.SETTINGS_REGEX, line)
Expand Down
16 changes: 13 additions & 3 deletions termius/porting/providers/ssh/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from os.path import expanduser
from pathlib2 import Path

from termius.core.models.terminal import Host
from termius.core.models.terminal import Host, Group

from ..base import BasePortingProvider
from .parser import SSHConfigParser
Expand Down Expand Up @@ -50,16 +50,26 @@ def provider_hosts(self):
]

to_import = []

parsed_group = None
for alias in parsed_hosts:
parsed_group_name = parser._config[parsed_hosts.index(alias) + 1].get("group", None)
if parsed_group_name:
matching_groups = [x for x in self.storage.get_all(Group) if x['label'] == parsed_group_name]
if len(matching_groups) == 0:
g = Group(label=parsed_group_name)
self.storage.create(g)
matching_groups = [g]

item = matching_groups.pop()
parsed_group = Group(**item)
parsed_host = parser.lookup(alias)

if 'ignore' in parsed_host:
continue

to_import.append(
self.adapter.adapt_ssh_config_host_to_instance(
alias, parsed_host
alias, parsed_host, parsed_group
)
)

Expand Down

0 comments on commit 7897308

Please sign in to comment.