Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support termius groups expansion from config file #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion termius/porting/providers/ssh/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ 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 +81,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_group = "# termius:group"
termius_ignore_regexp = re.compile(r'# termius:ignore')
termius_groups_regexp = re.compile(r'{}'.format(termius_group))

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_group, "").strip()

continue

match = re.match(self.SETTINGS_REGEX, line)
Expand Down
18 changes: 15 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,28 @@ 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